KickJava   Java API By Example, From Geeks To Geeks.

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


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: ContentLayoutManager.java 453310 2006-10-05 18:44:15Z spepping $ */
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.apps.FOUserAgent;
25 import org.apache.fop.fo.Constants;
26 import org.apache.fop.fo.pagination.Title;
27 import org.apache.fop.layoutmgr.AbstractBaseLayoutManager;
28 import org.apache.fop.layoutmgr.KnuthElement;
29 import org.apache.fop.layoutmgr.KnuthPossPosIter;
30 import org.apache.fop.layoutmgr.KnuthSequence;
31 import org.apache.fop.layoutmgr.LayoutContext;
32 import org.apache.fop.layoutmgr.LayoutManager;
33 import org.apache.fop.layoutmgr.PageSequenceLayoutManager;
34 import org.apache.fop.layoutmgr.Position;
35 import org.apache.fop.layoutmgr.PositionIterator;
36 import org.apache.fop.layoutmgr.SpaceSpecifier;
37 import org.apache.fop.area.Area;
38 import org.apache.fop.area.LineArea;
39 import org.apache.fop.area.inline.InlineArea;
40
41 import java.util.LinkedList JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.ListIterator JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import org.apache.fop.traits.MinOptMax;
47
48 import org.apache.fop.area.Block;
49
50 /**
51  * Content Layout Manager.
52  * For use with objects that contain inline areas such as
53  * leader use-content and title.
54  */

55 public class ContentLayoutManager extends AbstractBaseLayoutManager
56         implements InlineLevelLayoutManager {
57
58     /**
59      * logging instance
60      */

61     private static Log log = LogFactory.getLog(ContentLayoutManager.class);
62
63     private FOUserAgent userAgent;
64     private Area holder;
65     private int stackSize;
66     private LayoutManager parentLM;
67     private InlineLevelLayoutManager childLM = null;
68
69     /**
70      * Constructs a new ContentLayoutManager
71      *
72      * @param area The parent area
73      */

74     public ContentLayoutManager(Area area, LayoutManager parentLM) {
75         holder = area;
76         this.parentLM = parentLM;
77     }
78
79     /**
80      * Constructor using a fo:title formatting object and its PageSequenceLayoutManager parent.
81      * throws IllegalStateException if the foTitle has no children.
82      * TODO: convert IllegalStateException to FOPException;
83      * also in makeLayoutManager and makeContentLayoutManager and callers.
84      * @param pslm the PageSequenceLayoutManager parent of this LM
85      * @param foTitle the Title FO for which this LM is made
86      */

87     public ContentLayoutManager(PageSequenceLayoutManager pslm, Title foTitle) {
88         // get breaks then add areas to title
89
this.parentLM = pslm;
90         holder = new LineArea();
91
92         // setUserAgent(foTitle.getUserAgent());
93

94         // use special layout manager to add the inline areas
95
// to the Title.
96
try {
97             LayoutManager lm = pslm.getLayoutManagerMaker().makeLayoutManager(foTitle);
98             addChildLM(lm);
99             fillArea(lm);
100         } catch (IllegalStateException JavaDoc e) {
101             log.warn("Title has no content");
102             throw e;
103         }
104     }
105
106     public void initialize() {
107         // Empty
108
}
109
110     public void fillArea(LayoutManager curLM) {
111
112         int ipd = 1000000;
113
114         LayoutContext childLC = new LayoutContext(LayoutContext.NEW_AREA);
115         childLC.setLeadingSpace(new SpaceSpecifier(false));
116         childLC.setTrailingSpace(new SpaceSpecifier(false));
117         // set stackLimit for lines
118
childLC.setStackLimit(new MinOptMax(ipd));
119         childLC.setRefIPD(ipd);
120
121         int lineHeight = 14000;
122         int lead = 12000;
123         int follow = 2000;
124
125         int halfLeading = (lineHeight - lead - follow) / 2;
126         // height before baseline
127
int lineLead = lead + halfLeading;
128         // maximum size of top and bottom alignment
129
int maxtb = follow + halfLeading;
130         // max size of middle alignment below baseline
131
int middlefollow = maxtb;
132
133         stackSize = 0;
134
135         LinkedList JavaDoc contentList =
136             getNextKnuthElements(childLC, Constants.EN_START);
137         ListIterator JavaDoc contentIter = contentList.listIterator();
138         while (contentIter.hasNext()) {
139             KnuthElement element = (KnuthElement) contentIter.next();
140             if (element instanceof KnuthInlineBox) {
141                 KnuthInlineBox box = (KnuthInlineBox) element;
142                 // TODO handle alignment here?
143
}
144         }
145
146         if (maxtb - lineLead > middlefollow) {
147             middlefollow = maxtb - lineLead;
148         }
149
150         LayoutContext lc = new LayoutContext(0);
151
152         lc.setFlags(LayoutContext.RESOLVE_LEADING_SPACE, true);
153         lc.setLeadingSpace(new SpaceSpecifier(false));
154         lc.setTrailingSpace(new SpaceSpecifier(false));
155         KnuthPossPosIter contentPosIter =
156             new KnuthPossPosIter(contentList, 0, contentList.size());
157         curLM.addAreas(contentPosIter, lc);
158     }
159
160     public void addAreas(PositionIterator posIter, LayoutContext context) {
161         // add the content areas
162
// the area width has already been adjusted, and it must remain unchanged
163
// so save its value before calling addAreas, and set it again afterwards
164
int savedIPD = ((InlineArea)holder).getIPD();
165         // set to zero the ipd adjustment ratio, to avoid spaces in the pattern
166
// to be modified
167
LayoutContext childContext = new LayoutContext(context);
168         childContext.setIPDAdjust(0.0);
169         childLM.addAreas(posIter, childContext);
170         ((InlineArea)holder).setIPD(savedIPD);
171     }
172
173     public int getStackingSize() {
174         return stackSize;
175     }
176
177     /** @see org.apache.fop.layoutmgr.LayoutManager */
178     public Area getParentArea(Area childArea) {
179         return holder;
180     }
181
182     /**
183      * @see org.apache.fop.layoutmgr.LayoutManager#addChildArea(Area)
184      **/

185     public void addChildArea(Area childArea) {
186         holder.addChildArea(childArea);
187     }
188
189     /**
190      * @see org.apache.fop.layoutmgr.LayoutManager#setParent(LayoutManager)
191      */

192     public void setParent(LayoutManager lm) {
193         parentLM = lm;
194     }
195
196     /** @see org.apache.fop.layoutmgr.LayoutManager#getParent() */
197     public LayoutManager getParent() {
198         return this.parentLM;
199     }
200
201     /**
202      * @see org.apache.fop.layoutmgr.LayoutManager#isFinished()
203      */

204     public boolean isFinished() {
205         return false;
206     }
207
208     /**
209      * @see org.apache.fop.layoutmgr.LayoutManager#setFinished(boolean)
210      */

211     public void setFinished(boolean isFinished) {
212         //to be done
213
}
214
215     /**
216      * @see org.apache.fop.layoutmgr.LayoutManager#resetPosition(Position)
217      */

218     public void resetPosition(Position position) {
219         //to be done
220
}
221
222     /**
223      * @see org.apache.fop.layoutmgr.LayoutManager#createNextChildLMs(int)
224      */

225     public boolean createNextChildLMs(int pos) {
226         return false;
227     }
228
229     /**
230      * @see org.apache.fop.layoutmgr.LayoutManager#getChildLMs()
231      */

232     public List JavaDoc getChildLMs() {
233         List JavaDoc childLMs = new ArrayList JavaDoc(1);
234         childLMs.add(childLM);
235         return childLMs;
236     }
237
238     /**
239      * @see org.apache.fop.layoutmgr.LayoutManager#addChildLM
240      */

241     public void addChildLM(LayoutManager lm) {
242         if (lm == null) {
243             return;
244         }
245         lm.setParent(this);
246         childLM = (InlineLevelLayoutManager)lm;
247         log.trace(this.getClass().getName()
248                   + ": Adding child LM " + lm.getClass().getName());
249     }
250
251     /**
252      * @see org.apache.fop.layoutmgr.LayoutManager#addChildLMs
253      */

254     public void addChildLMs(List JavaDoc newLMs) {
255         if (newLMs == null || newLMs.size() == 0) {
256             return;
257         }
258         ListIterator JavaDoc iter = newLMs.listIterator();
259         while (iter.hasNext()) {
260             LayoutManager lm = (LayoutManager) iter.next();
261             addChildLM(lm);
262         }
263     }
264
265     public LinkedList JavaDoc getNextKnuthElements(LayoutContext context, int alignment) {
266         LinkedList JavaDoc contentList = new LinkedList JavaDoc();
267         LinkedList JavaDoc returnedList;
268
269         childLM.initialize();
270         while (!childLM.isFinished()) {
271             // get KnuthElements from childLM
272
returnedList = childLM.getNextKnuthElements(context, alignment);
273
274             if (returnedList != null) {
275                 // move elements to contentList, and accumulate their size
276
KnuthElement contentElement;
277                while (returnedList.size() > 0) {
278                     Object JavaDoc obj = returnedList.removeFirst();
279                     if (obj instanceof KnuthSequence) {
280                         KnuthSequence ks = (KnuthSequence)obj;
281                         for (Iterator it = ks.iterator(); it.hasNext(); ) {
282                             contentElement = (KnuthElement)it.next();
283                             stackSize += contentElement.getW();
284                             contentList.add(contentElement);
285                         }
286                     } else {
287                         contentElement = (KnuthElement)obj;
288                         stackSize += contentElement.getW();
289                         contentList.add(contentElement);
290                     }
291                 }
292             }
293         }
294
295         setFinished(true);
296         return contentList;
297     }
298
299     public List JavaDoc addALetterSpaceTo(List JavaDoc oldList) {
300         return oldList;
301     }
302
303     /**
304      * Remove the word space represented by the given elements
305      *
306      * @param oldList the elements representing the word space
307      */

308     public void removeWordSpace(List JavaDoc oldList) {
309         // do nothing
310
log.warn(this.getClass().getName() + " should not receive a call to removeWordSpace(list)");
311     }
312
313     public void getWordChars(StringBuffer JavaDoc sbChars, Position pos) {
314     }
315
316     public void hyphenate(Position pos, HyphContext hc) {
317     }
318
319     public boolean applyChanges(List JavaDoc oldList) {
320         return false;
321     }
322
323     public LinkedList JavaDoc getChangedKnuthElements(List JavaDoc oldList,
324                                               /*int flaggedPenalty,*/
325                                               int alignment) {
326         return null;
327     }
328     
329     public PageSequenceLayoutManager getPSLM() {
330         return parentLM.getPSLM();
331     }
332
333     // --------- Property Resolution related functions --------- //
334

335     /**
336      * Returns the IPD of the content area
337      * @return the IPD of the content area
338      */

339     public int getContentAreaIPD() {
340         return holder.getIPD();
341     }
342    
343     /**
344      * Returns the BPD of the content area
345      * @return the BPD of the content area
346      */

347     public int getContentAreaBPD() {
348         return holder.getBPD();
349     }
350     
351     /**
352      * @see org.apache.fop.layoutmgr.LayoutManager#getGeneratesReferenceArea
353      */

354     public boolean getGeneratesReferenceArea() {
355         return false;
356     }
357
358     /**
359      * @see org.apache.fop.layoutmgr.LayoutManager#getGeneratesBlockArea
360      */

361     public boolean getGeneratesBlockArea() {
362         return getGeneratesLineArea() || holder instanceof Block;
363     }
364    
365     /**
366      * @see org.apache.fop.layoutmgr.LayoutManager#getGeneratesLineArea
367      */

368     public boolean getGeneratesLineArea() {
369         return holder instanceof LineArea;
370     }
371
372     /* (non-Javadoc)
373      * @see org.apache.fop.layoutmgr.LayoutManager#notifyPos(org.apache.fop.layoutmgr.Position)
374      */

375     public Position notifyPos(Position pos) {
376         return pos;
377     }
378    
379 }
380
381
Popular Tags