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.

InlineParent.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.inline;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import org.apache.fop.area.Area;
  23. /**
  24. * Inline parent area.
  25. * This is an inline area that can have other inlines as children.
  26. */
  27. public class InlineParent extends InlineArea {
  28. private static final long serialVersionUID = -3047168298770354813L;
  29. /**
  30. * The list of inline areas added to this inline parent.
  31. */
  32. protected List<InlineArea> inlines = new ArrayList<InlineArea>();
  33. /** Controls whether the IPD is automatically adjusted based on the area's children. */
  34. protected transient boolean autoSize;
  35. /** The offset of the <q>beforest</q> child area of this area. */
  36. protected int minChildOffset;
  37. /**
  38. * The offset of the <q>afterest</q> child area of this area. Offset from the
  39. * before-edge of this area's content-rectangle and the after-edge of the child area's
  40. * allocation-rectangle.
  41. */
  42. private int maxAfterEdge;
  43. @Override
  44. public void addChildArea(Area c) {
  45. assert c instanceof InlineArea;
  46. if (inlines.size() == 0) {
  47. autoSize = (getIPD() == 0);
  48. }
  49. InlineArea childArea = (InlineArea) c;
  50. inlines.add(childArea);
  51. // set the parent area for the child area
  52. childArea.setParentArea(this);
  53. if (autoSize) {
  54. increaseIPD(childArea.getAllocIPD());
  55. }
  56. updateLevel(childArea.getBidiLevel());
  57. int childOffset = childArea.getVirtualOffset();
  58. minChildOffset = Math.min(minChildOffset, childOffset);
  59. maxAfterEdge = Math.max(maxAfterEdge, childOffset + childArea.getVirtualBPD());
  60. }
  61. @Override
  62. int getVirtualOffset() {
  63. return getBlockProgressionOffset() + minChildOffset;
  64. }
  65. @Override
  66. int getVirtualBPD() {
  67. return maxAfterEdge - minChildOffset;
  68. }
  69. /**
  70. * Get the child areas for this inline parent.
  71. *
  72. * @return the list of child areas
  73. */
  74. public List<InlineArea> getChildAreas() {
  75. return inlines;
  76. }
  77. /**
  78. * recursively apply the variation factor to all descendant areas
  79. * @param variationFactor the variation factor that must be applied to adjustments
  80. * @param lineStretch the total stretch of the line
  81. * @param lineShrink the total shrink of the line
  82. * @return true if there is an UnresolvedArea descendant
  83. */
  84. @Override
  85. public boolean applyVariationFactor(double variationFactor,
  86. int lineStretch, int lineShrink) {
  87. boolean hasUnresolvedAreas = false;
  88. int cumulativeIPD = 0;
  89. // recursively apply variation factor to descendant areas
  90. for (int i = 0, len = inlines.size(); i < len; i++) {
  91. InlineArea inline = inlines.get(i);
  92. hasUnresolvedAreas |= inline.applyVariationFactor(
  93. variationFactor, lineStretch, lineShrink);
  94. cumulativeIPD += inline.getIPD(); //Update this area's IPD based on changes to children
  95. }
  96. setIPD(cumulativeIPD);
  97. return hasUnresolvedAreas;
  98. }
  99. @Override
  100. public List collectInlineRuns(List runs) {
  101. for (Iterator<InlineArea> it = getChildAreas().iterator(); it.hasNext();) {
  102. InlineArea ia = it.next();
  103. runs = ia.collectInlineRuns(runs);
  104. }
  105. return runs;
  106. }
  107. /**
  108. * Reset bidirectionality level of all children to default (-1),
  109. * signalling that they will inherit the level of their parent text area.
  110. */
  111. public void resetChildrenLevel() {
  112. for (Iterator it = inlines.iterator(); it.hasNext();) {
  113. ((InlineArea) it.next()) .resetBidiLevel();
  114. }
  115. }
  116. private void updateLevel(int newLevel) {
  117. if (newLevel >= 0) {
  118. int curLevel = getBidiLevel();
  119. if (curLevel >= 0) {
  120. if (newLevel < curLevel) {
  121. setBidiLevel(newLevel);
  122. }
  123. } else {
  124. setBidiLevel(newLevel);
  125. }
  126. }
  127. }
  128. }