KickJava   Java API By Example, From Geeks To Geeks.

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


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: BalancingColumnBreakingAlgorithm.java 428750 2006-08-04 15:13:53Z jeremias $ */
19
20 package org.apache.fop.layoutmgr;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.fop.layoutmgr.PageBreakingAlgorithm.PageBreakingLayoutListener;
25 import org.apache.fop.traits.MinOptMax;
26
27 /**
28  * This is a the breaking algorithm that is responsible for balancing columns in multi-column
29  * layout.
30  */

31 public class BalancingColumnBreakingAlgorithm extends PageBreakingAlgorithm {
32
33     private Log log = LogFactory.getLog(BalancingColumnBreakingAlgorithm.class);
34     
35     private int columnCount;
36     private int fullLen;
37     private int idealPartLen;
38     
39     public BalancingColumnBreakingAlgorithm(LayoutManager topLevelLM,
40             PageSequenceLayoutManager.PageProvider pageProvider,
41             PageBreakingLayoutListener layoutListener,
42             int alignment, int alignmentLast,
43             MinOptMax footnoteSeparatorLength,
44             boolean partOverflowRecovery,
45             int columnCount) {
46         super(topLevelLM, pageProvider, layoutListener,
47                 alignment, alignmentLast,
48                 footnoteSeparatorLength, partOverflowRecovery, false, false);
49         this.columnCount = columnCount;
50         this.considerTooShort = true; //This is important!
51
}
52     
53     /** @see org.apache.fop.layoutmgr.BreakingAlgorithm */
54     protected double computeDemerits(KnuthNode activeNode,
55             KnuthElement element, int fitnessClass, double r) {
56         double dem = super.computeDemerits(activeNode, element, fitnessClass, r);
57         if (log.isTraceEnabled()) {
58             log.trace("original demerit=" + dem + " " + totalWidth
59                     + " line=" + activeNode.line + "/" + columnCount
60                     + " pos=" + activeNode.position + "/" + (par.size() - 1));
61         }
62         int remParts = columnCount - activeNode.line;
63         int curPos = par.indexOf(element);
64         if (fullLen == 0) {
65             fullLen = ElementListUtils.calcContentLength(par, activeNode.position, par.size() - 1);
66             this.idealPartLen = (fullLen / columnCount);
67         }
68         int partLen = ElementListUtils.calcContentLength(par, activeNode.position, curPos - 1);
69         int restLen = ElementListUtils.calcContentLength(par, curPos - 1, par.size() - 1);
70         int avgRestLen = 0;
71         if (remParts > 0) {
72             avgRestLen = restLen / remParts;
73         }
74         if (log.isTraceEnabled()) {
75             log.trace("remaining parts: " + remParts + " rest len: " + restLen
76                     + " avg=" + avgRestLen);
77         }
78         double balance = (idealPartLen - partLen) / 1000f;
79         if (log.isTraceEnabled()) {
80             log.trace("balance=" + balance);
81         }
82         double absBalance = Math.abs(balance);
83         dem = absBalance;
84         //Step 1: This does the rough balancing
85
if (columnCount > 2) {
86             if (balance > 0) {
87                 //shorter parts are less desired than longer ones
88
dem = dem * 1.2f;
89             }
90         } else {
91             if (balance < 0) {
92                 //shorter parts are less desired than longer ones
93
dem = dem * 1.2f;
94             }
95         }
96         //Step 2: This helps keep the trailing parts shorter than the previous ones
97
dem += (avgRestLen) / 1000f;
98         
99         if (activeNode.line >= columnCount) {
100             //We don't want more columns than available
101
dem = Double.MAX_VALUE;
102         }
103         if (log.isTraceEnabled()) {
104             log.trace("effective dem=" + dem + " " + totalWidth);
105         }
106         return dem;
107     }
108 }
109
Popular Tags