aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/layoutmgr/table/CollapsingBorderModelEyeCatching.java
blob: d63ba8d48769e29d73f2470300163f63d2783cbb (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * 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.layoutmgr.table;

import org.apache.fop.fo.Constants;
import org.apache.fop.fo.flow.Table;
import org.apache.fop.fo.flow.TableBody;
import org.apache.fop.fo.flow.TableCell;
import org.apache.fop.fo.flow.TableColumn;
import org.apache.fop.fo.flow.TableRow;
import org.apache.fop.fo.properties.CommonBorderPaddingBackground.BorderInfo;

/**
 * Implements the normal "collapse" border model defined in 6.7.10 in XSL 1.0.
 * 
 * TODO Column groups are not yet checked in this algorithm!
 */
public class CollapsingBorderModelEyeCatching extends CollapsingBorderModel {

    public BorderInfo determineWinner(GridUnit currentGridUnit, 
            GridUnit otherGridUnit, int side, int flags) {
        final boolean vertical = isVerticalRelation(side);
        final int otherSide = getOtherSide(side);
        
        //Get cells
        TableCell currentCell = currentGridUnit.getCell();
        TableCell otherCell = null;
        if (otherGridUnit != null) {
            otherCell = otherGridUnit.getCell();
        }
        
        //Get rows
        TableRow currentRow = currentGridUnit.getRow();
        TableRow otherRow = null;
        if (vertical && otherCell != null) {
            otherRow = otherGridUnit.getRow();
        }
        
        //get bodies
        TableBody currentBody = currentGridUnit.getBody();
        TableBody otherBody = null;
        if (otherRow != null) {
            otherBody = otherGridUnit.getBody();
        }

        //get columns
        TableColumn currentColumn = currentGridUnit.getColumn();
        TableColumn otherColumn = null;
        if (otherGridUnit != null) {
            otherColumn = otherGridUnit.getColumn();
        }
        
        //TODO get column groups
        
        //Get table
        Table table = currentGridUnit.getTable();
        
        //----------------------------------------------------------------------
        //We're creating two arrays containing the applicable BorderInfos for
        //each cell in question.
        //0 = cell, 1 = row, 2 = row group (body), 3 = column, 
        //4 = col group (spanned column, see 6.7.3), 5 = table

        BorderInfo[] current = new BorderInfo[6];
        BorderInfo[] other = new BorderInfo[6];
        //cell
        current[0] = currentGridUnit.getOriginalBorderInfoForCell(side);
        if (otherGridUnit != null) {
            other[0] = otherGridUnit.getOriginalBorderInfoForCell(otherSide);
        }
        if ((currentRow != null) 
                && (side == BEFORE 
                    || side == AFTER
                    || (currentGridUnit.getFlag(GridUnit.IN_FIRST_COLUMN) && side == START)
                    || (currentGridUnit.getFlag(GridUnit.IN_LAST_COLUMN) && side == END))) {
            //row
            current[1] = currentRow.getCommonBorderPaddingBackground().getBorderInfo(side);
        }
        if (otherRow != null) {
            //row
            other[1] = otherRow.getCommonBorderPaddingBackground().getBorderInfo(otherSide);
        }
        if (currentBody != null
                && ((side == BEFORE && currentGridUnit.getFlag(GridUnit.FIRST_IN_BODY))
                || (side == AFTER && currentGridUnit.getFlag(GridUnit.LAST_IN_BODY))
                || (currentGridUnit.getFlag(GridUnit.IN_FIRST_COLUMN) && side == START)
                || (currentGridUnit.getFlag(GridUnit.IN_LAST_COLUMN) && side == END))) {
            //row group (=body, table-header or table-footer)
            current[2] = currentBody.getCommonBorderPaddingBackground().getBorderInfo(side);
        }
        if (otherGridUnit != null
                && otherBody != null
                && ((otherSide == BEFORE && otherGridUnit.getFlag(GridUnit.FIRST_IN_BODY))
                    || (otherSide == AFTER && otherGridUnit.getFlag(GridUnit.LAST_IN_BODY)))) {
            //row group (=body, table-header or table-footer)
            other[2] = otherBody.getCommonBorderPaddingBackground().getBorderInfo(otherSide);
        }
        if ((side == BEFORE && otherGridUnit == null)
                || (side == AFTER && otherGridUnit == null)
                || (side == START)
                || (side == END)) {
            //column
            current[3] = currentColumn.getCommonBorderPaddingBackground().getBorderInfo(side);
        }
        if (otherColumn != null) {
            //column
            other[3] = otherColumn.getCommonBorderPaddingBackground().getBorderInfo(otherSide);
        }
        //TODO current[4] and other[4] for column groups
        if (otherGridUnit == null
            && ((side == BEFORE && (flags & VERTICAL_START_END_OF_TABLE) > 0)
                    || (side == AFTER && (flags & VERTICAL_START_END_OF_TABLE) > 0)
                    || (side == START)
                    || (side == END))) {
            //table
            current[5] = table.getCommonBorderPaddingBackground().getBorderInfo(side);
        }
        //other[6] is always null, since it's always the same table
        
        BorderInfo resolved = null;
        
        // *** Rule 1 ***
        resolved = doRule1(current, other);
        if (resolved != null) {
            return resolved;
        }
        
        // *** Rule 2 ***
        if (!doRule2(current, other)) {
        }
        
        // *** Rule 3 ***
        resolved = doRule3(current, other);
        if (resolved != null) {
            return resolved;
        }
        
        // *** Rule 4 ***
        resolved = doRule4(current, other);
        if (resolved != null) {
            return resolved;
        }
        
        // *** Rule 5 ***
        resolved = doRule5(current, other);
        if (resolved != null) {
            return resolved;
        }
        
        return null; //no winner, no border
    }

    private BorderInfo doRule1(BorderInfo[] current, BorderInfo[] other) {
        for (int i = 0; i < current.length; i++) {
            if ((current[i] != null) && (current[i].getStyle() == Constants.EN_HIDDEN)) {
                return current[i];
            }
            if ((other[i] != null) && (other[i].getStyle() == Constants.EN_HIDDEN)) {
                return other[i];
            }
        }
        return null;
    }
    
    private boolean doRule2(BorderInfo[] current, BorderInfo[] other) {
        boolean found = false;
        for (int i = 0; i < current.length; i++) {
            if ((current[i] != null) && (current[i].getStyle() != Constants.EN_NONE)) {
                found = true;
                break;
            }
            if ((other[i] != null) && (other[i].getStyle() != Constants.EN_NONE)) {
                found = true;
                break;
            }
        }
        return found;
    }

    private BorderInfo doRule3(BorderInfo[] current, BorderInfo[] other) {
        int width = 0;
        //Find max border width
        for (int i = 0; i < current.length; i++) {
            if ((current[i] != null) && (current[i].getRetainedWidth() > width)) {
                width = current[i].getRetainedWidth();
            }
            if ((other[i] != null) && (other[i].getRetainedWidth() > width)) {
                width = other[i].getRetainedWidth();
            }
        }
        BorderInfo widest = null;
        int count = 0;
        //See if there's only one with the widest border
        for (int i = 0; i < current.length; i++) {
            if ((current[i] != null) && (current[i].getRetainedWidth() == width)) {
                count++;
                if (widest == null) {
                    widest = current[i];
                }
            } else {
                current[i] = null; //Discard the narrower ones
            }
            if ((other[i] != null) && (other[i].getRetainedWidth() == width)) {
                count++;
                if (widest == null) {
                    widest = other[i];
                }
            } else {
                other[i] = null; //Discard the narrower ones
            }
        }
        if (count == 1) {
            return widest;
        } else {
            return null;
        }
    }

    private BorderInfo doRule4(BorderInfo[] current, BorderInfo[] other) {
        int pref = getPreferenceValue(Constants.EN_INSET); //Lowest preference
        //Find highest preference value
        for (int i = 0; i < current.length; i++) {
            if (current[i] != null) {
                int currPref = getPreferenceValue(current[i].getStyle());
                if (currPref > pref) {
                    pref = currPref;
                }
            }
            if (other[i] != null) {
                int currPref = getPreferenceValue(other[i].getStyle());
                if (currPref > pref) {
                    pref = currPref;
                }
            }
        }
        BorderInfo preferred = null;
        int count = 0;
        //See if there's only one with the preferred border style
        for (int i = 0; i < current.length; i++) {
            if (current[i] != null) {
                int currPref = getPreferenceValue(current[i].getStyle());
                if (currPref == pref) {
                    count++;
                    if (preferred == null) {
                        preferred = current[i];
                    }
                    break;
                }
            } else {
                current[i] = null; //Discard the ones that are not preferred
            }
            if (other[i] != null) {
                int currPref = getPreferenceValue(other[i].getStyle());
                if (currPref == pref) {
                    count++;
                    if (preferred == null) {
                        preferred = other[i];
                    }
                    break;
                }
            } else {
                other[i] = null; //Discard the ones that are not preferred
            }
        }
        if (count == 1) {
            return preferred;
        } else {
            return null;
        }
    }

    private BorderInfo doRule5(BorderInfo[] current, BorderInfo[] other) {
        for (int i = 0; i < current.length; i++) {
            if (current[i] != null) {
                return current[i];
            }
            if (other[i] != null) {
                return other[i];
            }
        }
        return null;
    }

}