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: RtfSpaceSplitter.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.render.rtf.rtflib.rtfdoc;
021:
022: /**
023: * This class splits block attributes into space-before attribute, space-after
024: * attribute and common attributes.
025: */
026: public class RtfSpaceSplitter {
027:
028: /** Common attributes for all text. */
029: private RtfAttributes commonAttributes;
030:
031: /** Space-before attributes of a block. */
032: private int spaceBefore;
033:
034: /** Space-after attributes of a block. */
035: private int spaceAfter;
036:
037: /** Indicate that we can update candidate for space-before. */
038: private boolean updatingSpaceBefore;
039:
040: /** Candidate for adding space-before. */
041: private RtfAttributes spaceBeforeCandidate;
042:
043: /** Candidate for adding space-before. */
044: private RtfAttributes spaceAfterCandidate;
045:
046: /**
047: * Create RtfSpaceSplitter with given RtfAttributes.
048: *
049: * @param attrs RtfAttributes for splitting
050: * @param previousSpace integer, representing accumulated spacing
051: */
052: public RtfSpaceSplitter(RtfAttributes attrs, int previousSpace) {
053: commonAttributes = attrs;
054: updatingSpaceBefore = true;
055: spaceBeforeCandidate = null;
056: spaceAfterCandidate = null;
057:
058: spaceBefore = split(RtfText.SPACE_BEFORE) + previousSpace;
059: spaceAfter = split(RtfText.SPACE_AFTER);
060: }
061:
062: /**
063: * Remove attributes with name <code>key</code> from
064: * <code>commonAttributes</code> and return it as int.
065: *
066: * @param key attributes name to extract
067: * @return integer, representing value of extracted attributes
068: */
069: public int split(String key) {
070: Integer i = (Integer) commonAttributes.getValue(key);
071: if (i == null) {
072: i = new Integer(0);
073: }
074:
075: commonAttributes.unset(key);
076: return i.intValue();
077: }
078:
079: /** @return attributes, applicable to whole block. */
080: public RtfAttributes getCommonAttributes() {
081: return commonAttributes;
082: }
083:
084: /** @return space-before value. */
085: public int getSpaceBefore() {
086: return spaceBefore;
087: }
088:
089: /**
090: * Sets a candidate for space-before property.
091: *
092: * @param candidate instance of <code>RtfAttributes</code>, considered as
093: * a candidate for space-before adding
094: */
095: public void setSpaceBeforeCandidate(RtfAttributes candidate) {
096: if (updatingSpaceBefore) {
097: this .spaceBeforeCandidate = candidate;
098: }
099: }
100:
101: /**
102: * Sets a candidate for space-after property.
103: *
104: * @param candidate instance of <code>RtfAttributes</code>, considered as
105: * a candidate for space-after adding
106: */
107: public void setSpaceAfterCandidate(RtfAttributes candidate) {
108: this .spaceAfterCandidate = candidate;
109: }
110:
111: /** @return true, if candidate for space-before is set. */
112: public boolean isBeforeCadidateSet() {
113: return spaceBeforeCandidate != null;
114: }
115:
116: /** @return true, if candidate for space-after is set. */
117: public boolean isAfterCadidateSet() {
118: return spaceAfterCandidate != null;
119: }
120:
121: /**
122: * Stops updating candidates for space-before attribute.
123: */
124: public void stopUpdatingSpaceBefore() {
125: updatingSpaceBefore = false;
126: }
127:
128: /**
129: * Adds corresponding attributes to their candidates.
130: *
131: * @return integer, representing value of space-before/space-after
132: * attributes, that can't be added anywhere (i.e. these attributes
133: * hasn't their candidates)
134: */
135: public int flush() {
136: int accumulatingSpace = 0;
137: if (!isBeforeCadidateSet()) {
138: accumulatingSpace += spaceBefore;
139: } else {
140: spaceBeforeCandidate.addIntegerValue(spaceBefore,
141: RtfText.SPACE_BEFORE);
142: }
143:
144: if (!isAfterCadidateSet()) {
145: accumulatingSpace += spaceAfter;
146: } else {
147: spaceAfterCandidate.addIntegerValue(spaceAfter,
148: RtfText.SPACE_AFTER);
149: }
150:
151: return accumulatingSpace;
152: }
153: }
|