KickJava   Java API By Example, From Geeks To Geeks.

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


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: KnuthPenalty.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr;
21
22 import org.apache.fop.fo.Constants;
23
24 /**
25  * An instance of this class represents information about a feasible
26  * breaking point; it does not represent any piece of content.
27  *
28  * A KnuthPenalty is a feasible breaking point unless its value is infinity;
29  * a KnuthPenalty whose value is -infinity represents a forced break.
30  *
31  * A KnuthPenalty is suppressed, and its width is ignored, if it is not a
32  * chosen breaking point; for example, a KnuthPenalty representing a
33  * hyphenation point has a width (the "-" width), which must be ignored if
34  * that point is not chosen as a breaking point.
35  *
36  * Besides the inherited methods and attributes, this class has two more
37  * attributes and the methods used to get them: the penalty value, which is
38  * a kind of "aesthetic cost" (the higher the value, the more unsightly the
39  * breaking point), and a boolean that marks KnuthPenalties which should not
40  * be chosen as breaking points for consecutive lines.
41  */

42 public class KnuthPenalty extends KnuthElement {
43
44     /** Used for flagged penalties. See Knuth algorithm. */
45     public static final int FLAGGED_PENALTY = 50;
46
47     private int penalty;
48     private boolean bFlagged;
49     private int breakClass = -1;
50
51     /**
52      * Create a new KnuthPenalty.
53      *
54      * @param w the width of this penalty
55      * @param p the penalty value of this penalty
56      * @param f is this penalty flagged?
57      * @param pos the Position stored in this penalty
58      * @param bAux is this penalty auxiliary?
59      */

60     public KnuthPenalty(int w, int p, boolean f, Position pos, boolean bAux) {
61         super(w, pos, bAux);
62         penalty = p;
63         bFlagged = f;
64     }
65
66     /**
67      * Create a new KnuthPenalty.
68      *
69      * @param w the width of this penalty
70      * @param p the penalty value of this penalty
71      * @param f is this penalty flagged?
72      * @param iBreakClass the break class of this penalty (one of the break-* constants)
73      * @param pos the Position stored in this penalty
74      * @param bAux is this penalty auxiliary?
75      */

76     public KnuthPenalty(int w, int p, boolean f,
77             int iBreakClass, Position pos, boolean bAux) {
78         super(w, pos, bAux);
79         penalty = p;
80         bFlagged = f;
81         breakClass = iBreakClass;
82     }
83
84     /** @see org.apache.fop.layoutmgr.KnuthElement#isPenalty() */
85     public boolean isPenalty() {
86         return true;
87     }
88
89     /**
90      * @return the penalty value of this penalty.
91      */

92     public int getP() {
93         return penalty;
94     }
95
96     /**
97      * Sets a new penalty value.
98      * @param p the new penalty value
99      */

100     public void setP(int p) {
101         this.penalty = p;
102     }
103     
104     /** @return true is this penalty is a flagged one. */
105     public boolean isFlagged() {
106         return bFlagged;
107     }
108
109     /** @see org.apache.fop.layoutmgr.KnuthElement#isForcedBreak() */
110     public boolean isForcedBreak() {
111         return penalty == -KnuthElement.INFINITE;
112     }
113     
114     /** @return the break class of this penalty (one of the break-* constants) */
115     public int getBreakClass() {
116         return breakClass;
117     }
118     
119     /**
120      * Sets the break class for this penalty.
121      * @param cl the break class (one of the break-* constants)
122      */

123     public void setBreakClass(int cl) {
124         this.breakClass = cl;
125     }
126     
127     /** @see java.lang.Object#toString() */
128     public String JavaDoc toString() {
129         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(64);
130         if (isAuxiliary()) {
131             sb.append("aux. ");
132         }
133         sb.append("penalty");
134         sb.append(" p=");
135         if (getP() < 0) {
136             sb.append("-");
137         }
138         if (Math.abs(getP()) == INFINITE) {
139             sb.append("INFINITE");
140         } else {
141             sb.append(getP());
142         }
143         if (isFlagged()) {
144             sb.append(" [flagged]");
145         }
146         sb.append(" w=");
147         sb.append(getW());
148         if (isForcedBreak()) {
149             sb.append(" (forced break");
150             switch (getBreakClass()) {
151             case Constants.EN_PAGE:
152                 sb.append(", page");
153                 break;
154             case Constants.EN_COLUMN:
155                 sb.append(", column");
156                 break;
157             case Constants.EN_EVEN_PAGE:
158                 sb.append(", even page");
159                 break;
160             case Constants.EN_ODD_PAGE:
161                 sb.append(", odd page");
162                 break;
163             default:
164             }
165             sb.append(")");
166         }
167         return sb.toString();
168     }
169     
170 }
171
Popular Tags