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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 List parts = new java.util.ArrayList();
  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 (Part[])parts.toArray(new Part[parts.size()]);
  85. }
  86. public void updateData1(int total, double demerits) {
  87. //nop
  88. }
  89. public void updateData2(KnuthNode bestActiveNode, KnuthSequence sequence, int total) {
  90. int difference = bestActiveNode.difference;
  91. // it is always allowed to adjust space, so the ratio must be set regardless of
  92. // the value of the property display-align; the ratio must be <= 1
  93. double ratio = bestActiveNode.adjustRatio;
  94. if (ratio < 0) {
  95. // page break with a negative difference:
  96. // spaces always have enough shrink
  97. difference = 0;
  98. } else if (ratio <= 1 && bestActiveNode.line < total) {
  99. // not-last page break with a positive difference smaller than the available
  100. // stretch: spaces can stretch to fill the whole difference
  101. difference = 0;
  102. } else if (ratio > 1) {
  103. // not-last page with a positive difference greater than the available stretch
  104. // spaces can stretch to fill the difference only partially
  105. ratio = 1;
  106. difference -= bestActiveNode.availableStretch;
  107. } else {
  108. // last page with a positive difference:
  109. // spaces do not need to stretch
  110. ratio = 0;
  111. }
  112. // add nodes at the beginning of the list, as they are found
  113. // backwards, from the last one to the first one
  114. Part part = new Part();
  115. part.difference = difference;
  116. part.ratio = ratio;
  117. part.position = bestActiveNode.position;
  118. parts.add(0, part);
  119. }
  120. protected int filterActiveNodes() {
  121. //nop
  122. return 0;
  123. }
  124. }
  125. }