KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layout > BlockArea


1 /*
2  * $Id: BlockArea.java,v 1.31.2.5 2003/03/02 13:47:44 pietsch Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.layout;
52
53 // FOP
54
import org.apache.fop.render.Renderer;
55 import org.apache.fop.fo.flow.*;
56
57 // Java
58
import java.util.ArrayList JavaDoc;
59
60 /**
61  * This class represents a Block Area.
62  * A block area is made up of a sequence of Line Areas.
63  *
64  * This class is used to organise the sequence of line areas as
65  * inline areas are added to this block it creates and ands line areas
66  * to hold the inline areas.
67  * This uses the line-height and line-stacking-strategy to work
68  * out how to stack the lines.
69  */

70 public class BlockArea extends Area {
71
72     /* relative to area container */
73     protected int startIndent;
74     protected int endIndent;
75
76     /* first line startIndent modifier */
77     protected int textIndent;
78
79     protected int lineHeight;
80
81     protected int halfLeading;
82
83
84     /* text-align of all but the last line */
85     protected int align;
86
87     /* text-align of the last line */
88     protected int alignLastLine;
89
90     protected LineArea currentLineArea;
91     protected LinkSet currentLinkSet;
92
93     /* hyphenation */
94     protected HyphenationProps hyphProps;
95
96     protected ArrayList JavaDoc pendingFootnotes = null;
97
98     public BlockArea(FontState fontState, int allocationWidth, int maxHeight,
99                      int startIndent, int endIndent, int textIndent,
100                      int align, int alignLastLine, int lineHeight) {
101         super(fontState, allocationWidth, maxHeight);
102
103         this.startIndent = startIndent;
104         this.endIndent = endIndent;
105         this.textIndent = textIndent;
106         this.contentRectangleWidth = allocationWidth - startIndent
107                                      - endIndent;
108         this.align = align;
109         this.alignLastLine = alignLastLine;
110         this.lineHeight = lineHeight;
111
112         if (fontState != null)
113             this.halfLeading = (lineHeight - fontState.getFontSize()) / 2;
114     }
115
116     public void render(Renderer renderer) {
117         renderer.renderBlockArea(this);
118     }
119
120     /**
121      * Add a Line Area to this block area.
122      * Used internally to add a completed line area to this block area
123      * when either a new line area is created or this block area is
124      * completed.
125      *
126      * @param la the LineArea to add
127      */

128     protected void addLineArea(LineArea la) {
129         if (!la.isEmpty()) {
130             la.verticalAlign();
131             this.addDisplaySpace(this.halfLeading);
132             int size = la.getHeight();
133             this.addChild(la);
134             this.increaseHeight(size);
135             this.addDisplaySpace(this.halfLeading);
136         }
137         // add pending footnotes
138
if (pendingFootnotes != null) {
139             for (int i = 0; i< pendingFootnotes.size(); i++) {
140                 FootnoteBody fb = (FootnoteBody)pendingFootnotes.get(i);
141                 Page page = getPage();
142                 if (!Footnote.layoutFootnote(page, fb, this)) {
143                     page.addPendingFootnote(fb);
144                 }
145             }
146             pendingFootnotes = null;
147         }
148     }
149
150     /**
151      * Get the current line area in this block area.
152      * This is used to get the current line area for adding
153      * inline objects.
154      * This will return null if there is not enough room left
155      * in the block area to accomodate the line area.
156      *
157      * @return the line area to be used to add inline objects
158      */

159     public LineArea getCurrentLineArea() {
160         if (currentHeight + lineHeight > maxHeight) {
161             return null;
162         }
163         if (this.currentLineArea==null ) {
164             this.currentLineArea = new LineArea(fontState, lineHeight,
165                                                 halfLeading, allocationWidth,
166                                                 startIndent + textIndent,
167                                                 endIndent, null);
168             this.currentLineArea.changeHyphenation(hyphProps);
169         }
170         return this.currentLineArea;
171     }
172
173     /**
174      * Create a new line area to add inline objects.
175      * This should be called after getting the current line area
176      * and discovering that the inline object will not fit inside the current
177      * line. This method will create a new line area to place the inline
178      * object into.
179      * This will return null if the new line cannot fit into the block area.
180      *
181      * @return the new current line area, which will be empty.
182      */

183     public LineArea createNextLineArea() {
184         if (this.currentLineArea!=null) {
185             this.currentLineArea.align(this.align);
186             this.addLineArea(this.currentLineArea);
187         }
188         if (currentHeight + lineHeight > maxHeight) {
189             return null;
190         }
191         this.currentLineArea = new LineArea(fontState, lineHeight,
192                                             halfLeading, allocationWidth,
193                                             startIndent, endIndent,
194                                             currentLineArea);
195         this.currentLineArea.changeHyphenation(hyphProps);
196         return this.currentLineArea;
197     }
198
199     public void setupLinkSet(LinkSet ls) {
200         if (ls != null) {
201             this.currentLinkSet = ls;
202             ls.setYOffset(currentHeight);
203         }
204     }
205
206     /**
207      * Notify this block that the area has completed layout.
208      * Indicates the the block has been fully laid out, this will
209      * add (if any) the current line area.
210      */

211     public void end() {
212         if (this.currentLineArea!=null) {
213             this.currentLineArea.addPending();
214             this.currentLineArea.align(this.alignLastLine);
215             this.addLineArea(this.currentLineArea);
216             this.currentLineArea = null;
217         }
218     }
219
220     public void start() {
221     }
222
223     public int getEndIndent() {
224         return endIndent;
225     }
226
227     // KL: I think we should just return startIndent here!
228
public int getStartIndent() {
229         // return startIndent + paddingLeft + borderWidthLeft;
230
return startIndent;
231     }
232
233     public void setIndents(int startIndent, int endIndent) {
234         this.startIndent = startIndent;
235         this.endIndent = endIndent;
236         this.contentRectangleWidth = allocationWidth - startIndent
237                                      - endIndent;
238     }
239
240     /**
241      * Return the maximum space remaining for this area's content in
242      * the block-progression-dimension.
243      * Remove top and bottom padding and spacing since these reduce
244      * available space for content and they are not yet accounted for
245      * in the positioning of the object.
246      */

247     public int spaceLeft() {
248         // return maxHeight - currentHeight ;
249
return maxHeight - currentHeight
250                 - (getPaddingTop() + getPaddingBottom()
251                 + getBorderTopWidth() + getBorderBottomWidth());
252     }
253
254     public int getHalfLeading() {
255         return halfLeading;
256     }
257
258     public void setHyphenation(HyphenationProps hyphProps) {
259         this.hyphProps = hyphProps;
260     }
261
262     public void addFootnote(FootnoteBody fb) {
263         if (pendingFootnotes == null) {
264             pendingFootnotes = new ArrayList JavaDoc();
265         }
266         pendingFootnotes.add(fb);
267     }
268
269 }
270
Popular Tags