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.

BlockLayoutManager.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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.layoutmgr;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import java.util.ListIterator;
  22. import java.util.Stack;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.apache.fop.area.Area;
  26. import org.apache.fop.area.Block;
  27. import org.apache.fop.area.LineArea;
  28. import org.apache.fop.datatypes.Length;
  29. import org.apache.fop.fo.properties.KeepProperty;
  30. import org.apache.fop.fonts.Font;
  31. import org.apache.fop.fonts.FontInfo;
  32. import org.apache.fop.fonts.FontTriplet;
  33. import org.apache.fop.layoutmgr.inline.InlineLevelLayoutManager;
  34. import org.apache.fop.layoutmgr.inline.LineLayoutManager;
  35. import org.apache.fop.traits.MinOptMax;
  36. import org.apache.fop.traits.SpaceVal;
  37. /**
  38. * LayoutManager for a block FO.
  39. */
  40. public class BlockLayoutManager extends BlockStackingLayoutManager
  41. implements ConditionalElementListener {
  42. /**
  43. * logging instance
  44. */
  45. private static Log log = LogFactory.getLog(BlockLayoutManager.class);
  46. private Block curBlockArea;
  47. /** Iterator over the child layout managers. */
  48. protected ListIterator proxyLMiter;
  49. private int lead = 12000;
  50. private Length lineHeight;
  51. private int follow = 2000;
  52. //private int middleShift = 0;
  53. private boolean discardBorderBefore;
  54. private boolean discardBorderAfter;
  55. private boolean discardPaddingBefore;
  56. private boolean discardPaddingAfter;
  57. private MinOptMax effSpaceBefore;
  58. private MinOptMax effSpaceAfter;
  59. /**
  60. * Creates a new BlockLayoutManager.
  61. * @param inBlock the block FO object to create the layout manager for.
  62. */
  63. public BlockLayoutManager(org.apache.fop.fo.flow.Block inBlock) {
  64. super(inBlock);
  65. proxyLMiter = new ProxyLMiter();
  66. }
  67. /** {@inheritDoc} */
  68. public void initialize() {
  69. super.initialize();
  70. FontInfo fi = getBlockFO().getFOEventHandler().getFontInfo();
  71. FontTriplet[] fontkeys = getBlockFO().getCommonFont().getFontState(fi);
  72. Font initFont = fi.getFontInstance(fontkeys[0],
  73. getBlockFO().getCommonFont().fontSize.getValue(this));
  74. lead = initFont.getAscender();
  75. follow = -initFont.getDescender();
  76. //middleShift = -fs.getXHeight() / 2;
  77. lineHeight = getBlockFO().getLineHeight().getOptimum(this).getLength();
  78. startIndent = getBlockFO().getCommonMarginBlock().startIndent.getValue(this);
  79. endIndent = getBlockFO().getCommonMarginBlock().endIndent.getValue(this);
  80. foSpaceBefore = new SpaceVal(getBlockFO().getCommonMarginBlock().spaceBefore, this)
  81. .getSpace();
  82. foSpaceAfter = new SpaceVal(getBlockFO().getCommonMarginBlock().spaceAfter, this)
  83. .getSpace();
  84. bpUnit = 0; // non-standard extension
  85. if (bpUnit == 0) {
  86. // use optimum space values
  87. adjustedSpaceBefore = getBlockFO().getCommonMarginBlock().spaceBefore.getSpace()
  88. .getOptimum(this).getLength().getValue(this);
  89. adjustedSpaceAfter = getBlockFO().getCommonMarginBlock().spaceAfter.getSpace()
  90. .getOptimum(this).getLength().getValue(this);
  91. } else {
  92. // use minimum space values
  93. adjustedSpaceBefore = getBlockFO().getCommonMarginBlock().spaceBefore.getSpace()
  94. .getMinimum(this).getLength().getValue(this);
  95. adjustedSpaceAfter = getBlockFO().getCommonMarginBlock().spaceAfter.getSpace()
  96. .getMinimum(this).getLength().getValue(this);
  97. }
  98. }
  99. /** {@inheritDoc} */
  100. public List getNextKnuthElements(LayoutContext context, int alignment) {
  101. return getNextKnuthElements(context, alignment, null, null, null);
  102. }
  103. /** {@inheritDoc} */
  104. public List getNextKnuthElements(LayoutContext context, int alignment, Stack lmStack,
  105. Position restartPosition, LayoutManager restartAtLM) {
  106. resetSpaces();
  107. if (lmStack == null) {
  108. return super.getNextKnuthElements(context, alignment);
  109. } else {
  110. return super.getNextKnuthElements(context, alignment, lmStack, restartPosition,
  111. restartAtLM);
  112. }
  113. }
  114. private void resetSpaces() {
  115. this.discardBorderBefore = false;
  116. this.discardBorderAfter = false;
  117. this.discardPaddingBefore = false;
  118. this.discardPaddingAfter = false;
  119. this.effSpaceBefore = null;
  120. this.effSpaceAfter = null;
  121. }
  122. /**
  123. * Proxy iterator for Block LM.
  124. * This iterator creates and holds the complete list
  125. * of child LMs.
  126. * It uses fobjIter as its base iterator.
  127. * Block LM's createNextChildLMs uses this iterator
  128. * as its base iterator.
  129. */
  130. protected class ProxyLMiter extends LMiter {
  131. /**
  132. * Constructs a proxy iterator for Block LM.
  133. */
  134. public ProxyLMiter() {
  135. super(BlockLayoutManager.this);
  136. listLMs = new java.util.ArrayList(10);
  137. }
  138. /**
  139. * @return true if there are more child lms
  140. */
  141. public boolean hasNext() {
  142. return (curPos < listLMs.size()) || createNextChildLMs(curPos);
  143. }
  144. /**
  145. * @param pos ...
  146. * @return true if new child lms were added
  147. */
  148. protected boolean createNextChildLMs(int pos) {
  149. List newLMs = createChildLMs(pos + 1 - listLMs.size());
  150. if (newLMs != null) {
  151. listLMs.addAll(newLMs);
  152. }
  153. return pos < listLMs.size();
  154. }
  155. }
  156. /**
  157. * {@inheritDoc}
  158. */
  159. public boolean createNextChildLMs(int pos) {
  160. while (proxyLMiter.hasNext()) {
  161. LayoutManager lm = (LayoutManager) proxyLMiter.next();
  162. if (lm instanceof InlineLevelLayoutManager) {
  163. LineLayoutManager lineLM = createLineManager(lm);
  164. addChildLM(lineLM);
  165. } else {
  166. addChildLM(lm);
  167. }
  168. if (pos < childLMs.size()) {
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. /**
  175. * Create a new LineLM, and collect all consecutive
  176. * inline generating LMs as its child LMs.
  177. * @param firstlm First LM in new LineLM
  178. * @return the newly created LineLM
  179. */
  180. private LineLayoutManager createLineManager(LayoutManager firstlm) {
  181. LineLayoutManager llm;
  182. llm = new LineLayoutManager(getBlockFO(), lineHeight, lead, follow);
  183. List inlines = new java.util.ArrayList();
  184. inlines.add(firstlm);
  185. while (proxyLMiter.hasNext()) {
  186. LayoutManager lm = (LayoutManager) proxyLMiter.next();
  187. if (lm instanceof InlineLevelLayoutManager) {
  188. inlines.add(lm);
  189. } else {
  190. proxyLMiter.previous();
  191. break;
  192. }
  193. }
  194. llm.addChildLMs(inlines);
  195. return llm;
  196. }
  197. /** {@inheritDoc} */
  198. public KeepProperty getKeepTogetherProperty() {
  199. return getBlockFO().getKeepTogether();
  200. }
  201. /** {@inheritDoc} */
  202. public KeepProperty getKeepWithPreviousProperty() {
  203. return getBlockFO().getKeepWithPrevious();
  204. }
  205. /** {@inheritDoc} */
  206. public KeepProperty getKeepWithNextProperty() {
  207. return getBlockFO().getKeepWithNext();
  208. }
  209. /** {@inheritDoc} */
  210. public void addAreas(PositionIterator parentIter,
  211. LayoutContext layoutContext) {
  212. getParentArea(null);
  213. // if this will create the first block area in a page
  214. // and display-align is after or center, add space before
  215. if (layoutContext.getSpaceBefore() > 0) {
  216. addBlockSpacing(0.0, new MinOptMax(layoutContext.getSpaceBefore()));
  217. }
  218. LayoutManager childLM;
  219. LayoutManager lastLM = null;
  220. LayoutContext lc = new LayoutContext(0);
  221. lc.setSpaceAdjust(layoutContext.getSpaceAdjust());
  222. // set space after in the LayoutContext for children
  223. if (layoutContext.getSpaceAfter() > 0) {
  224. lc.setSpaceAfter(layoutContext.getSpaceAfter());
  225. }
  226. PositionIterator childPosIter;
  227. // "unwrap" the NonLeafPositions stored in parentIter
  228. // and put them in a new list;
  229. LinkedList positionList = new LinkedList();
  230. Position pos;
  231. boolean spaceBefore = false;
  232. boolean spaceAfter = false;
  233. Position firstPos = null;
  234. Position lastPos = null;
  235. while (parentIter.hasNext()) {
  236. pos = (Position) parentIter.next();
  237. //log.trace("pos = " + pos.getClass().getName() + "; " + pos);
  238. if (pos.getIndex() >= 0) {
  239. if (firstPos == null) {
  240. firstPos = pos;
  241. }
  242. lastPos = pos;
  243. }
  244. Position innerPosition = pos;
  245. if (pos instanceof NonLeafPosition) {
  246. //Not all elements are wrapped
  247. innerPosition = pos.getPosition();
  248. }
  249. if (innerPosition == null) {
  250. // pos was created by this BlockLM and was inside an element
  251. // representing space before or after
  252. // this means the space was not discarded
  253. if (positionList.size() == 0) {
  254. // pos was in the element representing space-before
  255. spaceBefore = true;
  256. //log.trace(" space before");
  257. } else {
  258. // pos was in the element representing space-after
  259. spaceAfter = true;
  260. //log.trace(" space-after");
  261. }
  262. } else if (innerPosition.getLM() == this
  263. && !(innerPosition instanceof MappingPosition)) {
  264. // pos was created by this BlockLM and was inside a penalty
  265. // allowing or forbidding a page break
  266. // nothing to do
  267. //log.trace(" penalty");
  268. } else {
  269. // innerPosition was created by another LM
  270. positionList.add(innerPosition);
  271. lastLM = innerPosition.getLM();
  272. //log.trace(" " + innerPosition.getClass().getName());
  273. }
  274. }
  275. addId();
  276. addMarkersToPage(true, isFirst(firstPos), isLast(lastPos));
  277. if (bpUnit == 0) {
  278. // the Positions in positionList were inside the elements
  279. // created by the LineLM
  280. childPosIter = new StackingIter(positionList.listIterator());
  281. } else {
  282. // the Positions in positionList were inside the elements
  283. // created by the BlockLM in the createUnitElements() method
  284. //if (((Position) positionList.getLast()) instanceof
  285. // LeafPosition) {
  286. // // the last item inside positionList is a LeafPosition
  287. // // (a LineBreakPosition, more precisely); this means that
  288. // // the whole paragraph is on the same page
  289. // childPosIter = new KnuthPossPosIter(storedList, 0,
  290. // storedList.size());
  291. //} else {
  292. // // the last item inside positionList is a Position;
  293. // // this means that the paragraph has been split
  294. // // between consecutive pages
  295. LinkedList splitList = new LinkedList();
  296. int splitLength = 0;
  297. int iFirst = ((MappingPosition) positionList.getFirst()).getFirstIndex();
  298. int iLast = ((MappingPosition) positionList.getLast()).getLastIndex();
  299. // copy from storedList to splitList all the elements from
  300. // iFirst to iLast
  301. ListIterator storedListIterator = storedList.listIterator(iFirst);
  302. while (storedListIterator.nextIndex() <= iLast) {
  303. KnuthElement element = (KnuthElement) storedListIterator
  304. .next();
  305. // some elements in storedList (i.e. penalty items) were created
  306. // by this BlockLM, and must be ignored
  307. if (element.getLayoutManager() != this) {
  308. splitList.add(element);
  309. splitLength += element.getWidth();
  310. lastLM = element.getLayoutManager();
  311. }
  312. }
  313. //log.debug("Adding areas from " + iFirst + " to " + iLast);
  314. //log.debug("splitLength= " + splitLength
  315. // + " (" + neededUnits(splitLength) + " units') "
  316. // + (neededUnits(splitLength) * bpUnit - splitLength)
  317. // + " spacing");
  318. // add space before and / or after the paragraph
  319. // to reach a multiple of bpUnit
  320. if (spaceBefore && spaceAfter) {
  321. foSpaceBefore = new SpaceVal(getBlockFO()
  322. .getCommonMarginBlock().spaceBefore, this).getSpace();
  323. foSpaceAfter = new SpaceVal(getBlockFO()
  324. .getCommonMarginBlock().spaceAfter, this).getSpace();
  325. adjustedSpaceBefore = (neededUnits(splitLength
  326. + foSpaceBefore.min
  327. + foSpaceAfter.min)
  328. * bpUnit - splitLength) / 2;
  329. adjustedSpaceAfter = neededUnits(splitLength
  330. + foSpaceBefore.min
  331. + foSpaceAfter.min)
  332. * bpUnit - splitLength - adjustedSpaceBefore;
  333. } else if (spaceBefore) {
  334. adjustedSpaceBefore = neededUnits(splitLength
  335. + foSpaceBefore.min)
  336. * bpUnit - splitLength;
  337. } else {
  338. adjustedSpaceAfter = neededUnits(splitLength
  339. + foSpaceAfter.min)
  340. * bpUnit - splitLength;
  341. }
  342. //log.debug("spazio prima = " + adjustedSpaceBefore
  343. // + " spazio dopo = " + adjustedSpaceAfter + " totale = " +
  344. // (adjustedSpaceBefore + adjustedSpaceAfter + splitLength));
  345. childPosIter = new KnuthPossPosIter(splitList, 0, splitList
  346. .size());
  347. //}
  348. }
  349. while ((childLM = childPosIter.getNextChildLM()) != null) {
  350. // set last area flag
  351. lc.setFlags(LayoutContext.LAST_AREA,
  352. (layoutContext.isLastArea() && childLM == lastLM));
  353. lc.setStackLimitBP(layoutContext.getStackLimitBP());
  354. // Add the line areas to Area
  355. childLM.addAreas(childPosIter, lc);
  356. }
  357. addMarkersToPage(false, isFirst(firstPos), isLast(lastPos));
  358. TraitSetter.addPtr(curBlockArea, getBlockFO().getPtr()); // used for accessibility
  359. TraitSetter.addSpaceBeforeAfter(curBlockArea, layoutContext.getSpaceAdjust(),
  360. effSpaceBefore, effSpaceAfter);
  361. flush();
  362. curBlockArea = null;
  363. resetSpaces();
  364. //Notify end of block layout manager to the PSLM
  365. checkEndOfLayout(lastPos);
  366. }
  367. /**
  368. * Return an Area which can contain the passed childArea. The childArea
  369. * may not yet have any content, but it has essential traits set.
  370. * In general, if the LayoutManager already has an Area it simply returns
  371. * it. Otherwise, it makes a new Area of the appropriate class.
  372. * It gets a parent area for its area by calling its parent LM.
  373. * Finally, based on the dimensions of the parent area, it initializes
  374. * its own area. This includes setting the content IPD and the maximum
  375. * BPD.
  376. * @param childArea area to get the parent area for
  377. * @return the parent area
  378. */
  379. public Area getParentArea(Area childArea) {
  380. if (curBlockArea == null) {
  381. curBlockArea = new Block();
  382. curBlockArea.setIPD(super.getContentAreaIPD());
  383. TraitSetter.addBreaks(curBlockArea,
  384. getBlockFO().getBreakBefore(), getBlockFO().getBreakAfter());
  385. // Must get dimensions from parent area
  386. //Don't optimize this line away. It can have ugly side-effects.
  387. /*Area parentArea =*/ parentLM.getParentArea(curBlockArea);
  388. // set traits
  389. TraitSetter.setProducerID(curBlockArea, getBlockFO().getId());
  390. TraitSetter.addBorders(curBlockArea,
  391. getBlockFO().getCommonBorderPaddingBackground(),
  392. discardBorderBefore, discardBorderAfter, false, false, this);
  393. TraitSetter.addPadding(curBlockArea,
  394. getBlockFO().getCommonBorderPaddingBackground(),
  395. discardPaddingBefore, discardPaddingAfter, false, false, this);
  396. TraitSetter.addMargins(curBlockArea,
  397. getBlockFO().getCommonBorderPaddingBackground(),
  398. startIndent, endIndent,
  399. this);
  400. setCurrentArea(curBlockArea); // ??? for generic operations
  401. }
  402. return curBlockArea;
  403. }
  404. /**
  405. * {@inheritDoc}
  406. */
  407. public void addChildArea(Area childArea) {
  408. if (curBlockArea != null) {
  409. if (childArea instanceof LineArea) {
  410. curBlockArea.addLineArea((LineArea) childArea);
  411. } else {
  412. curBlockArea.addBlock((Block) childArea);
  413. }
  414. }
  415. }
  416. /**
  417. * Force current area to be added to parent area.
  418. * {@inheritDoc}
  419. */
  420. protected void flush() {
  421. if (curBlockArea != null) {
  422. TraitSetter.addBackground(curBlockArea,
  423. getBlockFO().getCommonBorderPaddingBackground(),
  424. this);
  425. super.flush();
  426. }
  427. }
  428. /**
  429. * convenience method that returns the Block node
  430. * @return the block node
  431. */
  432. protected org.apache.fop.fo.flow.Block getBlockFO() {
  433. return (org.apache.fop.fo.flow.Block) fobj;
  434. }
  435. // --------- Property Resolution related functions --------- //
  436. /**
  437. * Returns the IPD of the content area
  438. * @return the IPD of the content area
  439. */
  440. public int getContentAreaIPD() {
  441. if (curBlockArea != null) {
  442. return curBlockArea.getIPD();
  443. }
  444. return super.getContentAreaIPD();
  445. }
  446. /**
  447. * Returns the BPD of the content area
  448. * @return the BPD of the content area
  449. */
  450. public int getContentAreaBPD() {
  451. if (curBlockArea != null) {
  452. return curBlockArea.getBPD();
  453. }
  454. return -1;
  455. }
  456. /**
  457. * {@inheritDoc}
  458. */
  459. public boolean getGeneratesBlockArea() {
  460. return true;
  461. }
  462. /** {@inheritDoc} */
  463. public void notifySpace(RelSide side, MinOptMax effectiveLength) {
  464. if (RelSide.BEFORE == side) {
  465. if (log.isDebugEnabled()) {
  466. log.debug(this + ": Space " + side + ", "
  467. + this.effSpaceBefore + "-> " + effectiveLength);
  468. }
  469. this.effSpaceBefore = effectiveLength;
  470. } else {
  471. if (log.isDebugEnabled()) {
  472. log.debug(this + ": Space " + side + ", "
  473. + this.effSpaceAfter + "-> " + effectiveLength);
  474. }
  475. this.effSpaceAfter = effectiveLength;
  476. }
  477. }
  478. /** {@inheritDoc} */
  479. public void notifyBorder(RelSide side, MinOptMax effectiveLength) {
  480. if (effectiveLength == null) {
  481. if (RelSide.BEFORE == side) {
  482. this.discardBorderBefore = true;
  483. } else {
  484. this.discardBorderAfter = true;
  485. }
  486. }
  487. if (log.isDebugEnabled()) {
  488. log.debug(this + ": Border " + side + " -> " + effectiveLength);
  489. }
  490. }
  491. /** {@inheritDoc} */
  492. public void notifyPadding(RelSide side, MinOptMax effectiveLength) {
  493. if (effectiveLength == null) {
  494. if (RelSide.BEFORE == side) {
  495. this.discardPaddingBefore = true;
  496. } else {
  497. this.discardPaddingAfter = true;
  498. }
  499. }
  500. if (log.isDebugEnabled()) {
  501. log.debug(this + ": Padding " + side + " -> " + effectiveLength);
  502. }
  503. }
  504. /** {@inheritDoc} */
  505. public boolean isRestartable() {
  506. return true;
  507. }
  508. }