aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/area/inline/AbstractTextArea.java
blob: a50500979919f6ece8a9f68667ad13b1e55d9a9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright 2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.area.inline;

/**
 * Abstract base class for both TextArea and Character.
 */
public abstract class AbstractTextArea extends InlineArea {

    /**
     * this class stores information about spaces and potential adjustments
     * that can be used in order to re-compute adjustments when a
     * page-number or a page-number-citation is resolved
     */
    protected class TextAdjustingInfo extends InlineAdjustingInfo {

        /** difference between the optimal width of a space
         * and the default width of a space according to the font
         * (this is equivalent to the property word-spacing.optimum)
         */
        protected int spaceDifference = 0;
        
        /**
         * Constructor
         *
         * @param stretch the available space for stretching
         * @param shrink the available space for shrinking
         * @param adj space adjustment type
         */
        protected TextAdjustingInfo(int stretch, int shrink, int adj) {
            super(stretch, shrink, adj);
        }
    }

    private int textWordSpaceAdjust = 0;
    private int textLetterSpaceAdjust = 0;
    private TextAdjustingInfo adjustingInfo = null;
    private int baselineOffset = 0;

    /**
     * Default constructor
     */
    public AbstractTextArea() {
    }

    /**
     * Constructor with extra parameters:
     * create a TextAdjustingInfo object
     * @param stretch  the available stretch of the text
     * @param shrink   the available shrink of the text
     * @param adj      the current adjustment of the area
     */
    public AbstractTextArea(int stretch, int shrink, int adj) {
        adjustingInfo = new TextAdjustingInfo(stretch, shrink, adj);
    }
    
    /**
     * Get text word space adjust.
     *
     * @return the text word space adjustment
     */
    public int getTextWordSpaceAdjust() {
        return textWordSpaceAdjust;
    }

    /**
     * Set text word space adjust.
     *
     * @param textWordSpaceAdjust the text word space adjustment
     */
    public void setTextWordSpaceAdjust(int textWordSpaceAdjust) {
        this.textWordSpaceAdjust = textWordSpaceAdjust;
    }

    /**
     * Get text letter space adjust.
     *
     * @return the text letter space adjustment
     */
    public int getTextLetterSpaceAdjust() {
        return textLetterSpaceAdjust;
    }

    /**
     * Set text letter space adjust.
     *
     * @param textLetterSpaceAdjust the text letter space adjustment
     */
    public void setTextLetterSpaceAdjust(int textLetterSpaceAdjust) {
        this.textLetterSpaceAdjust = textLetterSpaceAdjust;
    }

    /**
     * Set the difference between optimal width of a space and 
     * default width of a space according to the font; this part
     * of the space adjustment is fixed and must not be 
     * multiplied by the variation factor.
     * @param spaceDiff the space difference
     */
    public void setSpaceDifference(int spaceDiff) {
        adjustingInfo.spaceDifference = spaceDiff;
    }

    /**
     * recursively apply the variation factor to all descendant areas
     * @param variationFactor the variation factor that must be applied to adjustments
     * @param lineStretch     the total stretch of the line
     * @param lineShrink      the total shrink of the line
     * @return true if there is an UnresolvedArea descendant
     */
    public boolean applyVariationFactor(double variationFactor,
                                        int lineStretch, int lineShrink) {
        if (adjustingInfo != null) {
            // compute the new adjustments:
            // if the variation factor is negative, it means that before 
            // the ipd variation the line had to stretch and now it has
            // to shrink (or vice versa);
            // in this case, if the stretch and shrink are not equally 
            // divided among the inline areas, we must compute a 
            // balancing factor
            double balancingFactor = 1.0;
            if (variationFactor < 0) {
                if (textWordSpaceAdjust < 0) {
                    // from a negative adjustment to a positive one
                    balancingFactor 
                        = ((double) adjustingInfo.availableStretch / adjustingInfo.availableShrink)
                            * ((double) lineShrink / lineStretch);
                } else {
                    // from a positive adjustment to a negative one
                    balancingFactor 
                        = ((double) adjustingInfo.availableShrink / adjustingInfo.availableStretch)
                            * ((double) lineStretch / lineShrink);
                }
            }
            textWordSpaceAdjust = (int) ((textWordSpaceAdjust - adjustingInfo.spaceDifference)
                    * variationFactor * balancingFactor)
                    + adjustingInfo.spaceDifference;
            textLetterSpaceAdjust *= variationFactor;
            // update the ipd of the area
            int oldAdjustment = adjustingInfo.adjustment;
            adjustingInfo.adjustment *= balancingFactor * variationFactor;
            ipd += adjustingInfo.adjustment - oldAdjustment;
        }
        return false;
    }

    /**
     * Get baseline offset, i.e. the distance from the before edge
     * of this area to the nominal baseline.
     *
     * @return the baseline offset
     */
    public int getBaselineOffset() {
        return baselineOffset;
    }

    /**
     * Set the baseline offset.
     *
     * @param baselineOffset the baseline offset
     */
    public void setBaselineOffset(int baselineOffset) {
        this.baselineOffset = baselineOffset;
    }
}