Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LineArea.java 8.1KB

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