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: SpaceVal.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.traits;
021:
022: import org.apache.fop.datatypes.PercentBaseContext;
023: import org.apache.fop.fo.Constants;
024: import org.apache.fop.fo.properties.Property;
025: import org.apache.fop.fo.properties.SpaceProperty;
026: import org.apache.fop.fonts.Font;
027:
028: /**
029: * Store a single Space property value in simplified form, with all
030: * Length values resolved. See section 4.3 in the specs.
031: */
032: public class SpaceVal {
033:
034: private final MinOptMax space;
035: private final boolean bConditional;
036: private final boolean bForcing;
037: private final int iPrecedence; // Numeric only, if forcing, set to 0
038:
039: /**
040: * Constructor for SpaceVal objects based on Space objects.
041: * @param spaceprop Space object to use
042: * @param context Percentage evaluation context
043: */
044: public SpaceVal(SpaceProperty spaceprop, PercentBaseContext context) {
045: space = new MinOptMax(spaceprop.getMinimum(context).getLength()
046: .getValue(context), spaceprop.getOptimum(context)
047: .getLength().getValue(context), spaceprop.getMaximum(
048: context).getLength().getValue(context));
049: bConditional = (spaceprop.getConditionality().getEnum() == Constants.EN_DISCARD);
050: Property precProp = spaceprop.getPrecedence();
051: if (precProp.getNumber() != null) {
052: iPrecedence = precProp.getNumber().intValue();
053: bForcing = false;
054: } else {
055: bForcing = (precProp.getEnum() == Constants.EN_FORCE);
056: iPrecedence = 0;
057: }
058: }
059:
060: /**
061: * Constructor for SpaceVal objects based on the full set of properties.
062: * @param space space to use
063: * @param bConditional Conditionality value
064: * @param bForcing Forcing value
065: * @param iPrecedence Precedence value
066: */
067: public SpaceVal(MinOptMax space, boolean bConditional,
068: boolean bForcing, int iPrecedence) {
069: this .space = space;
070: this .bConditional = bConditional;
071: this .bForcing = bForcing;
072: this .iPrecedence = iPrecedence;
073: }
074:
075: static public SpaceVal makeWordSpacing(Property wordSpacing,
076: SpaceVal letterSpacing, Font fs) {
077: if (wordSpacing.getEnum() == Constants.EN_NORMAL) {
078: // give word spaces the possibility to shrink by a third,
079: // and stretch by a half;
080: int spaceCharIPD = fs.getCharWidth(' ');
081: MinOptMax space = new MinOptMax(-spaceCharIPD / 3, 0,
082: spaceCharIPD / 2);
083: //TODO Adding 2 letter spaces here is not 100% correct. Spaces don't have letter spacing
084: return new SpaceVal(MinOptMax.add(space, MinOptMax
085: .multiply(letterSpacing.getSpace(), 2)), true,
086: true, 0);
087: } else {
088: return new SpaceVal(wordSpacing.getSpace(), null);
089: }
090: }
091:
092: static public SpaceVal makeLetterSpacing(Property letterSpacing) {
093: if (letterSpacing.getEnum() == Constants.EN_NORMAL) {
094: // letter spaces are set to zero (or use different values?)
095: return new SpaceVal(new MinOptMax(0), true, true, 0);
096: } else {
097: return new SpaceVal(letterSpacing.getSpace(), null);
098: }
099: }
100:
101: /**
102: * Returns the Conditionality value.
103: * @return the Conditionality value
104: */
105: public boolean isConditional() {
106: return bConditional;
107: }
108:
109: /**
110: * Returns the Forcing value.
111: * @return the Forcing value
112: */
113: public boolean isForcing() {
114: return bForcing;
115: }
116:
117: /**
118: * Returns the Precedence value.
119: * @return the Precedence value
120: */
121: public int getPrecedence() {
122: return iPrecedence;
123: }
124:
125: /**
126: * Returns the Space value.
127: * @return the Space value
128: */
129: public MinOptMax getSpace() {
130: return space;
131: }
132:
133: public String toString() {
134: return "SpaceVal: " + getSpace().toString();
135: }
136: }
|