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.

MainReference.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.area;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import org.apache.fop.traits.WritingModeTraitsGetter;
  22. /**
  23. * The main-reference-area generated by an fo:region-body
  24. * This object holds one or more span-reference-areas (block-areas
  25. * stacked in the block progression direction)
  26. * See fo:region-body definition in the XSL Rec for more information.
  27. */
  28. public class MainReference extends Area {
  29. private static final long serialVersionUID = 7635126485620012448L;
  30. private BodyRegion parent;
  31. private List<Span> spanAreas = new java.util.ArrayList<Span>();
  32. private boolean isEmpty = true;
  33. private transient WritingModeTraitsGetter wmtg;
  34. /**
  35. * Constructor
  36. *
  37. * @param parent the body region this reference area is placed in.
  38. */
  39. public MainReference(BodyRegion parent) {
  40. this.parent = parent;
  41. addTrait(Trait.IS_REFERENCE_AREA, Boolean.TRUE);
  42. }
  43. /**
  44. * Add a span area to this area.
  45. *
  46. * @param spanAll whether to make a single-column span
  47. * @return the created span area.
  48. */
  49. public Span createSpan(boolean spanAll) {
  50. if (spanAreas.size() > 0 && getCurrentSpan().isEmpty()) {
  51. //Remove the current one if it is empty
  52. spanAreas.remove(spanAreas.size() - 1);
  53. }
  54. Span newSpan = new Span(((spanAll) ? 1 : getColumnCount()),
  55. getColumnGap(), parent.getContentIPD());
  56. spanAreas.add(newSpan);
  57. if (wmtg != null) {
  58. newSpan.setWritingModeTraits(wmtg);
  59. }
  60. return getCurrentSpan();
  61. }
  62. /**
  63. * Get the span areas from this area.
  64. *
  65. * @return the list of span areas
  66. */
  67. public List<Span> getSpans() {
  68. return spanAreas;
  69. }
  70. /**
  71. * Do not use. Used to handle special page-master for last page: transfer the content
  72. * that had already been added to a normal page to this main reference for the last
  73. * page. TODO this is hacky.
  74. *
  75. * @param spans content already laid out
  76. */
  77. public void setSpans(List<Span> spans) {
  78. spanAreas = new ArrayList<Span>(spans);
  79. }
  80. /**
  81. * Get the span area currently being filled (i.e., the last span created).
  82. * @return the active span.
  83. */
  84. public Span getCurrentSpan() {
  85. return spanAreas.get(spanAreas.size() - 1);
  86. }
  87. /**
  88. * Indicates whether any child areas have been added to this reference area.
  89. *
  90. * This is achieved by looping through each span.
  91. * @return true if no child areas have been added yet.
  92. */
  93. public boolean isEmpty() {
  94. if (isEmpty && spanAreas != null) {
  95. for (Span spanArea : spanAreas) {
  96. if (!spanArea.isEmpty()) {
  97. isEmpty = false;
  98. break;
  99. }
  100. }
  101. }
  102. return isEmpty;
  103. }
  104. /** @return the number of columns */
  105. public int getColumnCount() {
  106. return parent.getColumnCount();
  107. }
  108. /** @return the column gap in millipoints */
  109. public int getColumnGap() {
  110. return parent.getColumnGap();
  111. }
  112. /**
  113. * Sets the writing mode traits for the spans of this main
  114. * reference area.
  115. * @param wmtg a WM traits getter
  116. */
  117. public void setWritingModeTraits(WritingModeTraitsGetter wmtg) {
  118. this.wmtg = wmtg;
  119. for (Span s : getSpans()) {
  120. s.setWritingModeTraits(wmtg);
  121. }
  122. }
  123. }