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.

RtfSpaceManager.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. import java.util.LinkedList;
  20. /**
  21. * This class is responsible for saving space-before/space-after attributes
  22. * history and adding spacing to established candidates (i.e. attributes) or
  23. * accumulation spacing in case of candidate absence.
  24. */
  25. public class RtfSpaceManager {
  26. /** Stack for saving rtf block-level attributes. */
  27. private LinkedList blockAttributes = new LinkedList();
  28. /** Stack for saving rtf inline-level attributes. */
  29. private LinkedList inlineAttributes = new LinkedList();
  30. /**
  31. * Keeps value of accumulated space in twips. For example if block has
  32. * nonzero space-before or space-after properties and has no plain text
  33. * inside, then the next block should has increased value of space-before
  34. * property.
  35. */
  36. private int accumulatedSpace;
  37. /**
  38. * Construct a newly allocated <code>RtfSpaceManager</code> object.
  39. */
  40. public RtfSpaceManager() {
  41. }
  42. /**
  43. * Iterates block-level stack (i.e. all open blocks) and stops updating
  44. * candidate for adding space-before/space-after attribute in case of
  45. * candidate presence.
  46. */
  47. public void stopUpdatingSpaceBefore() {
  48. for (Object blockAttribute : blockAttributes) {
  49. RtfSpaceSplitter splitter = (RtfSpaceSplitter) blockAttribute;
  50. if (splitter.isBeforeCadidateSet()) {
  51. splitter.stopUpdatingSpaceBefore();
  52. }
  53. }
  54. }
  55. /**
  56. * Set attributes as candidate for space attributes inheritance.
  57. *
  58. * @param attrs attributes to set
  59. */
  60. public void setCandidate(RtfAttributes attrs) {
  61. for (Object blockAttribute : blockAttributes) {
  62. RtfSpaceSplitter splitter = (RtfSpaceSplitter) blockAttribute;
  63. splitter.setSpaceBeforeCandidate(attrs);
  64. splitter.setSpaceAfterCandidate(attrs);
  65. }
  66. }
  67. /**
  68. * Builds RtfSpaceSplitter on <code>attrs</code> and adds it to the
  69. * block-level stack.
  70. *
  71. * @param attrs RtfAttribute to add
  72. * @return instance of RtfSpaceSplitter
  73. */
  74. public RtfSpaceSplitter pushRtfSpaceSplitter(RtfAttributes attrs) {
  75. RtfSpaceSplitter splitter;
  76. splitter = new RtfSpaceSplitter(attrs, accumulatedSpace);
  77. // set accumulatedSpace to 0, because now accumulatedSpace used
  78. // in splitter
  79. accumulatedSpace = 0;
  80. blockAttributes.addLast(splitter);
  81. return splitter;
  82. }
  83. /**
  84. * Removes RtfSpaceSplitter from top of block-level stack.
  85. */
  86. public void popRtfSpaceSplitter() {
  87. if (!blockAttributes.isEmpty()) {
  88. RtfSpaceSplitter splitter;
  89. splitter = (RtfSpaceSplitter) blockAttributes.removeLast();
  90. accumulatedSpace += splitter.flush();
  91. }
  92. }
  93. /**
  94. * Pushes inline attributes to inline-level stack.
  95. *
  96. * @param attrs attributes to add
  97. */
  98. public void pushInlineAttributes(RtfAttributes attrs) {
  99. inlineAttributes.addLast(attrs);
  100. }
  101. /**
  102. * Pops inline attributes from inline-level stack.
  103. */
  104. public void popInlineAttributes() {
  105. if (!inlineAttributes.isEmpty()) {
  106. inlineAttributes.removeLast();
  107. }
  108. }
  109. /**
  110. * Peeks at inline-level attribute stack.
  111. *
  112. * @return RtfAttributes from top of inline-level stack
  113. */
  114. public RtfAttributes getLastInlineAttribute() {
  115. return (RtfAttributes) inlineAttributes.getLast();
  116. }
  117. }