KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > flow > Leader


1 /*
2  * $Id: Leader.java,v 1.13.2.7 2003/04/11 00:24:38 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.fo.flow;
52
53 // FOP
54
import org.apache.fop.fo.*;
55 import org.apache.fop.datatypes.*;
56 import org.apache.fop.layout.*;
57 import org.apache.fop.layout.BlockArea;
58 import org.apache.fop.layout.LineArea;
59 import org.apache.fop.layout.FontState;
60 import org.apache.fop.apps.FOPException;
61
62 /**
63  * Implements fo:leader; main property of leader leader-pattern.
64  * The following patterns are treated: rule, space, dots.
65  * The pattern use-content is ignored, i.e. it still must be implemented.
66  */

67
68 public class Leader extends FObjMixed {
69
70     public static class Maker extends FObj.Maker {
71         public FObj make(FObj parent, PropertyList propertyList,
72                          String JavaDoc systemId, int line, int column)
73             throws FOPException {
74             return new Leader(parent, propertyList, systemId, line, column);
75         }
76
77     }
78
79     public static FObj.Maker maker() {
80         return new Leader.Maker();
81     }
82
83     public Leader(FObj parent, PropertyList propertyList,
84                   String JavaDoc systemId, int line, int column)
85         throws FOPException {
86         super(parent, propertyList, systemId, line, column);
87     }
88
89     public String JavaDoc getName() {
90         return "fo:leader";
91     }
92
93     public int layout(Area area) throws FOPException {
94         BlockArea blockArea;
95         // restriction in this version
96
if (!(area instanceof BlockArea)) {
97             log.warn("in this version of Fop fo:leader must be a direct child of fo:block ");
98             return Status.OK;
99         } else {
100             blockArea = (BlockArea)area;
101         }
102
103         // Common Accessibility Properties
104
AccessibilityProps mAccProps = propMgr.getAccessibilityProps();
105
106         // Common Aural Properties
107
AuralProps mAurProps = propMgr.getAuralProps();
108
109         // Common Border, Padding, and Background Properties
110
BorderAndPadding bap = propMgr.getBorderAndPadding();
111         BackgroundProps bProps = propMgr.getBackgroundProps();
112
113         // Common Font Properties
114
//this.fontState = propMgr.getFontState(area.getFontInfo());
115

116         // Common Margin Properties-Inline
117
MarginInlineProps mProps = propMgr.getMarginInlineProps();
118
119         // Common Relative Position Properties
120
RelativePositionProps mRelProps = propMgr.getRelativePositionProps();
121
122         // this.properties.get("alignment-adjust");
123
// this.properties.get("alignment-baseline");
124
// this.properties.get("baseline-shift");
125
// this.properties.get("color");
126
// this.properties.get("dominant-baseline");
127
// this.properties.get("text-depth");
128
// this.properties.get("text-altitude");
129
// this.properties.get("id");
130
// this.properties.get("leader-alignment");
131
// this.properties.get("leader-length");
132
// this.properties.get("leader-pattern");
133
// this.properties.get("leader-pattern-width");
134
// this.properties.get("rule-style");
135
// this.properties.get("rule-thickness");
136
// this.properties.get("letter-spacing");
137
// this.properties.get("line-height");
138
// this.properties.get("line-height-shift-adjustment");
139
// this.properties.get("text-shadow");
140
// this.properties.get("visibility");
141
// this.properties.get("word-spacing");
142
// this.properties.get("z-index");
143

144         // color properties
145
ColorType c = this.properties.get("color").getColorType();
146         float red = c.red();
147         float green = c.green();
148         float blue = c.blue();
149
150         // fo:leader specific properties
151
// determines the pattern of leader; allowed values: space, rule,dots, use-content
152
int leaderPattern = this.properties.get("leader-pattern").getEnum();
153         // length of the leader
154
Length length = this.properties.get("leader-length.minimum").getLength();
155         int leaderLengthMinimum;
156         if (length instanceof PercentLength) {
157             leaderLengthMinimum = (int)(((PercentLength)length).value()
158                                         * area.getAllocationWidth());
159         } else {
160             leaderLengthMinimum = length.mvalue();
161         }
162         length = this.properties.get("leader-length.optimum").getLength();
163         int leaderLengthOptimum;
164         if (length instanceof PercentLength) {
165             leaderLengthOptimum = (int)(((PercentLength)length).value()
166                                       * area.getAllocationWidth());
167         } else {
168             leaderLengthOptimum = length.mvalue();
169         }
170         length = this.properties.get("leader-length.maximum").getLength();
171         int leaderLengthMaximum;
172         if (length instanceof PercentLength) {
173             leaderLengthMaximum = (int)(((PercentLength)length).value()
174                                       * area.getAllocationWidth());
175         } else {
176             leaderLengthMaximum = length.mvalue();
177         }
178         // the following properties only apply for leader-pattern = "rule"
179
int ruleThickness =
180             this.properties.get("rule-thickness").getLength().mvalue();
181         int ruleStyle = this.properties.get("rule-style").getEnum();
182         // if leaderPatternWidth = 0 = default = use-font-metric
183
int leaderPatternWidth =
184             this.properties.get("leader-pattern-width").getLength().mvalue();
185         int leaderAlignment =
186             this.properties.get("leader-alignment").getEnum();
187
188         // initialize id
189
String JavaDoc id = this.properties.get("id").getString();
190         try {
191             blockArea.getIDReferences().initializeID(id, blockArea);
192         }
193         catch(FOPException e) {
194             if (!e.isLocationSet()) {
195                 e.setLocation(systemId, line, column);
196             }
197             throw e;
198         }
199         
200         // adds leader to blockarea, there the leaderArea is generated
201
int succeeded = addLeader(blockArea,
202                                   propMgr.getFontState(area.getFontInfo()),
203                                   red, green, blue, leaderPattern,
204                                   leaderLengthMinimum, leaderLengthOptimum,
205                                   leaderLengthMaximum, ruleThickness,
206                                   ruleStyle, leaderPatternWidth,
207                                   leaderAlignment);
208         if (succeeded == 1) {
209             return Status.OK;
210         } else {
211             // not sure that this is the correct Status here
212
return Status.AREA_FULL_SOME;
213         }
214     }
215
216     /**
217      * adds a leader to current line area of containing block area
218      * the actual leader area is created in the line area
219      *
220      * @return int +1 for success and -1 for none
221      */

222     public int addLeader(BlockArea ba, FontState fontState, float red,
223                          float green, float blue, int leaderPattern,
224                          int leaderLengthMinimum, int leaderLengthOptimum,
225                          int leaderLengthMaximum, int ruleThickness,
226                          int ruleStyle, int leaderPatternWidth,
227                          int leaderAlignment) {
228
229         LineArea la = ba.getCurrentLineArea();
230         // this should start a new page
231
if (la == null) {
232             return -1;
233         }
234
235         la.changeFont(fontState);
236         la.changeColor(red, green, blue);
237
238         // check whether leader fits into the (rest of the) line
239
// using length.optimum to determine where to break the line as defined
240
// in the xsl:fo spec: "User agents may choose to use the value of 'leader-length.optimum'
241
// to determine where to break the line" (7.20.4)
242
// if leader is longer then create a new LineArea and put leader there
243
if (leaderLengthOptimum <= la.getRemainingWidth()) {
244             la.addLeader(leaderPattern, leaderLengthMinimum,
245                          leaderLengthOptimum, leaderLengthMaximum, ruleStyle,
246                          ruleThickness, leaderPatternWidth, leaderAlignment);
247         } else {
248             la = ba.createNextLineArea();
249             if (la == null) {
250                 // not enough room
251
return -1;
252             }
253             la.changeFont(fontState);
254             la.changeColor(red, green, blue);
255
256             // check whether leader fits into LineArea at all, otherwise
257
// clip it (should honor the clip option of containing area)
258
if (leaderLengthMinimum <= la.getContentWidth()) {
259                 la.addLeader(leaderPattern, leaderLengthMinimum,
260                              leaderLengthOptimum, leaderLengthMaximum,
261                              ruleStyle, ruleThickness, leaderPatternWidth,
262                              leaderAlignment);
263             } else {
264                 log.error("Leader doesn't fit into line, it will be clipped to fit.");
265                 la.addLeader(leaderPattern, la.getRemainingWidth(),
266                              la.getRemainingWidth(), la.getRemainingWidth(),
267                              ruleStyle, ruleThickness, leaderPatternWidth,
268                              leaderAlignment);
269             }
270         }
271         return 1;
272     }
273
274 }
275
Popular Tags