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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /* $Id$ */
  18. package org.apache.fop;
  19. import java.util.List;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. import static org.junit.Assert.assertEquals;
  23. import org.apache.fop.layoutmgr.BlockKnuthSequence;
  24. import org.apache.fop.layoutmgr.BreakingAlgorithm;
  25. import org.apache.fop.layoutmgr.ElementListObserver;
  26. import org.apache.fop.layoutmgr.KnuthBox;
  27. import org.apache.fop.layoutmgr.KnuthGlue;
  28. import org.apache.fop.layoutmgr.KnuthPenalty;
  29. import org.apache.fop.layoutmgr.KnuthSequence;
  30. /**
  31. * Tests the Knuth algorithm implementation.
  32. */
  33. public class KnuthAlgorithmTestCase {
  34. @Before
  35. public void setUp() {
  36. DebugHelper.registerStandardElementListObservers();
  37. }
  38. private KnuthSequence getKnuthSequence1() {
  39. KnuthSequence seq = new BlockKnuthSequence();
  40. for (int i = 0; i < 5; i++) {
  41. seq.add(new KnuthBox(0, null, true));
  42. seq.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false, null, true));
  43. seq.add(new KnuthGlue(5000, 0, 0, null, true));
  44. seq.add(new KnuthBox(10000, null, false));
  45. if (i < 4) {
  46. seq.add(new KnuthPenalty(0, 0, false, null, false));
  47. seq.add(new KnuthGlue(-5000, 0, 0, null, true));
  48. }
  49. }
  50. seq.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false, null, false));
  51. seq.add(new KnuthGlue(0, Integer.MAX_VALUE, 0, null, false));
  52. seq.add(new KnuthPenalty(0, -KnuthPenalty.INFINITE, false, null, false));
  53. ElementListObserver.observe(seq, "test", null);
  54. return seq;
  55. }
  56. /**
  57. * Tests a special condition where a negative-length glue occurs directly after a break
  58. * possibility.
  59. * @throws Exception if an error occurs
  60. */
  61. @Test
  62. public void test1() throws Exception {
  63. MyBreakingAlgorithm algo = new MyBreakingAlgorithm(0, 0, true, true, 0);
  64. algo.setConstantLineWidth(30000);
  65. KnuthSequence seq = getKnuthSequence1();
  66. algo.findBreakingPoints(seq, 1, true, BreakingAlgorithm.ALL_BREAKS);
  67. Part[] parts = algo.getParts();
  68. assertEquals("Sequence must produce 3 parts", 3, parts.length);
  69. assertEquals(5000, parts[0].difference);
  70. assertEquals(5000, parts[1].difference);
  71. }
  72. private class Part {
  73. private int difference;
  74. private double ratio;
  75. private int position;
  76. }
  77. private class MyBreakingAlgorithm extends BreakingAlgorithm {
  78. private final List<Part> parts = new java.util.ArrayList<Part>();
  79. public MyBreakingAlgorithm(int align, int alignLast, boolean first,
  80. boolean partOverflowRecovery, int maxFlagCount) {
  81. super(align, alignLast, first, partOverflowRecovery, maxFlagCount);
  82. }
  83. public Part[] getParts() {
  84. return parts.toArray(new Part[parts.size()]);
  85. }
  86. @Override
  87. public void updateData1(int total, double demerits) {
  88. //nop
  89. }
  90. @Override
  91. public void updateData2(KnuthNode bestActiveNode, KnuthSequence sequence, int total) {
  92. int difference = bestActiveNode.difference;
  93. // it is always allowed to adjust space, so the ratio must be set regardless of
  94. // the value of the property display-align; the ratio must be <= 1
  95. double ratio = bestActiveNode.adjustRatio;
  96. if (ratio < 0) {
  97. // page break with a negative difference:
  98. // spaces always have enough shrink
  99. difference = 0;
  100. } else if (ratio <= 1 && bestActiveNode.line < total) {
  101. // not-last page break with a positive difference smaller than the available
  102. // stretch: spaces can stretch to fill the whole difference
  103. difference = 0;
  104. } else if (ratio > 1) {
  105. // not-last page with a positive difference greater than the available stretch
  106. // spaces can stretch to fill the difference only partially
  107. ratio = 1;
  108. difference -= bestActiveNode.availableStretch;
  109. } else {
  110. // last page with a positive difference:
  111. // spaces do not need to stretch
  112. ratio = 0;
  113. }
  114. // add nodes at the beginning of the list, as they are found
  115. // backwards, from the last one to the first one
  116. Part part = new Part();
  117. part.difference = difference;
  118. part.ratio = ratio;
  119. part.position = bestActiveNode.position;
  120. parts.add(0, part);
  121. }
  122. @Override
  123. protected int filterActiveNodes() {
  124. //nop
  125. return 0;
  126. }
  127. }
  128. }