You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KnuthAlgorithmTestCase.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop;
  18. import java.util.List;
  19. import org.apache.fop.layoutmgr.BlockKnuthSequence;
  20. import org.apache.fop.layoutmgr.BreakingAlgorithm;
  21. import org.apache.fop.layoutmgr.ElementListObserver;
  22. import org.apache.fop.layoutmgr.KnuthBox;
  23. import org.apache.fop.layoutmgr.KnuthGlue;
  24. import org.apache.fop.layoutmgr.KnuthPenalty;
  25. import org.apache.fop.layoutmgr.KnuthSequence;
  26. import junit.framework.TestCase;
  27. /**
  28. * Tests the Knuth algorithm implementation.
  29. */
  30. public class KnuthAlgorithmTestCase extends TestCase {
  31. /** @see junit.framework.TestCase#setUp() */
  32. protected void setUp() throws Exception {
  33. super.setUp();
  34. DebugHelper.registerStandardElementListObservers();
  35. }
  36. private KnuthSequence getKnuthSequence1() {
  37. KnuthSequence seq = new BlockKnuthSequence();
  38. for (int i = 0; i < 5; i++) {
  39. seq.add(new KnuthBox(0, null, true));
  40. seq.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false, null, true));
  41. seq.add(new KnuthGlue(5000, 0, 0, null, true));
  42. seq.add(new KnuthBox(10000, null, false));
  43. if (i < 4) {
  44. seq.add(new KnuthPenalty(0, 0, false, null, false));
  45. seq.add(new KnuthGlue(-5000, 0, 0, null, true));
  46. }
  47. }
  48. seq.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false, null, false));
  49. seq.add(new KnuthGlue(0, Integer.MAX_VALUE, 0, null, false));
  50. seq.add(new KnuthPenalty(0, -KnuthPenalty.INFINITE, false, null, false));
  51. ElementListObserver.observe(seq, "test", null);
  52. return seq;
  53. }
  54. /**
  55. * Tests a special condition where a negative-length glue occurs directly after a break
  56. * possibility.
  57. * @throws Exception if an error occurs
  58. */
  59. public void test1() throws Exception {
  60. MyBreakingAlgorithm algo = new MyBreakingAlgorithm(0, 0, true, true, 0);
  61. algo.setConstantLineWidth(30000);
  62. KnuthSequence seq = getKnuthSequence1();
  63. algo.findBreakingPoints(seq, 1, true, BreakingAlgorithm.ALL_BREAKS);
  64. Part[] parts = algo.getParts();
  65. assertEquals("Sequence must produce 3 parts", 3, parts.length);
  66. assertEquals(5000, parts[0].difference);
  67. assertEquals(5000, parts[1].difference);
  68. }
  69. private class Part {
  70. private int difference;
  71. private double ratio;
  72. private int position;
  73. }
  74. private class MyBreakingAlgorithm extends BreakingAlgorithm {
  75. private List parts = new java.util.ArrayList();
  76. public MyBreakingAlgorithm(int align, int alignLast, boolean first,
  77. boolean partOverflowRecovery, int maxFlagCount) {
  78. super(align, alignLast, first, partOverflowRecovery, maxFlagCount);
  79. }
  80. public Part[] getParts() {
  81. return (Part[])parts.toArray(new Part[parts.size()]);
  82. }
  83. public void updateData1(int total, double demerits) {
  84. //nop
  85. }
  86. public void updateData2(KnuthNode bestActiveNode, KnuthSequence sequence, int total) {
  87. int difference = bestActiveNode.difference;
  88. // it is always allowed to adjust space, so the ratio must be set regardless of
  89. // the value of the property display-align; the ratio must be <= 1
  90. double ratio = bestActiveNode.adjustRatio;
  91. if (ratio < 0) {
  92. // page break with a negative difference:
  93. // spaces always have enough shrink
  94. difference = 0;
  95. } else if (ratio <= 1 && bestActiveNode.line < total) {
  96. // not-last page break with a positive difference smaller than the available
  97. // stretch: spaces can stretch to fill the whole difference
  98. difference = 0;
  99. } else if (ratio > 1) {
  100. // not-last page with a positive difference greater than the available stretch
  101. // spaces can stretch to fill the difference only partially
  102. ratio = 1;
  103. difference -= bestActiveNode.availableStretch;
  104. } else {
  105. // last page with a positive difference:
  106. // spaces do not need to stretch
  107. ratio = 0;
  108. }
  109. // add nodes at the beginning of the list, as they are found
  110. // backwards, from the last one to the first one
  111. Part part = new Part();
  112. part.difference = difference;
  113. part.ratio = ratio;
  114. part.position = bestActiveNode.position;
  115. parts.add(0, part);
  116. }
  117. protected int filterActiveNodes() {
  118. //nop
  119. return 0;
  120. }
  121. }
  122. }