KickJava   Java API By Example, From Geeks To Geeks.

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


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: LeafNodeLayoutManager.java 462812 2006-10-11 14:19:28Z spepping $ */
19
20 package org.apache.fop.layoutmgr.inline;
21
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.fop.area.Area;
28 import org.apache.fop.area.inline.InlineArea;
29 import org.apache.fop.fo.FObj;
30 import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
31 import org.apache.fop.layoutmgr.AbstractLayoutManager;
32 import org.apache.fop.layoutmgr.InlineKnuthSequence;
33 import org.apache.fop.layoutmgr.KnuthGlue;
34 import org.apache.fop.layoutmgr.KnuthPenalty;
35 import org.apache.fop.layoutmgr.KnuthSequence;
36 import org.apache.fop.layoutmgr.LayoutContext;
37 import org.apache.fop.layoutmgr.LeafPosition;
38 import org.apache.fop.layoutmgr.Position;
39 import org.apache.fop.layoutmgr.PositionIterator;
40 import org.apache.fop.layoutmgr.TraitSetter;
41 import org.apache.fop.traits.MinOptMax;
42
43
44 /**
45  * Base LayoutManager for leaf-node FObj, ie: ones which have no children.
46  * These are all inline objects. Most of them cannot be split (Text is
47  * an exception to this rule.)
48  * This class can be extended to handle the creation and adding of the
49  * inline area.
50  */

51 public abstract class LeafNodeLayoutManager extends AbstractLayoutManager
52                                    implements InlineLevelLayoutManager {
53
54     /**
55      * logging instance
56      */

57     private static Log log = LogFactory.getLog(LeafNodeLayoutManager.class);
58
59     /**
60      * The inline area that this leafnode will add.
61      */

62     protected InlineArea curArea = null;
63     /** Any border, padding and background properties applying to this area */
64     protected CommonBorderPaddingBackground commonBorderPaddingBackground = null;
65     /** The alignment context applying to this area */
66     protected AlignmentContext alignmentContext = null;
67     
68     private MinOptMax ipd;
69
70     /** Flag to indicate if something was changed as part of the getChangeKnuthElements sequence */
71     protected boolean isSomethingChanged = false;
72     /** Our area info for the Knuth elements */
73     protected AreaInfo areaInfo = null;
74
75     /**
76      * Store information about the inline area
77      */

78     protected class AreaInfo {
79         protected short iLScount;
80         protected MinOptMax ipdArea;
81         protected boolean bHyphenated;
82         protected AlignmentContext alignmentContext;
83
84         public AreaInfo(short iLS, MinOptMax ipd, boolean bHyph,
85                         AlignmentContext alignmentContext) {
86             iLScount = iLS;
87             ipdArea = ipd;
88             bHyphenated = bHyph;
89             this.alignmentContext = alignmentContext;
90         }
91         
92     }
93
94
95     /**
96      * Create a Leaf node layout mananger.
97      * @param node the FObj to attach to this LM.
98      */

99     public LeafNodeLayoutManager(FObj node) {
100         super(node);
101     }
102
103     /**
104      * Create a Leaf node layout mananger.
105      */

106     public LeafNodeLayoutManager() {
107     }
108
109     /**
110      * get the inline area.
111      * @param context the context used to create the area
112      * @return the current inline area for this layout manager
113      */

114     public InlineArea get(LayoutContext context) {
115         return curArea;
116     }
117
118     /**
119      * Check if this inline area is resolved due to changes in
120      * page or ipd.
121      * Currently not used.
122      * @return true if the area is resolved when adding
123      */

124     public boolean resolved() {
125         return false;
126     }
127
128     /**
129      * Set the current inline area.
130      * @param ia the inline area to set for this layout manager
131      */

132     public void setCurrentArea(InlineArea ia) {
133         curArea = ia;
134     }
135
136     /**
137      * This is a leaf-node, so this method is never called.
138      * @param childArea the childArea to add
139      */

140     public void addChildArea(Area childArea) {
141     }
142
143     /**
144      * This is a leaf-node, so this method is never called.
145      * @param childArea the childArea to get the parent for
146      * @return the parent area
147      */

148     public Area getParentArea(Area childArea) {
149         return null;
150     }
151
152     /**
153      * Set the border and padding properties of the inline area.
154      * @param commonBorderPaddingBackground the alignment adjust property
155      */

156     protected void setCommonBorderPaddingBackground(
157             CommonBorderPaddingBackground commonBorderPaddingBackground) {
158         this.commonBorderPaddingBackground = commonBorderPaddingBackground;
159     }
160
161     /**
162      * Get the allocation ipd of the inline area.
163      * This method may be overridden to handle percentage values.
164      * @param refIPD the ipd of the parent reference area
165      * @return the min/opt/max ipd of the inline area
166      */

167     protected MinOptMax getAllocationIPD(int refIPD) {
168         return new MinOptMax(curArea.getIPD());
169     }
170
171     /**
172      * Add the area for this layout manager.
173      * This adds the single inline area to the parent.
174      * @param posIter the position iterator
175      * @param context the layout context for adding the area
176      */

177     public void addAreas(PositionIterator posIter, LayoutContext context) {
178         addId();
179
180         InlineArea area = getEffectiveArea();
181         if (area.getAllocIPD() > 0 || area.getAllocBPD() > 0) {
182             offsetArea(area, context);
183             widthAdjustArea(area, context);
184             if (commonBorderPaddingBackground != null) {
185                 // Add border and padding to area
186
TraitSetter.setBorderPaddingTraits(area,
187                                                    commonBorderPaddingBackground,
188                                                    false, false, this);
189                 TraitSetter.addBackground(area, commonBorderPaddingBackground, this);
190             }
191             parentLM.addChildArea(area);
192         }
193
194         while (posIter.hasNext()) {
195             posIter.next();
196         }
197     }
198
199     /**
200      * @return the effective area to be added to the area tree. Normally, this is simply "curArea"
201      * but in the case of page-number(-citation) curArea is cloned, updated and returned.
202      */

203     protected InlineArea getEffectiveArea() {
204         return curArea;
205     }
206     
207     /**
208      * This method is called by addAreas() so IDs can be added to a page for FOs that
209      * support the 'id' property.
210      */

211     protected void addId() {
212         // Do nothing here, overriden in subclasses that have an 'id' property.
213
}
214     
215     /**
216      * Offset this area.
217      * Offset the inline area in the bpd direction when adding the
218      * inline area.
219      * This is used for vertical alignment.
220      * Subclasses should override this if necessary.
221      * @param area the inline area to be updated
222      * @param context the layout context used for adding the area
223      */

224     protected void offsetArea(InlineArea area, LayoutContext context) {
225         area.setOffset(alignmentContext.getOffset());
226     }
227
228     /**
229      * Creates a new alignment context or returns the current
230      * alignment context.
231      * This is used for vertical alignment.
232      * Subclasses should override this if necessary.
233      * @param context the layout context used
234      * @return the appropriate alignment context
235      */

236     protected AlignmentContext makeAlignmentContext(LayoutContext context) {
237         return context.getAlignmentContext();
238     }
239
240     /**
241      * Adjust the width of the area when adding.
242      * This uses the min/opt/max values to adjust the with
243      * of the inline area by a percentage.
244      * @param area the inline area to be updated
245      * @param context the layout context for adding this area
246      */

247     protected void widthAdjustArea(InlineArea area, LayoutContext context) {
248         double dAdjust = context.getIPDAdjust();
249         int width = areaInfo.ipdArea.opt;
250         if (dAdjust < 0) {
251             width = (int) (width + dAdjust * (areaInfo.ipdArea.opt
252                                              - areaInfo.ipdArea.min));
253         } else if (dAdjust > 0) {
254             width = (int) (width + dAdjust * (areaInfo.ipdArea.max
255                                              - areaInfo.ipdArea.opt));
256         }
257         area.setIPD(width);
258         area.setAdjustment(width - areaInfo.ipdArea.opt);
259     }
260     
261     /** @see org.apache.fop.layoutmgr.LayoutManager#getNextKnuthElements(LayoutContext, int) */
262     public LinkedList JavaDoc getNextKnuthElements(LayoutContext context, int alignment) {
263         curArea = get(context);
264         
265         if (curArea == null) {
266             setFinished(true);
267             return null;
268         }
269
270         alignmentContext = makeAlignmentContext(context);
271         
272         MinOptMax ipd = getAllocationIPD(context.getRefIPD());
273
274         // create the AreaInfo object to store the computed values
275
areaInfo = new AreaInfo((short) 0, ipd, false, alignmentContext);
276
277         // node is a fo:ExternalGraphic, fo:InstreamForeignObject,
278
// fo:PageNumber or fo:PageNumberCitation
279
KnuthSequence seq = new InlineKnuthSequence();
280
281         addKnuthElementsForBorderPaddingStart(seq);
282         
283         seq.add(new KnuthInlineBox(areaInfo.ipdArea.opt, alignmentContext,
284                                     notifyPos(new LeafPosition(this, 0)), false));
285
286         addKnuthElementsForBorderPaddingEnd(seq);
287         
288         LinkedList JavaDoc returnList = new LinkedList JavaDoc();
289
290         returnList.add(seq);
291         setFinished(true);
292         return returnList;
293     }
294
295     /** @see InlineLevelLayoutManager#addALetterSpaceTo(List) */
296     public List JavaDoc addALetterSpaceTo(List JavaDoc oldList) {
297         // return the unchanged elements
298
return oldList;
299     }
300
301     /**
302      * Remove the word space represented by the given elements
303      *
304      * @param oldList the elements representing the word space
305      */

306     public void removeWordSpace(List JavaDoc oldList) {
307         // do nothing
308
log.warn(this.getClass().getName() + " should not receive a call to removeWordSpace(list)");
309     }
310
311     /** @see InlineLevelLayoutManager#getWordChars(StringBuffer, Position) */
312     public void getWordChars(StringBuffer JavaDoc sbChars, Position pos) {
313     }
314
315     /** @see InlineLevelLayoutManager#hyphenate(Position, HyphContext) */
316     public void hyphenate(Position pos, HyphContext hc) {
317     }
318
319     /** @see InlineLevelLayoutManager#applyChanges(List) */
320     public boolean applyChanges(List JavaDoc oldList) {
321         setFinished(false);
322         return false;
323     }
324
325     /** @see org.apache.fop.layoutmgr.LayoutManager#getChangedKnuthElements(List, int) */
326     public LinkedList JavaDoc getChangedKnuthElements(List JavaDoc oldList,
327                                               int alignment) {
328         if (isFinished()) {
329             return null;
330         }
331
332         LinkedList JavaDoc returnList = new LinkedList JavaDoc();
333
334         addKnuthElementsForBorderPaddingStart(returnList);
335         
336         // fobj is a fo:ExternalGraphic, fo:InstreamForeignObject,
337
// fo:PageNumber or fo:PageNumberCitation
338
returnList.add(new KnuthInlineBox(areaInfo.ipdArea.opt, areaInfo.alignmentContext,
339                                           notifyPos(new LeafPosition(this, 0)), true));
340
341         addKnuthElementsForBorderPaddingEnd(returnList);
342         
343         setFinished(true);
344         return returnList;
345     }
346     
347     /**
348      * Creates Knuth elements for start border padding and adds them to the return list.
349      * @param returnList return list to add the additional elements to
350      */

351     protected void addKnuthElementsForBorderPaddingStart(List JavaDoc returnList) {
352         //Border and Padding (start)
353
if (commonBorderPaddingBackground != null) {
354             int ipStart = commonBorderPaddingBackground.getBorderStartWidth(false)
355                          + commonBorderPaddingBackground.getPaddingStart(false, this);
356             if (ipStart > 0) {
357                 // Add a non breakable glue
358
returnList.add(new KnuthPenalty(0, KnuthPenalty.INFINITE,
359                                                 false, new LeafPosition(this, -1), true));
360                 returnList.add(new KnuthGlue(ipStart, 0, 0, new LeafPosition(this, -1), true));
361             }
362         }
363     }
364
365     /**
366      * Creates Knuth elements for end border padding and adds them to the return list.
367      * @param returnList return list to add the additional elements to
368      */

369     protected void addKnuthElementsForBorderPaddingEnd(List JavaDoc returnList) {
370         //Border and Padding (after)
371
if (commonBorderPaddingBackground != null) {
372             int ipEnd = commonBorderPaddingBackground.getBorderEndWidth(false)
373                         + commonBorderPaddingBackground.getPaddingEnd(false, this);
374             if (ipEnd > 0) {
375                 // Add a non breakable glue
376
returnList.add(new KnuthPenalty(0, KnuthPenalty.INFINITE,
377                                                 false, new LeafPosition(this, -1), true));
378                 returnList.add(new KnuthGlue(ipEnd, 0, 0, new LeafPosition(this, -1), true));
379             }
380         }
381     }
382
383 }
384
385
Popular Tags