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 8.1KB

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