KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > KnuthAlgorithmTestCase


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: KnuthAlgorithmTestCase.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop;
21
22 import java.util.List JavaDoc;
23
24 import org.apache.fop.layoutmgr.BlockKnuthSequence;
25 import org.apache.fop.layoutmgr.BreakingAlgorithm;
26 import org.apache.fop.layoutmgr.ElementListObserver;
27 import org.apache.fop.layoutmgr.KnuthBox;
28 import org.apache.fop.layoutmgr.KnuthGlue;
29 import org.apache.fop.layoutmgr.KnuthPenalty;
30 import org.apache.fop.layoutmgr.KnuthSequence;
31
32 import junit.framework.TestCase;
33
34 /**
35  * Tests the Knuth algorithm implementation.
36  */

37 public class KnuthAlgorithmTestCase extends TestCase {
38
39     /** @see junit.framework.TestCase#setUp() */
40     protected void setUp() throws Exception JavaDoc {
41         super.setUp();
42         DebugHelper.registerStandardElementListObservers();
43     }
44
45     private KnuthSequence getKnuthSequence1() {
46         KnuthSequence seq = new BlockKnuthSequence();
47         for (int i = 0; i < 5; i++) {
48             seq.add(new KnuthBox(0, null, true));
49             seq.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false, null, true));
50             seq.add(new KnuthGlue(5000, 0, 0, null, true));
51             seq.add(new KnuthBox(10000, null, false));
52             if (i < 4) {
53                 seq.add(new KnuthPenalty(0, 0, false, null, false));
54                 seq.add(new KnuthGlue(-5000, 0, 0, null, true));
55             }
56         }
57         
58         seq.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false, null, false));
59         seq.add(new KnuthGlue(0, Integer.MAX_VALUE, 0, null, false));
60         seq.add(new KnuthPenalty(0, -KnuthPenalty.INFINITE, false, null, false));
61         ElementListObserver.observe(seq, "test", null);
62         return seq;
63     }
64     
65     /**
66      * Tests a special condition where a negative-length glue occurs directly after a break
67      * possibility.
68      * @throws Exception if an error occurs
69      */

70     public void test1() throws Exception JavaDoc {
71         MyBreakingAlgorithm algo = new MyBreakingAlgorithm(0, 0, true, true, 0);
72         algo.setConstantLineWidth(30000);
73         KnuthSequence seq = getKnuthSequence1();
74         algo.findBreakingPoints(seq, 1, true, BreakingAlgorithm.ALL_BREAKS);
75         Part[] parts = algo.getParts();
76         assertEquals("Sequence must produce 3 parts", 3, parts.length);
77         assertEquals(5000, parts[0].difference);
78         assertEquals(5000, parts[1].difference);
79     }
80     
81     private class Part {
82         private int difference;
83         private double ratio;
84         private int position;
85     }
86     
87     private class MyBreakingAlgorithm extends BreakingAlgorithm {
88
89         private List JavaDoc parts = new java.util.ArrayList JavaDoc();
90         
91         public MyBreakingAlgorithm(int align, int alignLast, boolean first,
92                     boolean partOverflowRecovery, int maxFlagCount) {
93             super(align, alignLast, first, partOverflowRecovery, maxFlagCount);
94         }
95
96         public Part[] getParts() {
97             return (Part[])parts.toArray(new Part[parts.size()]);
98         }
99         
100         public void updateData1(int total, double demerits) {
101             //nop
102
}
103
104         public void updateData2(KnuthNode bestActiveNode, KnuthSequence sequence, int total) {
105             int difference = bestActiveNode.difference;
106             // it is always allowed to adjust space, so the ratio must be set regardless of
107
// the value of the property display-align; the ratio must be <= 1
108
double ratio = bestActiveNode.adjustRatio;
109             if (ratio < 0) {
110                 // page break with a negative difference:
111
// spaces always have enough shrink
112
difference = 0;
113             } else if (ratio <= 1 && bestActiveNode.line < total) {
114                 // not-last page break with a positive difference smaller than the available
115
// stretch: spaces can stretch to fill the whole difference
116
difference = 0;
117             } else if (ratio > 1) {
118                 // not-last page with a positive difference greater than the available stretch
119
// spaces can stretch to fill the difference only partially
120
ratio = 1;
121                 difference -= bestActiveNode.availableStretch;
122             } else {
123                 // last page with a positive difference:
124
// spaces do not need to stretch
125
ratio = 0;
126             }
127
128             // add nodes at the beginning of the list, as they are found
129
// backwards, from the last one to the first one
130
Part part = new Part();
131             part.difference = difference;
132             part.ratio = ratio;
133             part.position = bestActiveNode.position;
134             parts.add(0, part);
135         }
136
137         protected int filterActiveNodes() {
138             //nop
139
return 0;
140         }
141         
142     }
143     
144 }
145
Popular Tags