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.

KnuthGlue.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.layoutmgr;
  19. import org.apache.fop.traits.MinOptMax;
  20. /**
  21. * An instance of this class represents a piece of content with adjustable
  22. * width: for example a space between words of justified text.
  23. *
  24. * A KnuthGlue is a feasible breaking point only if it immediately follows
  25. * a KnuthBox.
  26. *
  27. * The represented piece of content is suppressed if either the KnuthGlue
  28. * is a chosen breaking point or there isn't any KnuthBox between the
  29. * previous breaking point and the KnuthGlue itself.
  30. *
  31. * So, an unsuppressible piece of content with adjustable width, for example
  32. * a leader or a word with adjustable letter space, cannot be represented
  33. * by a single KnuthGlue; it can be represented using the sequence:
  34. * KnuthBox(width = 0)
  35. * KnuthPenalty(width = 0, penalty = infinity)
  36. * KnuthGlue(...)
  37. * KnuthBox(width = 0)
  38. * where the infinity penalty avoids choosing the KnuthGlue as a breaking point
  39. * and the 0-width KnuthBoxes prevent suppression.
  40. *
  41. * Besides the inherited methods and attributes, this class has two attributes
  42. * used to store the stretchability (difference between max and opt width) and
  43. * the shrinkability (difference between opt and min width), and the methods
  44. * to get these values.
  45. */
  46. public class KnuthGlue extends KnuthElement {
  47. private final int stretch;
  48. private final int shrink;
  49. private final Adjustment adjustmentClass;
  50. /**
  51. * Creates a new <code>KnuthGlue</code>.
  52. *
  53. * @param minOptMax a <code>MinOptMax</code> where the {@link MinOptMax#getOpt() opt-value} is
  54. * mapped to the width, the {@link MinOptMax#getStretch()
  55. * stretchability} is mapped to the stretchability and the the {@link
  56. * MinOptMax#getShrink() shrinkability} is mapped to the shrinkability
  57. * @param pos the Position stored in this glue
  58. * @param auxiliary is this glue auxiliary?
  59. */
  60. public KnuthGlue(MinOptMax minOptMax, Position pos, boolean auxiliary) {
  61. super(minOptMax.getOpt(), pos, auxiliary);
  62. this.stretch = minOptMax.getStretch();
  63. this.shrink = minOptMax.getShrink();
  64. this.adjustmentClass = Adjustment.NO_ADJUSTMENT;
  65. }
  66. /**
  67. * Creates a new <code>KnuthGlue</code>.
  68. *
  69. * @param width the width of this glue
  70. * @param stretch the stretchability of this glue
  71. * @param shrink the shrinkability of this glue
  72. * @param pos the Position stored in this glue
  73. * @param auxiliary is this glue auxiliary?
  74. */
  75. public KnuthGlue(int width, int stretch, int shrink, Position pos,
  76. boolean auxiliary) {
  77. super(width, pos, auxiliary);
  78. this.stretch = stretch;
  79. this.shrink = shrink;
  80. this.adjustmentClass = Adjustment.NO_ADJUSTMENT;
  81. }
  82. /**
  83. * Creates a new <code>KnuthGlue</code>.
  84. *
  85. * @param width the width of this glue
  86. * @param stretch the stretchability of this glue
  87. * @param shrink the shrinkability of this glue
  88. * @param adjustmentClass the adjsutment class
  89. * @param pos the Position stored in this glue
  90. * @param auxiliary is this glue auxiliary?
  91. */
  92. public KnuthGlue(int width, int stretch, int shrink, Adjustment adjustmentClass,
  93. Position pos, boolean auxiliary) {
  94. super(width, pos, auxiliary);
  95. this.stretch = stretch;
  96. this.shrink = shrink;
  97. this.adjustmentClass = adjustmentClass;
  98. }
  99. /** {@inheritDoc} */
  100. public boolean isGlue() {
  101. return true;
  102. }
  103. /** @return the stretchability of this glue. */
  104. public int getStretch() {
  105. return stretch;
  106. }
  107. /** @return the shrinkability of this glue. */
  108. public int getShrink() {
  109. return shrink;
  110. }
  111. /** @return the adjustment class (or role) of this glue. */
  112. public Adjustment getAdjustmentClass() {
  113. return adjustmentClass;
  114. }
  115. /** {@inheritDoc} */
  116. public String toString() {
  117. StringBuffer buffer = new StringBuffer(64);
  118. if (isAuxiliary()) {
  119. buffer.append("aux. ");
  120. }
  121. buffer.append("glue");
  122. buffer.append(" w=").append(getWidth());
  123. buffer.append(" stretch=").append(getStretch());
  124. buffer.append(" shrink=").append(getShrink());
  125. if (!getAdjustmentClass().equals(Adjustment.NO_ADJUSTMENT)) {
  126. buffer.append(" adj-class=").append(getAdjustmentClass());
  127. }
  128. return buffer.toString();
  129. }
  130. }