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.

RtfSpaceSplitter.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.render.rtf.rtflib.rtfdoc;
  19. /**
  20. * This class splits block attributes into space-before attribute, space-after
  21. * attribute and common attributes.
  22. */
  23. public class RtfSpaceSplitter {
  24. /** Common attributes for all text. */
  25. private RtfAttributes commonAttributes;
  26. /** Space-before attributes of a block. */
  27. private int spaceBefore;
  28. /** Space-after attributes of a block. */
  29. private int spaceAfter;
  30. /** Indicate that we can update candidate for space-before. */
  31. private boolean updatingSpaceBefore;
  32. /** Candidate for adding space-before. */
  33. private RtfAttributes spaceBeforeCandidate;
  34. /** Candidate for adding space-before. */
  35. private RtfAttributes spaceAfterCandidate;
  36. /**
  37. * Create RtfSpaceSplitter with given RtfAttributes.
  38. *
  39. * @param attrs RtfAttributes for splitting
  40. * @param previousSpace integer, representing accumulated spacing
  41. */
  42. public RtfSpaceSplitter(RtfAttributes attrs, int previousSpace) {
  43. commonAttributes = attrs;
  44. updatingSpaceBefore = true;
  45. spaceBeforeCandidate = null;
  46. spaceAfterCandidate = null;
  47. spaceBefore = split(RtfText.SPACE_BEFORE) + previousSpace;
  48. spaceAfter = split(RtfText.SPACE_AFTER);
  49. }
  50. /**
  51. * Remove attributes with name <code>key</code> from
  52. * <code>commonAttributes</code> and return it as int.
  53. *
  54. * @param key attributes name to extract
  55. * @return integer, representing value of extracted attributes
  56. */
  57. public int split(String key) {
  58. Integer i = (Integer) commonAttributes.getValue(key);
  59. if (i == null) {
  60. i = 0;
  61. }
  62. commonAttributes.unset(key);
  63. return i.intValue();
  64. }
  65. /** @return attributes, applicable to whole block. */
  66. public RtfAttributes getCommonAttributes() {
  67. return commonAttributes;
  68. }
  69. /** @return space-before value. */
  70. public int getSpaceBefore() {
  71. return spaceBefore;
  72. }
  73. /**
  74. * Sets a candidate for space-before property.
  75. *
  76. * @param candidate instance of <code>RtfAttributes</code>, considered as
  77. * a candidate for space-before adding
  78. */
  79. public void setSpaceBeforeCandidate(RtfAttributes candidate) {
  80. if (updatingSpaceBefore) {
  81. this.spaceBeforeCandidate = candidate;
  82. }
  83. }
  84. /**
  85. * Sets a candidate for space-after property.
  86. *
  87. * @param candidate instance of <code>RtfAttributes</code>, considered as
  88. * a candidate for space-after adding
  89. */
  90. public void setSpaceAfterCandidate(RtfAttributes candidate) {
  91. this.spaceAfterCandidate = candidate;
  92. }
  93. /** @return true, if candidate for space-before is set. */
  94. public boolean isBeforeCadidateSet() {
  95. return spaceBeforeCandidate != null;
  96. }
  97. /** @return true, if candidate for space-after is set. */
  98. public boolean isAfterCadidateSet() {
  99. return spaceAfterCandidate != null;
  100. }
  101. /**
  102. * Stops updating candidates for space-before attribute.
  103. */
  104. public void stopUpdatingSpaceBefore() {
  105. updatingSpaceBefore = false;
  106. }
  107. /**
  108. * Adds corresponding attributes to their candidates.
  109. *
  110. * @return integer, representing value of space-before/space-after
  111. * attributes, that can't be added anywhere (i.e. these attributes
  112. * hasn't their candidates)
  113. */
  114. public int flush() {
  115. int accumulatingSpace = 0;
  116. if (!isBeforeCadidateSet()) {
  117. accumulatingSpace += spaceBefore;
  118. } else {
  119. spaceBeforeCandidate.addIntegerValue(spaceBefore,
  120. RtfText.SPACE_BEFORE);
  121. }
  122. if (!isAfterCadidateSet()) {
  123. accumulatingSpace += spaceAfter;
  124. } else {
  125. spaceAfterCandidate.addIntegerValue(spaceAfter,
  126. RtfText.SPACE_AFTER);
  127. }
  128. return accumulatingSpace;
  129. }
  130. }