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: KnuthGlue.java 985537 2010-08-14 17:17:00Z jeremias $ */
19 
20 package org.apache.fop.layoutmgr;
21 
22 import org.apache.fop.traits.MinOptMax;
23 
24 /**
25  * An instance of this class represents a piece of content with adjustable
26  * width: for example a space between words of justified text.
27  *
28  * A KnuthGlue is a feasible breaking point only if it immediately follows
29  * a KnuthBox.
30  *
31  * The represented piece of content is suppressed if either the KnuthGlue
32  * is a chosen breaking point or there isn't any KnuthBox between the
33  * previous breaking point and the KnuthGlue itself.
34  *
35  * So, an unsuppressible piece of content with adjustable width, for example
36  * a leader or a word with adjustable letter space, cannot be represented
37  * by a single KnuthGlue; it can be represented using the sequence:
38  *   KnuthBox(width = 0)
39  *   KnuthPenalty(width = 0, penalty = infinity)
40  *   KnuthGlue(...)
41  *   KnuthBox(width = 0)
42  * where the infinity penalty avoids choosing the KnuthGlue as a breaking point
43  * and the 0-width KnuthBoxes prevent suppression.
44  *
45  * Besides the inherited methods and attributes, this class has two attributes
46  * used to store the stretchability (difference between max and opt width) and
47  * the shrinkability (difference between opt and min width), and the methods
48  * to get these values.
49  */
50 public class KnuthGlue extends KnuthElement {
51 
52     private final int stretch;
53     private final int shrink;
54     private final Adjustment adjustmentClass;
55 
56     /**
57      * Creates a new <code>KnuthGlue</code>.
58      *
59      * @param minOptMax a <code>MinOptMax</code> where the {@link MinOptMax#getOpt() opt-value} is
60      *                  mapped to the width, the {@link MinOptMax#getStretch()
61      *                  stretchability} is mapped to the stretchability and the the {@link
62      *                  MinOptMax#getShrink() shrinkability} is mapped to the shrinkability
63      * @param pos       the Position stored in this glue
64      * @param auxiliary is this glue auxiliary?
65      */
KnuthGlue(MinOptMax minOptMax, Position pos, boolean auxiliary)66     public KnuthGlue(MinOptMax minOptMax, Position pos, boolean auxiliary) {
67         super(minOptMax.getOpt(), pos, auxiliary);
68         this.stretch = minOptMax.getStretch();
69         this.shrink = minOptMax.getShrink();
70         this.adjustmentClass = Adjustment.NO_ADJUSTMENT;
71     }
72 
73     /**
74      * Creates a new <code>KnuthGlue</code>.
75      *
76      * @param width     the width of this glue
77      * @param stretch   the stretchability of this glue
78      * @param shrink    the shrinkability of this glue
79      * @param pos       the Position stored in this glue
80      * @param auxiliary is this glue auxiliary?
81      */
KnuthGlue(int width, int stretch, int shrink, Position pos, boolean auxiliary)82     public KnuthGlue(int width, int stretch, int shrink, Position pos,
83                      boolean auxiliary) {
84         super(width, pos, auxiliary);
85         this.stretch = stretch;
86         this.shrink = shrink;
87         this.adjustmentClass = Adjustment.NO_ADJUSTMENT;
88     }
89 
90     /**
91      * Creates a new <code>KnuthGlue</code>.
92      *
93      * @param width     the width of this glue
94      * @param stretch   the stretchability of this glue
95      * @param shrink    the shrinkability of this glue
96      * @param adjustmentClass the adjsutment class
97      * @param pos       the Position stored in this glue
98      * @param auxiliary is this glue auxiliary?
99      */
KnuthGlue(int width, int stretch, int shrink, Adjustment adjustmentClass, Position pos, boolean auxiliary)100     public KnuthGlue(int width, int stretch, int shrink, Adjustment adjustmentClass,
101                      Position pos, boolean auxiliary) {
102         super(width, pos, auxiliary);
103         this.stretch = stretch;
104         this.shrink = shrink;
105         this.adjustmentClass = adjustmentClass;
106     }
107 
108     /** {@inheritDoc} */
isGlue()109     public boolean isGlue() {
110         return true;
111     }
112 
113     /** @return the stretchability of this glue. */
getStretch()114     public int getStretch() {
115         return stretch;
116     }
117 
118     /** @return the shrinkability of this glue. */
getShrink()119     public int getShrink() {
120         return shrink;
121     }
122 
123     /** @return the adjustment class (or role) of this glue. */
getAdjustmentClass()124     public Adjustment getAdjustmentClass() {
125         return adjustmentClass;
126     }
127 
128     /** {@inheritDoc} */
toString()129     public String toString() {
130         StringBuffer buffer = new StringBuffer(64);
131         if (isAuxiliary()) {
132             buffer.append("aux. ");
133         }
134         buffer.append("glue");
135         buffer.append(" w=").append(getWidth());
136         buffer.append(" stretch=").append(getStretch());
137         buffer.append(" shrink=").append(getShrink());
138         if (!getAdjustmentClass().equals(Adjustment.NO_ADJUSTMENT)) {
139             buffer.append(" adj-class=").append(getAdjustmentClass());
140         }
141         return buffer.toString();
142     }
143 
144 }
145