001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: KnuthPenalty.java 553790 2007-07-06 08:41:46Z vhennebert $ */
019:
020: package org.apache.fop.layoutmgr;
021:
022: import org.apache.fop.fo.Constants;
023:
024: /**
025: * An instance of this class represents information about a feasible
026: * breaking point; it does not represent any piece of content.
027: *
028: * A KnuthPenalty is a feasible breaking point unless its value is infinity;
029: * a KnuthPenalty whose value is -infinity represents a forced break.
030: *
031: * A KnuthPenalty is suppressed, and its width is ignored, if it is not a
032: * chosen breaking point; for example, a KnuthPenalty representing a
033: * hyphenation point has a width (the "-" width), which must be ignored if
034: * that point is not chosen as a breaking point.
035: *
036: * Besides the inherited methods and attributes, this class has two more
037: * attributes and the methods used to get them: the penalty value, which is
038: * a kind of "aesthetic cost" (the higher the value, the more unsightly the
039: * breaking point), and a boolean that marks KnuthPenalties which should not
040: * be chosen as breaking points for consecutive lines.
041: */
042: public class KnuthPenalty extends KnuthElement {
043:
044: /** Used for flagged penalties. See Knuth algorithm. */
045: public static final int FLAGGED_PENALTY = 50;
046:
047: private int penalty;
048: private boolean bFlagged;
049: private int breakClass = -1;
050:
051: /**
052: * Create a new KnuthPenalty.
053: *
054: * @param w the width of this penalty
055: * @param p the penalty value of this penalty
056: * @param f is this penalty flagged?
057: * @param pos the Position stored in this penalty
058: * @param bAux is this penalty auxiliary?
059: */
060: public KnuthPenalty(int w, int p, boolean f, Position pos,
061: boolean bAux) {
062: super (w, pos, bAux);
063: penalty = p;
064: bFlagged = f;
065: }
066:
067: /**
068: * Create a new KnuthPenalty.
069: *
070: * @param w the width of this penalty
071: * @param p the penalty value of this penalty
072: * @param f is this penalty flagged?
073: * @param iBreakClass the break class of this penalty (one of the break-* constants)
074: * @param pos the Position stored in this penalty
075: * @param bAux is this penalty auxiliary?
076: */
077: public KnuthPenalty(int w, int p, boolean f, int iBreakClass,
078: Position pos, boolean bAux) {
079: super (w, pos, bAux);
080: penalty = p;
081: bFlagged = f;
082: breakClass = iBreakClass;
083: }
084:
085: /** @see org.apache.fop.layoutmgr.KnuthElement#isPenalty() */
086: public boolean isPenalty() {
087: return true;
088: }
089:
090: /**
091: * @return the penalty value of this penalty.
092: */
093: public int getP() {
094: return penalty;
095: }
096:
097: /**
098: * Sets a new penalty value.
099: * @param p the new penalty value
100: */
101: public void setP(int p) {
102: this .penalty = p;
103: }
104:
105: /** @return true is this penalty is a flagged one. */
106: public boolean isFlagged() {
107: return bFlagged;
108: }
109:
110: /** @see org.apache.fop.layoutmgr.KnuthElement#isForcedBreak() */
111: public boolean isForcedBreak() {
112: return penalty == -KnuthElement.INFINITE;
113: }
114:
115: /**
116: * @return the break class of this penalty (EN_AUTO, EN_COLUMN, EN_PAGE, EN_EVEN_PAGE,
117: * EN_ODD_PAGE)
118: */
119: public int getBreakClass() {
120: return breakClass;
121: }
122:
123: /**
124: * Sets the break class for this penalty.
125: * @param cl the break class (EN_AUTO, EN_COLUMN, EN_PAGE, EN_EVEN_PAGE, EN_ODD_PAGE)
126: */
127: public void setBreakClass(int cl) {
128: this .breakClass = cl;
129: }
130:
131: /** @see java.lang.Object#toString() */
132: public String toString() {
133: StringBuffer sb = new StringBuffer(64);
134: if (isAuxiliary()) {
135: sb.append("aux. ");
136: }
137: sb.append("penalty");
138: sb.append(" p=");
139: if (getP() < 0) {
140: sb.append("-");
141: }
142: if (Math.abs(getP()) == INFINITE) {
143: sb.append("INFINITE");
144: } else {
145: sb.append(getP());
146: }
147: if (isFlagged()) {
148: sb.append(" [flagged]");
149: }
150: sb.append(" w=");
151: sb.append(getW());
152: if (isForcedBreak()) {
153: sb.append(" (forced break");
154: switch (getBreakClass()) {
155: case Constants.EN_PAGE:
156: sb.append(", page");
157: break;
158: case Constants.EN_COLUMN:
159: sb.append(", column");
160: break;
161: case Constants.EN_EVEN_PAGE:
162: sb.append(", even page");
163: break;
164: case Constants.EN_ODD_PAGE:
165: sb.append(", odd page");
166: break;
167: default:
168: }
169: sb.append(")");
170: }
171: return sb.toString();
172: }
173:
174: }
|