001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: MinOptMaxUtil.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.layoutmgr;
021:
022: import org.apache.fop.datatypes.PercentBaseContext;
023: import org.apache.fop.fo.Constants;
024: import org.apache.fop.fo.properties.LengthRangeProperty;
025: import org.apache.fop.traits.MinOptMax;
026:
027: /**
028: * Utilities for MinOptMax and LengthRangeProperty.
029: */
030: public class MinOptMaxUtil {
031:
032: /**
033: * Restricts a MinOptMax using the values from a LengthRangeProperty.
034: * @param mom MinOptMax to restrict
035: * @param lr restricting source
036: * @param context Percentage evaluation context
037: */
038: public static void restrict(MinOptMax mom, LengthRangeProperty lr,
039: PercentBaseContext context) {
040: if (lr.getEnum() != Constants.EN_AUTO) {
041: if (lr.getMinimum(context).getEnum() != Constants.EN_AUTO) {
042: int min = lr.getMinimum(context).getLength().getValue(
043: context);
044: if (min > mom.min) {
045: mom.min = min;
046: fixAfterMinChanged(mom);
047: }
048: }
049: if (lr.getMaximum(context).getEnum() != Constants.EN_AUTO) {
050: int max = lr.getMaximum(context).getLength().getValue(
051: context);
052: if (max < mom.max) {
053: mom.max = max;
054: if (mom.max < mom.opt) {
055: mom.opt = mom.max;
056: mom.min = mom.opt;
057: }
058: }
059: }
060: if (lr.getOptimum(context).getEnum() != Constants.EN_AUTO) {
061: int opt = lr.getOptimum(context).getLength().getValue(
062: context);
063: if (opt > mom.min) {
064: mom.opt = opt;
065: if (mom.opt > mom.max) {
066: mom.max = mom.opt;
067: }
068: }
069: }
070: }
071: }
072:
073: /**
074: * Extend the minimum length to the given length.
075: * @param mom the min/opt/max trait
076: * @param len the new minimum length
077: * @param optToLen if set adjusts the optimum length to be the smaller of the
078: * minimum length and the given length
079: */
080: public static void extendMinimum(MinOptMax mom, int len,
081: boolean optToLen) {
082: if (mom.min < len) {
083: mom.min = len;
084: mom.opt = Math.max(mom.min, mom.opt);
085: if (optToLen) {
086: mom.opt = Math.min(mom.min, len);
087: }
088: mom.max = Math.max(mom.opt, mom.max);
089: }
090: }
091:
092: /**
093: * After a calculation on a MinOptMax, this can be called to set opt to
094: * a new effective value.
095: * @param mom MinOptMax to adjust
096: */
097: public static void fixAfterMinChanged(MinOptMax mom) {
098: if (mom.min > mom.opt) {
099: mom.opt = mom.min;
100: if (mom.opt > mom.max) {
101: mom.max = mom.opt;
102: }
103: }
104: }
105:
106: /**
107: * Converts a LengthRangeProperty to a MinOptMax.
108: * @param prop LengthRangeProperty
109: * @param context Percentage evaluation context
110: * @return the requested MinOptMax instance
111: */
112: public static MinOptMax toMinOptMax(LengthRangeProperty prop,
113: PercentBaseContext context) {
114: MinOptMax mom = new MinOptMax((prop.getMinimum(context)
115: .isAuto() ? 0 : prop.getMinimum(context).getLength()
116: .getValue(context)),
117: (prop.getOptimum(context).isAuto() ? 0 : prop
118: .getOptimum(context).getLength().getValue(
119: context)), (prop.getMinimum(context)
120: .isAuto() ? Integer.MAX_VALUE : prop
121: .getMaximum(context).getLength().getValue(
122: context)));
123: return mom;
124: }
125:
126: }
|