1 17 18 19 20 package org.apache.fop; 21 22 import java.util.List ; 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 37 public class KnuthAlgorithmTestCase extends TestCase { 38 39 40 protected void setUp() throws Exception { 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 70 public void test1() throws Exception { 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 parts = new java.util.ArrayList (); 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 } 103 104 public void updateData2(KnuthNode bestActiveNode, KnuthSequence sequence, int total) { 105 int difference = bestActiveNode.difference; 106 double ratio = bestActiveNode.adjustRatio; 109 if (ratio < 0) { 110 difference = 0; 113 } else if (ratio <= 1 && bestActiveNode.line < total) { 114 difference = 0; 117 } else if (ratio > 1) { 118 ratio = 1; 121 difference -= bestActiveNode.availableStretch; 122 } else { 123 ratio = 0; 126 } 127 128 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 return 0; 140 } 141 142 } 143 144 } 145 | Popular Tags |