您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LineArea.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.List;
  22. import org.apache.fop.area.inline.InlineArea;
  23. import static org.apache.fop.fo.Constants.EN_CENTER;
  24. import static org.apache.fop.fo.Constants.EN_END;
  25. import static org.apache.fop.fo.Constants.EN_JUSTIFY;
  26. import static org.apache.fop.fo.Constants.EN_START;
  27. /**
  28. * The line area.
  29. * This is a line area that contains inline areas.
  30. */
  31. public class LineArea extends Area {
  32. private static final long serialVersionUID = 7670235908329290684L;
  33. /**
  34. * this class stores information about line width and potential adjustments
  35. * that can be used in order to re-compute adjustement and / or indents when a
  36. * page-number or a page-number-citation is resolved
  37. */
  38. // @SuppressFBWarnings("SE_INNER_CLASS")
  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 (InlineArea ia : (Iterable<InlineArea>) inlineAreas) {
  115. Area pa = ia.getParentArea();
  116. if (pa == null) {
  117. ia.setParentArea(this);
  118. } else {
  119. assert pa == this;
  120. }
  121. }
  122. this.inlineAreas = inlineAreas;
  123. }
  124. /**
  125. * Get the inline child areas of this line area.
  126. *
  127. * @return the list of inline areas
  128. */
  129. public List getInlineAreas() {
  130. return inlineAreas;
  131. }
  132. /**
  133. * Get the start indent of this line area.
  134. * The start indent is used for offsetting the start of
  135. * the inline areas for alignment or other indents.
  136. *
  137. * @return the start indent value
  138. */
  139. public int getStartIndent() {
  140. if (hasTrait(Trait.START_INDENT)) {
  141. return getTraitAsInteger(Trait.START_INDENT);
  142. } else {
  143. return 0;
  144. }
  145. }
  146. /**
  147. * Get the end indent of this line area.
  148. * The end indent is used for offsetting the end of
  149. * the inline areas for alignment or other indents.
  150. *
  151. * @return the end indent value
  152. */
  153. public int getEndIndent() {
  154. if (hasTrait(Trait.END_INDENT)) {
  155. return getTraitAsInteger(Trait.END_INDENT);
  156. } else {
  157. return 0;
  158. }
  159. }
  160. /**
  161. * Updates the extents of the line area from its children.
  162. */
  163. public void updateExtentsFromChildren() {
  164. int ipd = 0;
  165. int bpd = 0;
  166. for (InlineArea inlineArea : inlineAreas) {
  167. ipd = Math.max(ipd, inlineArea.getAllocIPD());
  168. bpd += inlineArea.getAllocBPD();
  169. }
  170. setIPD(ipd);
  171. setBPD(bpd);
  172. }
  173. /**
  174. * receive notification about the ipd variation of a descendant area
  175. * and perform the needed adjustment, according to the alignment;
  176. * in particular:
  177. * <ul>
  178. * <li>left-aligned text needs no adjustement;</li>
  179. * <li>right-aligned text and centered text are handled locally,
  180. * adjusting the indent of this LineArea;</li>
  181. * <li>justified text requires a more complex adjustment, as the
  182. * variation factor computed on the basis of the total
  183. * stretch and shrink of the line must be applied in every
  184. * descendant leaf areas (text areas and leader areas).</li>
  185. * </ul>
  186. * @param ipdVariation the difference between old and new ipd
  187. */
  188. public void handleIPDVariation(int ipdVariation) {
  189. int si = getStartIndent();
  190. int ei = getEndIndent();
  191. switch (adjustingInfo.lineAlignment) {
  192. case EN_START:
  193. // adjust end indent
  194. addTrait(Trait.END_INDENT, ei - ipdVariation);
  195. break;
  196. case EN_CENTER:
  197. // adjust start and end indents
  198. addTrait(Trait.START_INDENT, si - ipdVariation / 2);
  199. addTrait(Trait.END_INDENT, ei - ipdVariation / 2);
  200. break;
  201. case EN_END:
  202. // adjust start indent
  203. addTrait(Trait.START_INDENT, si - ipdVariation);
  204. break;
  205. case EN_JUSTIFY:
  206. // compute variation factor
  207. adjustingInfo.variationFactor *= (float) (adjustingInfo.difference - ipdVariation)
  208. / adjustingInfo.difference;
  209. adjustingInfo.difference -= ipdVariation;
  210. // if the LineArea has already been added to the area tree,
  211. // call finalize(); otherwise, wait for the LineLM to call it
  212. if (adjustingInfo.bAddedToAreaTree) {
  213. finish();
  214. }
  215. break;
  216. default:
  217. throw new RuntimeException();
  218. }
  219. }
  220. /**
  221. * apply the variation factor to all descendant areas
  222. * and destroy the AdjustingInfo object if there are
  223. * no UnresolvedAreas left
  224. */
  225. public void finish() {
  226. if (adjustingInfo.lineAlignment == EN_JUSTIFY) {
  227. if (log.isTraceEnabled()) {
  228. log.trace("Applying variation factor to justified line: " + adjustingInfo);
  229. }
  230. // justified line: apply the variation factor
  231. boolean bUnresolvedAreasPresent = false;
  232. // recursively apply variation factor to descendant areas
  233. for (InlineArea inlineArea : inlineAreas) {
  234. bUnresolvedAreasPresent |= inlineArea
  235. .applyVariationFactor(adjustingInfo.variationFactor,
  236. adjustingInfo.availableStretch,
  237. adjustingInfo.availableShrink);
  238. }
  239. if (!bUnresolvedAreasPresent) {
  240. // there are no more UnresolvedAreas:
  241. // destroy the AdjustingInfo instance
  242. adjustingInfo = null;
  243. } else {
  244. // this method will be called again later:
  245. // the first time, it is called by the LineLM,
  246. // afterwards it must be called by the LineArea itself
  247. if (!adjustingInfo.bAddedToAreaTree) {
  248. adjustingInfo.bAddedToAreaTree = true;
  249. }
  250. // reset the variation factor
  251. adjustingInfo.variationFactor = 1.0;
  252. }
  253. } else {
  254. // the line is not justified: the ipd variation has already
  255. // been handled, modifying the line indent
  256. }
  257. }
  258. public int getEffectiveIPD() {
  259. int maxIPD = 0;
  260. if (inlineAreas != null) {
  261. for (Area area : inlineAreas) {
  262. int effectiveIPD = area.getEffectiveIPD();
  263. if (effectiveIPD > maxIPD) {
  264. maxIPD = effectiveIPD;
  265. }
  266. }
  267. }
  268. return maxIPD;
  269. }
  270. }