Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MinOptMaxUtil.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.datatypes.PercentBaseContext;
  20. import org.apache.fop.fo.Constants;
  21. import org.apache.fop.fo.properties.LengthRangeProperty;
  22. import org.apache.fop.traits.MinOptMax;
  23. /**
  24. * Utilities for MinOptMax and LengthRangeProperty.
  25. */
  26. public class MinOptMaxUtil {
  27. /**
  28. * Restricts a MinOptMax using the values from a LengthRangeProperty.
  29. * @param mom MinOptMax to restrict
  30. * @param lr restricting source
  31. * @param context Percentage evaluation context
  32. */
  33. public static void restrict(MinOptMax mom, LengthRangeProperty lr,
  34. PercentBaseContext context) {
  35. if (lr.getEnum() != Constants.EN_AUTO) {
  36. if (lr.getMinimum(context).getEnum() != Constants.EN_AUTO) {
  37. int min = lr.getMinimum(context).getLength().getValue(context);
  38. if (min > mom.min) {
  39. mom.min = min;
  40. fixAfterMinChanged(mom);
  41. }
  42. }
  43. if (lr.getMaximum(context).getEnum() != Constants.EN_AUTO) {
  44. int max = lr.getMaximum(context).getLength().getValue(context);
  45. if (max < mom.max) {
  46. mom.max = max;
  47. if (mom.max < mom.opt) {
  48. mom.opt = mom.max;
  49. mom.min = mom.opt;
  50. }
  51. }
  52. }
  53. if (lr.getOptimum(context).getEnum() != Constants.EN_AUTO) {
  54. int opt = lr.getOptimum(context).getLength().getValue(context);
  55. if (opt > mom.min) {
  56. mom.opt = opt;
  57. if (mom.opt > mom.max) {
  58. mom.max = mom.opt;
  59. }
  60. }
  61. }
  62. }
  63. }
  64. /**
  65. * Extends the minimum length to the given length if necessary, and adjusts opt and
  66. * max accordingly.
  67. *
  68. * @param mom the min/opt/max trait
  69. * @param len the new minimum length
  70. */
  71. public static void extendMinimum(MinOptMax mom, int len) {
  72. if (mom.min < len) {
  73. mom.min = len;
  74. mom.opt = Math.max(mom.min, mom.opt);
  75. mom.max = Math.max(mom.opt, mom.max);
  76. }
  77. }
  78. /**
  79. * After a calculation on a MinOptMax, this can be called to set opt to
  80. * a new effective value.
  81. * @param mom MinOptMax to adjust
  82. */
  83. public static void fixAfterMinChanged(MinOptMax mom) {
  84. if (mom.min > mom.opt) {
  85. mom.opt = mom.min;
  86. if (mom.opt > mom.max) {
  87. mom.max = mom.opt;
  88. }
  89. }
  90. }
  91. /**
  92. * Converts a LengthRangeProperty to a MinOptMax.
  93. * @param prop LengthRangeProperty
  94. * @param context Percentage evaluation context
  95. * @return the requested MinOptMax instance
  96. */
  97. public static MinOptMax toMinOptMax(LengthRangeProperty prop, PercentBaseContext context) {
  98. MinOptMax mom = new MinOptMax(
  99. (prop.getMinimum(context).isAuto()
  100. ? 0 : prop.getMinimum(context).getLength().getValue(context)),
  101. (prop.getOptimum(context).isAuto()
  102. ? 0 : prop.getOptimum(context).getLength().getValue(context)),
  103. (prop.getMaximum(context).isAuto()
  104. ? Integer.MAX_VALUE
  105. : prop.getMaximum(context).getLength().getValue(context)));
  106. return mom;
  107. }
  108. }