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.

KeepUtil.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.fo.Constants;
  20. import org.apache.fop.fo.properties.KeepProperty;
  21. import org.apache.fop.fo.properties.Property;
  22. /**
  23. * Utility class for working with keeps.
  24. */
  25. public class KeepUtil {
  26. /**
  27. * Converts a keep property into an integer value.
  28. * <p>
  29. * Note: The conversion restricts the effectively available integer range by two values.
  30. * Integer.MIN_VALUE is used to represent the value "auto" and
  31. * Integer.MAX_VALUE is used to represebt the value "always".
  32. * @param keep the keep property
  33. * @return the keep value as an integer
  34. */
  35. public static int getKeepStrength(Property keep) {
  36. if (keep.isAuto()) {
  37. return BlockLevelLayoutManager.KEEP_AUTO;
  38. } else if (keep.getEnum() == Constants.EN_ALWAYS) {
  39. return BlockLevelLayoutManager.KEEP_ALWAYS;
  40. } else {
  41. return keep.getNumber().intValue();
  42. }
  43. }
  44. /**
  45. * Returns the combined block-level keep strength from a keep property.
  46. * <p>
  47. * Note: This is a temporary method to be used until it is possible to differentiate between
  48. * page and column keeps!
  49. * @param keep the keep property
  50. * @return the combined keep strength
  51. */
  52. public static int getCombinedBlockLevelKeepStrength(KeepProperty keep) {
  53. return Math.max(
  54. getKeepStrength(keep.getWithinPage()),
  55. getKeepStrength(keep.getWithinColumn()));
  56. }
  57. /**
  58. * Indicates whether a keep strength indicates a keep constraint.
  59. * @param strength the keep strength
  60. * @return true if the keep is not "auto"
  61. */
  62. public static boolean hasKeep(int strength) {
  63. return strength > BlockLevelLayoutManager.KEEP_AUTO;
  64. }
  65. /**
  66. * Returns the penalty value to be used for a certain keep strength.
  67. * <ul>
  68. * <li>"auto": returns 0</li>
  69. * <li>"always": returns KnuthElement.INFINITE</li>
  70. * <li>otherwise: returns KnuthElement.INFINITE - 1</li>
  71. * </ul>
  72. * @param keepStrength the keep strength
  73. * @return the penalty value
  74. */
  75. public static int getPenaltyForKeep(int keepStrength) {
  76. if (keepStrength == BlockLevelLayoutManager.KEEP_AUTO) {
  77. return 0;
  78. }
  79. int penalty = KnuthElement.INFINITE;
  80. if (keepStrength < BlockLevelLayoutManager.KEEP_ALWAYS) {
  81. penalty--;
  82. }
  83. return penalty;
  84. }
  85. /**
  86. * Returns a string representation of a keep strength value.
  87. * @param keepStrength the keep strength
  88. * @return the string representation
  89. */
  90. public static String keepStrengthToString(int keepStrength) {
  91. if (keepStrength == BlockLevelLayoutManager.KEEP_AUTO) {
  92. return "auto";
  93. } else if (keepStrength == BlockLevelLayoutManager.KEEP_ALWAYS) {
  94. return "always";
  95. } else {
  96. return Integer.toString(keepStrength);
  97. }
  98. }
  99. }