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

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