KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > LayoutContext


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: LayoutContext.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr;
21
22 import java.util.Collections JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.fop.fo.Constants;
26 import org.apache.fop.layoutmgr.inline.HyphContext;
27 import org.apache.fop.traits.MinOptMax;
28 import org.apache.fop.layoutmgr.inline.AlignmentContext;
29
30
31 /**
32  * This class is used to pass information to the getNextKnuthElements()
33  * method. It is set up by higher level LM and used by lower level LM.
34  */

35 public class LayoutContext {
36     /**
37      * Values for flags.
38      */

39     public static final int LINEBREAK_AT_LF_ONLY = 0x01;
40     /** Generated break possibility is first in a new area */
41     public static final int NEW_AREA = 0x02;
42     public static final int IPD_UNKNOWN = 0x04;
43     /** Signal to a Line LM that a higher level LM may provoke a change
44      * in the reference area, thus ref area IPD. The LineLM should return
45      * without looking for a line break.
46      */

47     public static final int CHECK_REF_AREA = 0x08;
48
49     /**
50      * If this flag is set, it indicates that any leading fo:character
51      * objects with suppress-at-line-break="suppress" should not generate
52      * areas. This is the case at the beginning of each new LineArea
53      * except the first.
54      */

55     public static final int SUPPRESS_LEADING_SPACE = 0x10;
56     public static final int FIRST_AREA = 0x20;
57     public static final int TRY_HYPHENATE = 0x40;
58     public static final int LAST_AREA = 0x80;
59
60     public static final int RESOLVE_LEADING_SPACE = 0x100;
61
62     /**
63      * This flag indicates that there's a keep-with-next that hasn't
64      * been processed, yet.
65      */

66     public static final int KEEP_WITH_NEXT_PENDING = 0x200;
67     /**
68      * This flag indicates that there's a keep-with-previous that hasn't
69      * been processed, yet.
70      */

71     public static final int KEEP_WITH_PREVIOUS_PENDING = 0x400;
72
73
74     public int flags; // Contains some set of flags defined above
75
/**
76      * Total available stacking dimension for a "galley-level" layout
77      * manager (Line or Flow). It is passed by the parent LM. For LineLM,
78      * the block LM determines this based on indent properties.
79      * These LM <b>may</b> wish to pass this information down to lower
80      * level LM to allow them to optimize returned break possibilities.
81      */

82     MinOptMax stackLimit;
83
84     /** True if current element list is spanning in multi-column layout. */
85     private int nextSpan = Constants.NOT_SET;
86
87     /** inline-progression-dimension of nearest ancestor reference area */
88     int refIPD;
89
90     /** the writing mode established by the nearest ancestor reference area */
91     private int writingMode = Constants.EN_LR_TB;
92
93     /** Current pending space-after or space-end from preceding area */
94     SpaceSpecifier trailingSpace;
95
96     /** Current pending space-before or space-start from ancestor areas */
97     SpaceSpecifier leadingSpace;
98     
99     /**
100      * A list of pending marks (border and padding) on the after edge when a page break occurs.
101      * May be null.
102      */

103     private List JavaDoc pendingAfterMarks;
104     
105     /**
106      * A list of pending marks (border and padding) on the before edge when a page break occurs.
107      * May be null.
108      */

109     private List JavaDoc pendingBeforeMarks;
110
111     /** Current hyphenation context. May be null. */
112     private HyphContext hyphContext = null;
113
114     /** Alignment in BP direction */
115     private int bpAlignment = Constants.EN_START;
116     
117     /** Stretch or shrink value when making areas. */
118     private double ipdAdjust = 0.0;
119
120     /** Stretch or shrink value when adding spaces. */
121     private double dSpaceAdjust = 0.0;
122
123     private AlignmentContext alignmentContext = null;
124     
125     /** Amount of space before / start */
126     private int spaceBefore = 0;
127     
128     /** Amount of space after / end */
129     private int spaceAfter = 0;
130     
131     /** Amount of space to reserve at the beginning of each line */
132     private int lineStartBorderAndPaddingWidth = 0;
133     /** Amount of space to reserve at the end of each line */
134     private int lineEndBorderAndPaddingWidth = 0;
135
136     /**
137      * Copy constructor for creating child layout contexts.
138      * @param parentLC the parent layout context to copy from
139      */

140     public LayoutContext(LayoutContext parentLC) {
141         this.flags = parentLC.flags;
142         this.refIPD = parentLC.refIPD;
143         this.writingMode = parentLC.writingMode;
144         this.stackLimit = null; // Don't reference parent MinOptMax!
145
this.leadingSpace = parentLC.leadingSpace; //???
146
this.trailingSpace = parentLC.trailingSpace; //???
147
this.hyphContext = parentLC.hyphContext;
148         this.bpAlignment = parentLC.bpAlignment;
149         this.dSpaceAdjust = parentLC.dSpaceAdjust;
150         this.ipdAdjust = parentLC.ipdAdjust;
151         this.alignmentContext = parentLC.alignmentContext;
152         this.lineStartBorderAndPaddingWidth = parentLC.lineStartBorderAndPaddingWidth;
153         this.lineEndBorderAndPaddingWidth = parentLC.lineEndBorderAndPaddingWidth;
154         copyPendingMarksFrom(parentLC);
155         // Copy other fields as necessary.
156
}
157
158     /**
159      * Main constructor.
160      * @param flags the initial flags
161      */

162     public LayoutContext(int flags) {
163         this.flags = flags;
164         this.refIPD = 0;
165         stackLimit = new MinOptMax(0);
166         leadingSpace = null;
167         trailingSpace = null;
168     }
169
170     public void copyPendingMarksFrom(LayoutContext source) {
171         if (source.pendingAfterMarks != null) {
172             this.pendingAfterMarks = new java.util.ArrayList JavaDoc(source.pendingAfterMarks);
173         }
174         if (source.pendingBeforeMarks != null) {
175             this.pendingBeforeMarks = new java.util.ArrayList JavaDoc(source.pendingBeforeMarks);
176         }
177     }
178     
179     public void setFlags(int flags) {
180         setFlags(flags, true);
181     }
182
183     public void setFlags(int flags, boolean bSet) {
184         if (bSet) {
185             this.flags |= flags;
186         } else {
187             this.flags &= ~flags;
188         }
189     }
190
191     public void unsetFlags(int flags) {
192         setFlags(flags, false);
193     }
194
195     public boolean isStart() {
196         return ((this.flags & NEW_AREA) != 0);
197     }
198
199     public boolean startsNewArea() {
200         return ((this.flags & NEW_AREA) != 0 && leadingSpace != null);
201     }
202
203     public boolean isFirstArea() {
204         return ((this.flags & FIRST_AREA) != 0);
205     }
206
207     public boolean isLastArea() {
208         return ((this.flags & LAST_AREA) != 0);
209     }
210
211     public boolean suppressLeadingSpace() {
212         return ((this.flags & SUPPRESS_LEADING_SPACE) != 0);
213     }
214
215     public boolean isKeepWithNextPending() {
216         return ((this.flags & KEEP_WITH_NEXT_PENDING) != 0);
217     }
218     
219     public boolean isKeepWithPreviousPending() {
220         return ((this.flags & KEEP_WITH_PREVIOUS_PENDING) != 0);
221     }
222     
223     public void setLeadingSpace(SpaceSpecifier space) {
224         leadingSpace = space;
225     }
226
227     public SpaceSpecifier getLeadingSpace() {
228         return leadingSpace;
229     }
230
231     public boolean resolveLeadingSpace() {
232         return ((this.flags & RESOLVE_LEADING_SPACE) != 0);
233     }
234
235     public void setTrailingSpace(SpaceSpecifier space) {
236         trailingSpace = space;
237     }
238
239     public SpaceSpecifier getTrailingSpace() {
240         return trailingSpace;
241     }
242
243     /**
244      * Adds a border or padding element to the pending list which will be used to generate
245      * the right element list for break possibilities. Conditionality resolution will be done
246      * elsewhere.
247      * @param element the border, padding or space element
248      */

249     public void addPendingAfterMark(UnresolvedListElementWithLength element) {
250         if (this.pendingAfterMarks == null) {
251             this.pendingAfterMarks = new java.util.ArrayList JavaDoc();
252         }
253         this.pendingAfterMarks.add(element);
254     }
255     
256     /**
257      * @return the pending border and padding elements at the after edge
258      * @see #addPendingAfterMark(UnresolvedListElementWithLength)
259      */

260     public List JavaDoc getPendingAfterMarks() {
261         if (this.pendingAfterMarks != null) {
262             return Collections.unmodifiableList(this.pendingAfterMarks);
263         } else {
264             return null;
265         }
266     }
267     
268     /**
269      * Adds a border or padding element to the pending list which will be used to generate
270      * the right element list for break possibilities. Conditionality resolution will be done
271      * elsewhere.
272      * @param element the border, padding or space element
273      */

274     public void addPendingBeforeMark(UnresolvedListElementWithLength element) {
275         if (this.pendingBeforeMarks == null) {
276             this.pendingBeforeMarks = new java.util.ArrayList JavaDoc();
277         }
278         this.pendingBeforeMarks.add(element);
279     }
280     
281     /**
282      * @return the pending border and padding elements at the before edge
283      * @see #addPendingBeforeMark(UnresolvedListElementWithLength)
284      */

285     public List JavaDoc getPendingBeforeMarks() {
286         if (this.pendingBeforeMarks != null) {
287             return Collections.unmodifiableList(this.pendingBeforeMarks);
288         } else {
289             return null;
290         }
291     }
292     
293     public void setStackLimit(MinOptMax limit) {
294         stackLimit = limit;
295     }
296
297     public MinOptMax getStackLimit() {
298         return stackLimit;
299     }
300
301     public void setRefIPD(int ipd) {
302         refIPD = ipd;
303     }
304
305     public int getRefIPD() {
306         return refIPD;
307     }
308
309     public void setHyphContext(HyphContext hyph) {
310         hyphContext = hyph;
311     }
312
313     public HyphContext getHyphContext() {
314         return hyphContext;
315     }
316
317     public boolean tryHyphenate() {
318         return ((this.flags & TRY_HYPHENATE) != 0);
319     }
320
321     /**
322      * Sets the currently applicable alignment in BP direction.
323      * @param alignment one of EN_START, EN_JUSTIFY etc.
324      */

325     public void setBPAlignment(int alignment) {
326         this.bpAlignment = alignment;
327     }
328     
329     /** @return the currently applicable alignment in BP direction (EN_START, EN_JUSTIFY...) */
330     public int getBPAlignment() {
331         return this.bpAlignment;
332     }
333     
334     public void setSpaceAdjust(double adjust) {
335         dSpaceAdjust = adjust;
336     }
337
338     public double getSpaceAdjust() {
339         return dSpaceAdjust;
340     }
341
342     public void setIPDAdjust(double ipdA) {
343         ipdAdjust = ipdA;
344     }
345
346     public double getIPDAdjust() {
347         return ipdAdjust;
348     }
349
350     public void setAlignmentContext(AlignmentContext alignmentContext) {
351         this.alignmentContext = alignmentContext;
352     }
353     
354     public AlignmentContext getAlignmentContext() {
355         return this.alignmentContext;
356     }
357
358     public void resetAlignmentContext() {
359         if (this.alignmentContext != null) {
360             this.alignmentContext = this.alignmentContext.getParentAlignmentContext();
361         }
362     }
363     
364     /**
365      * Get the width to be reserved for border and padding at the start of the line.
366      * @return the width to be reserved
367      */

368     public int getLineStartBorderAndPaddingWidth() {
369         return lineStartBorderAndPaddingWidth;
370     }
371     
372     /**
373      * Set the width to be reserved for border and padding at the start of the line.
374      * @param lineStartBorderAndPaddingWidth the width to be reserved
375      */

376     public void setLineStartBorderAndPaddingWidth(int lineStartBorderAndPaddingWidth) {
377         this.lineStartBorderAndPaddingWidth = lineStartBorderAndPaddingWidth;
378     }
379     
380     /**
381      * Get the width to be reserved for border and padding at the end of the line.
382      * @return the width to be reserved
383      */

384     public int getLineEndBorderAndPaddingWidth() {
385         return lineEndBorderAndPaddingWidth;
386     }
387     
388     /**
389      * Set the width to be reserved for border and padding at the end of the line.
390      * @param lineEndBorderAndPaddingWidth the width to be reserved
391      */

392     public void setLineEndBorderAndPaddingWidth(int lineEndBorderAndPaddingWidth) {
393         this.lineEndBorderAndPaddingWidth = lineEndBorderAndPaddingWidth;
394     }
395     
396     /**
397      * @return true if the current element list ends early because of a span change
398      * in multi-column layout.
399      */

400     public int getNextSpan() {
401         return nextSpan;
402     }
403     
404     /**
405      * Used to signal the PSLM that the element list ends early because of a span change in
406      * multi-column layout.
407      * @param span the new span value (legal values: NOT_SET, EN_NONE, EN_ALL)
408      */

409     public void signalSpanChange(int span) {
410         if (span == Constants.NOT_SET || span == Constants.EN_NONE || span == Constants.EN_ALL) {
411             this.nextSpan = span;
412         } else {
413             throw new IllegalArgumentException JavaDoc("Illegal value on signalSpanChange() for span: "
414                     + span);
415         }
416     }
417     
418     /**
419      * Get the writing mode of the relevant reference area.
420      * @return the applicable writing mode
421      */

422     public int getWritingMode() {
423         return writingMode;
424     }
425
426     /**
427      * Set the writing mode.
428      * @param writingMode the writing mode
429      */

430     public void setWritingMode(int writingMode) {
431         this.writingMode = writingMode;
432     }
433
434     /**
435      * Get the current amount of space before / start
436      * @return the space before / start amount
437      */

438     public int getSpaceBefore() {
439         return spaceBefore;
440     }
441
442     /**
443      * Set the amount of space before / start
444      * @param spaceBefore the amount of space before / start
445      */

446     public void setSpaceBefore(int spaceBefore) {
447         this.spaceBefore = spaceBefore;
448     }
449
450     /**
451      * Get the current amount of space after / end
452      * @return the space after / end amount
453      */

454     public int getSpaceAfter() {
455         return spaceAfter;
456     }
457
458     /**
459      * Set the amount of space after / end
460      * @param spaceAfter the amount of space after / end
461      */

462     public void setSpaceAfter(int spaceAfter) {
463         this.spaceAfter = spaceAfter;
464     }
465     
466     /** @see java.lang.Object#toString() */
467     public String JavaDoc toString() {
468         return "Layout Context:" +
469         "\nStack Limit: \t" + (getStackLimit() == null ? "null" : getStackLimit().toString()) +
470         "\nTrailing Space: \t" + (getTrailingSpace() == null ? "null" : getTrailingSpace().toString()) +
471         "\nLeading Space: \t" + (getLeadingSpace() == null ? "null" : getLeadingSpace().toString()) +
472         "\nReference IPD: \t" + getRefIPD() +
473         "\nSpace Adjust: \t" + getSpaceAdjust() +
474         "\nIPD Adjust: \t" + getIPDAdjust() +
475         "\nResolve Leading Space: \t" + resolveLeadingSpace() +
476         "\nSuppress Leading Space: \t" + suppressLeadingSpace() +
477         "\nIs First Area: \t" + isFirstArea() +
478         "\nStarts New Area: \t" + startsNewArea() +
479         "\nIs Last Area: \t" + isLastArea() +
480         "\nTry Hyphenate: \t" + tryHyphenate() +
481         "\nKeeps: \t[" + (isKeepWithNextPending() ? "keep-with-next" : "") + "]["
482             + (isKeepWithPreviousPending() ? "keep-with-previous" : "") + "] pending";
483     }
484
485 }
486
487
Popular Tags