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.0KB

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.area;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;
  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 spanAreas = new java.util.ArrayList();
  32. private boolean isEmpty = true;
  33. /**
  34. * Constructor
  35. *
  36. * @param parent the body region this reference area is placed in.
  37. */
  38. public MainReference(BodyRegion parent) {
  39. this.parent = parent;
  40. addTrait(Trait.IS_REFERENCE_AREA, Boolean.TRUE);
  41. }
  42. /**
  43. * Add a span area to this area.
  44. *
  45. * @param spanAll whether to make a single-column span
  46. * @return the created span area.
  47. */
  48. public Span createSpan(boolean spanAll) {
  49. if (spanAreas.size() > 0 && getCurrentSpan().isEmpty()) {
  50. //Remove the current one if it is empty
  51. spanAreas.remove(spanAreas.size() - 1);
  52. }
  53. RegionViewport rv = parent.getRegionViewport();
  54. int ipdWidth = (int) parent.getIPD()
  55. - rv.getBorderAndPaddingWidthStart() - rv.getBorderAndPaddingWidthEnd();
  56. Span newSpan = new Span(((spanAll) ? 1 : getColumnCount()),
  57. getColumnGap(), ipdWidth);
  58. spanAreas.add(newSpan);
  59. return getCurrentSpan();
  60. }
  61. /**
  62. * Get the span areas from this area.
  63. *
  64. * @return the list of span areas
  65. */
  66. public List getSpans() {
  67. return spanAreas;
  68. }
  69. /**
  70. * Do not use. Used to handle special page-master for last page: transfer the content
  71. * that had already been added to a normal page to this main reference for the last
  72. * page. TODO this is hacky.
  73. *
  74. * @param spans content already laid out
  75. */
  76. public void setSpans(List spans) {
  77. spanAreas = new ArrayList(spans);
  78. }
  79. /**
  80. * Get the span area currently being filled (i.e., the last span created).
  81. * @return the active span.
  82. */
  83. public Span getCurrentSpan() {
  84. return (Span) spanAreas.get(spanAreas.size() - 1);
  85. }
  86. /**
  87. * Indicates whether any child areas have been added to this reference area.
  88. *
  89. * This is achieved by looping through each span.
  90. * @return true if no child areas have been added yet.
  91. */
  92. public boolean isEmpty() {
  93. if (isEmpty) {
  94. boolean nonEmptyFound = false;
  95. if (spanAreas != null) {
  96. for (Iterator spaniter = spanAreas.iterator(); spaniter.hasNext();) {
  97. Span spanArea = (Span) spaniter.next();
  98. nonEmptyFound |= !spanArea.isEmpty();
  99. }
  100. }
  101. isEmpty = !nonEmptyFound;
  102. }
  103. return isEmpty;
  104. }
  105. /** @return the number of columns */
  106. public int getColumnCount() {
  107. return parent.getColumnCount();
  108. }
  109. /** @return the column gap in millipoints */
  110. public int getColumnGap() {
  111. return parent.getColumnGap();
  112. }
  113. }