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.

LineArea.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 org.apache.fop.area.inline.InlineArea;
  20. import org.apache.fop.fo.Constants;
  21. import java.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. /**
  25. * The line area.
  26. * This is a line area that contains inline areas.
  27. */
  28. public class LineArea extends Area {
  29. /**
  30. * this class stores information about line width and potential adjustments
  31. * that can be used in order to re-compute adjustement and / or indents when a
  32. * page-number or a page-number-citation is resolved
  33. */
  34. private final class LineAdjustingInfo implements Serializable {
  35. private int lineAlignment;
  36. private int difference;
  37. private int availableStretch;
  38. private int availableShrink;
  39. private double variationFactor;
  40. private boolean bAddedToAreaTree;
  41. private LineAdjustingInfo(int alignment, int diff,
  42. int stretch, int shrink) {
  43. lineAlignment = alignment;
  44. difference = diff;
  45. availableStretch = stretch;
  46. availableShrink = shrink;
  47. variationFactor = 1.0;
  48. bAddedToAreaTree = false;
  49. }
  50. }
  51. private LineAdjustingInfo adjustingInfo = null;
  52. // this class can contain the dominant char styling info
  53. // this means that many renderers can optimise a bit
  54. private List inlineAreas = new ArrayList();
  55. /**
  56. * default constructor:
  57. * nothing to do
  58. */
  59. public LineArea() {
  60. }
  61. /**
  62. * constructor with extra parameters:
  63. * a new LineAdjustingInfo object is created
  64. * @param alignment alignment of this line
  65. * @param diff difference between content width and line width
  66. * @param stretch the available stretch for any adjustments
  67. * @param shrink the available shrink for any adjustments
  68. */
  69. public LineArea(int alignment, int diff,
  70. int stretch, int shrink) {
  71. adjustingInfo = new LineAdjustingInfo(alignment, diff, stretch, shrink);
  72. }
  73. /**
  74. * Add a child area to this line area.
  75. *
  76. * @param childArea the inline child area to add
  77. */
  78. public void addChildArea(Area childArea) {
  79. if (childArea instanceof InlineArea) {
  80. addInlineArea((InlineArea)childArea);
  81. // set the parent area for the child area
  82. ((InlineArea) childArea).setParentArea(this);
  83. }
  84. }
  85. /**
  86. * Add an inline child area to this line area.
  87. *
  88. * @param area the inline child area to add
  89. */
  90. public void addInlineArea(InlineArea area) {
  91. inlineAreas.add(area);
  92. }
  93. /**
  94. * Get the inline child areas of this line area.
  95. *
  96. * @return the list of inline areas
  97. */
  98. public List getInlineAreas() {
  99. return inlineAreas;
  100. }
  101. /**
  102. * Get the start indent of this line area.
  103. * The start indent is used for offsetting the start of
  104. * the inline areas for alignment or other indents.
  105. *
  106. * @return the start indent value
  107. */
  108. public int getStartIndent() {
  109. if (hasTrait(Trait.START_INDENT)) {
  110. return getTraitAsInteger(Trait.START_INDENT);
  111. } else {
  112. return 0;
  113. }
  114. }
  115. /**
  116. * Updates the extents of the line area from its children.
  117. */
  118. public void updateExtentsFromChildren() {
  119. int ipd = 0;
  120. int bpd = 0;
  121. for (int i = 0, len = inlineAreas.size(); i < len; i++) {
  122. ipd = Math.max(ipd, ((InlineArea)inlineAreas.get(i)).getAllocIPD());
  123. bpd += ((InlineArea)inlineAreas.get(i)).getAllocBPD();
  124. }
  125. setIPD(ipd);
  126. setBPD(bpd);
  127. }
  128. /**
  129. * receive notification about the ipd variation of a descendant area
  130. * and perform the needed adjustment, according to the alignment;
  131. * in particular:
  132. * <ul>
  133. * <li>left-aligned text needs no adjustement;</li>
  134. * <li>right-aligned text and centered text are handled locally,
  135. * adjusting the indent of this LineArea;</li>
  136. * <li>justified text requires a more complex adjustment, as the
  137. * variation factor computed on the basis of the total
  138. * stretch and shrink of the line must be applied in every
  139. * descendant leaf areas (text areas and leader areas).</li>
  140. * </ul>
  141. * @param ipdVariation the difference between old and new ipd
  142. */
  143. public void handleIPDVariation(int ipdVariation) {
  144. switch (adjustingInfo.lineAlignment) {
  145. case Constants.EN_START:
  146. // nothing to do in this case
  147. break;
  148. case Constants.EN_CENTER:
  149. // re-compute indent
  150. addTrait(Trait.START_INDENT, new Integer(getStartIndent() - ipdVariation / 2));
  151. break;
  152. case Constants.EN_END:
  153. // re-compute indent
  154. addTrait(Trait.START_INDENT, new Integer(getStartIndent() - ipdVariation));
  155. break;
  156. case Constants.EN_JUSTIFY:
  157. // compute variation factor
  158. adjustingInfo.variationFactor *= (float) (adjustingInfo.difference - ipdVariation)
  159. / adjustingInfo.difference;
  160. adjustingInfo.difference -= ipdVariation;
  161. // if the LineArea has already been added to the area tree,
  162. // call finalize(); otherwise, wait for the LineLM to call it
  163. if (adjustingInfo.bAddedToAreaTree) {
  164. finalise();
  165. }
  166. break;
  167. default:
  168. throw new RuntimeException();
  169. }
  170. }
  171. /**
  172. * apply the variation factor to all descendant areas
  173. * and destroy the AdjustingInfo object if there are
  174. * no UnresolvedAreas left
  175. */
  176. public void finalise() {
  177. if (adjustingInfo.lineAlignment == Constants.EN_JUSTIFY) {
  178. // justified line: apply the variation factor
  179. boolean bUnresolvedAreasPresent = false;
  180. // recursively apply variation factor to descendant areas
  181. for (int i = 0, len = inlineAreas.size(); i < len; i++) {
  182. bUnresolvedAreasPresent |= ((InlineArea) inlineAreas.get(i))
  183. .applyVariationFactor(adjustingInfo.variationFactor,
  184. adjustingInfo.availableStretch,
  185. adjustingInfo.availableShrink);
  186. }
  187. if (!bUnresolvedAreasPresent) {
  188. // there are no more UnresolvedAreas:
  189. // destroy the AdjustingInfo instance
  190. adjustingInfo = null;
  191. } else {
  192. // this method will be called again later:
  193. // the first time, it is called by the LineLM,
  194. // afterwards it must be called by the LineArea itself
  195. if (!adjustingInfo.bAddedToAreaTree) {
  196. adjustingInfo.bAddedToAreaTree = true;
  197. }
  198. // reset the variation factor
  199. adjustingInfo.variationFactor = 1.0;
  200. }
  201. } else {
  202. // the line is not justified: the ipd variation has already
  203. // been handled, modifying the line indent
  204. }
  205. }
  206. }