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.

Keep.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
  3. * agreements. See the NOTICE file distributed with this work for additional information
  4. * regarding copyright ownership. The ASF licenses this file to You under the Apache
  5. * License, Version 2.0 (the "License"); you may not use this file except in compliance
  6. * with the License. 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 distributed under
  11. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. * KIND, either express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. /* $Id$ */
  16. package org.apache.fop.layoutmgr;
  17. import org.apache.fop.fo.Constants;
  18. import org.apache.fop.fo.properties.KeepProperty;
  19. import org.apache.fop.fo.properties.Property;
  20. /**
  21. * Object representing a keep constraint, corresponding
  22. * to the XSL-FO <a href="http://www.w3.org/TR/xsl/#d0e26492">keep properties</a>.
  23. */
  24. public class Keep {
  25. /** The integer value for "auto" keep strength. */
  26. private static final int STRENGTH_AUTO = Integer.MIN_VALUE;
  27. /** The integer value for "always" keep strength. */
  28. private static final int STRENGTH_ALWAYS = Integer.MAX_VALUE;
  29. public static final Keep KEEP_AUTO = new Keep(STRENGTH_AUTO, Constants.EN_AUTO);
  30. public static final Keep KEEP_ALWAYS = new Keep(STRENGTH_ALWAYS, Constants.EN_LINE);
  31. private int strength;
  32. private int context;
  33. private Keep(int strength, int context) {
  34. this.strength = strength;
  35. this.context = context;
  36. }
  37. private static int getKeepStrength(Property keep) {
  38. if (keep.isAuto()) {
  39. return STRENGTH_AUTO;
  40. } else if (keep.getEnum() == Constants.EN_ALWAYS) {
  41. return STRENGTH_ALWAYS;
  42. } else {
  43. return keep.getNumber().intValue();
  44. }
  45. }
  46. /**
  47. * Obtain a Keep instance corresponding to the given {@link KeepProperty}
  48. *
  49. * @param keepProperty the {@link KeepProperty}
  50. * @return a new instance corresponding to the given property
  51. */
  52. public static Keep getKeep(KeepProperty keepProperty) {
  53. Keep keep = new Keep(STRENGTH_AUTO, Constants.EN_AUTO);
  54. keep.update(keepProperty.getWithinPage(), Constants.EN_PAGE);
  55. keep.update(keepProperty.getWithinColumn(), Constants.EN_COLUMN);
  56. keep.update(keepProperty.getWithinLine(), Constants.EN_LINE);
  57. return keep;
  58. }
  59. private void update(Property keep, int context) {
  60. if (!keep.isAuto()) {
  61. this.strength = getKeepStrength(keep);
  62. this.context = context;
  63. }
  64. }
  65. /** @return {@code true} if the keep property was specified as "auto" */
  66. public boolean isAuto() {
  67. return strength == STRENGTH_AUTO;
  68. }
  69. /**
  70. * Returns the context of this keep.
  71. *
  72. * @return one of {@link Constants#EN_LINE}, {@link Constants#EN_COLUMN} or
  73. * {@link Constants#EN_PAGE}
  74. */
  75. public int getContext() {
  76. return context;
  77. }
  78. /** @return the penalty value corresponding to the strength of this Keep */
  79. public int getPenalty() {
  80. if (strength == STRENGTH_AUTO) {
  81. return 0;
  82. } else if (strength == STRENGTH_ALWAYS) {
  83. return KnuthElement.INFINITE;
  84. } else {
  85. return KnuthElement.INFINITE - 1;
  86. }
  87. }
  88. private static int getKeepContextPriority(int context) {
  89. switch (context) {
  90. case Constants.EN_LINE: return 0;
  91. case Constants.EN_COLUMN: return 1;
  92. case Constants.EN_PAGE: return 2;
  93. case Constants.EN_AUTO: return 3;
  94. default: throw new IllegalArgumentException();
  95. }
  96. }
  97. /**
  98. * Compare this Keep instance to another one, and return the
  99. * stronger one if the context is the same
  100. *
  101. * @param other the instance to compare to
  102. * @return the winning Keep instance
  103. */
  104. public Keep compare(Keep other) {
  105. /* check strength "always" first, regardless of priority */
  106. if (this.strength == STRENGTH_ALWAYS
  107. && this.strength > other.strength) {
  108. return this;
  109. } else if (other.strength == STRENGTH_ALWAYS
  110. && other.strength > this.strength) {
  111. return other;
  112. }
  113. int pThis = getKeepContextPriority(this.context);
  114. int pOther = getKeepContextPriority(other.context);
  115. /* equal priority: strongest wins */
  116. if (pThis == pOther) {
  117. return (strength >= other.strength) ? this : other;
  118. }
  119. /* different priority: lowest priority wins */
  120. return (pThis < pOther) ? this : other;
  121. }
  122. /** {@inheritDoc} */
  123. public String toString() {
  124. return (strength == STRENGTH_AUTO) ? "auto"
  125. : (strength == STRENGTH_ALWAYS) ? "always"
  126. : Integer.toString(strength);
  127. }
  128. }