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.

BlockLayoutManager.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 implements ConditionalElementListener,
  41. BreakOpportunity {
  42. /** logging instance */
  43. private static Log log = LogFactory.getLog(BlockLayoutManager.class);
  44. private Block curBlockArea;
  45. /** Iterator over the child layout managers. */
  46. protected ListIterator<LayoutManager> proxyLMiter;
  47. private int lead = 12000;
  48. private Length lineHeight;
  49. private int follow = 2000;
  50. //private int middleShift = 0;
  51. private boolean discardBorderBefore;
  52. private boolean discardBorderAfter;
  53. private boolean discardPaddingBefore;
  54. private boolean discardPaddingAfter;
  55. private MinOptMax effSpaceBefore;
  56. private MinOptMax effSpaceAfter;
  57. /**
  58. * Creates a new BlockLayoutManager.
  59. * @param inBlock the block FO object to create the layout manager for.
  60. */
  61. public BlockLayoutManager(org.apache.fop.fo.flow.Block inBlock) {
  62. super(inBlock);
  63. proxyLMiter = new ProxyLMiter();
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public void initialize() {
  68. super.initialize();
  69. org.apache.fop.fo.flow.Block fo = getBlockFO();
  70. FontInfo fi = fo.getFOEventHandler().getFontInfo();
  71. FontTriplet[] fontkeys = fo.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 = fo.getLineHeight().getOptimum(this).getLength();
  78. startIndent = fo.getCommonMarginBlock().startIndent.getValue(this);
  79. endIndent = fo.getCommonMarginBlock().endIndent.getValue(this);
  80. foSpaceBefore = new SpaceVal(fo.getCommonMarginBlock().spaceBefore, this).getSpace();
  81. foSpaceAfter = new SpaceVal(fo.getCommonMarginBlock().spaceAfter, this).getSpace();
  82. // use optimum space values
  83. adjustedSpaceBefore = fo.getCommonMarginBlock().spaceBefore.getSpace()
  84. .getOptimum(this).getLength().getValue(this);
  85. adjustedSpaceAfter = fo.getCommonMarginBlock().spaceAfter.getSpace()
  86. .getOptimum(this).getLength().getValue(this);
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public List getNextKnuthElements(LayoutContext context, int alignment) {
  91. return getNextKnuthElements(context, alignment, null, null, null);
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public List getNextKnuthElements(LayoutContext context, int alignment, Stack lmStack,
  96. Position restartPosition, LayoutManager restartAtLM) {
  97. resetSpaces();
  98. return super.getNextKnuthElements(
  99. context, alignment, lmStack, restartPosition, restartAtLM);
  100. }
  101. /**
  102. * Overridden to take into account that the childLM may be the block's
  103. * {@link LineLayoutManager}.
  104. * {@inheritDoc}
  105. */
  106. @Override
  107. protected List<ListElement> getNextChildElements(LayoutManager childLM, LayoutContext context,
  108. LayoutContext childLC, int alignment, Stack lmStack, Position restartPosition,
  109. LayoutManager restartAtLM) {
  110. childLC.copyPendingMarksFrom(context);
  111. if (childLM instanceof LineLayoutManager) {
  112. childLC.setRefIPD(getContentAreaIPD());
  113. } else {
  114. // nop; will have been properly set by makeChildLayoutContext()
  115. }
  116. if (childLM == this.childLMs.get(0)) {
  117. childLC.setFlags(LayoutContext.SUPPRESS_BREAK_BEFORE);
  118. //Handled already by the parent (break collapsing, see above)
  119. }
  120. if (lmStack == null) {
  121. return childLM.getNextKnuthElements(childLC, alignment);
  122. } else {
  123. if (childLM instanceof LineLayoutManager) {
  124. assert (restartPosition instanceof LeafPosition);
  125. return ((LineLayoutManager) childLM).getNextKnuthElements(childLC, alignment,
  126. (LeafPosition) restartPosition);
  127. } else {
  128. return childLM.getNextKnuthElements(childLC, alignment,
  129. lmStack, restartPosition, restartAtLM);
  130. }
  131. }
  132. }
  133. private void resetSpaces() {
  134. this.discardBorderBefore = false;
  135. this.discardBorderAfter = false;
  136. this.discardPaddingBefore = false;
  137. this.discardPaddingAfter = false;
  138. this.effSpaceBefore = null;
  139. this.effSpaceAfter = null;
  140. }
  141. /**
  142. * Proxy iterator for Block LM.
  143. * This iterator creates and holds the complete list
  144. * of child LMs.
  145. * It uses fobjIter as its base iterator.
  146. * Block LM's createNextChildLMs uses this iterator
  147. * as its base iterator.
  148. */
  149. protected class ProxyLMiter extends LMiter {
  150. /**
  151. * Constructs a proxy iterator for Block LM.
  152. */
  153. public ProxyLMiter() {
  154. super(BlockLayoutManager.this);
  155. listLMs = new java.util.ArrayList<LayoutManager>(10);
  156. }
  157. /**
  158. * @return true if there are more child lms
  159. */
  160. public boolean hasNext() {
  161. return (curPos < listLMs.size()) || createNextChildLMs(curPos);
  162. }
  163. /**
  164. * @param pos ...
  165. * @return true if new child lms were added
  166. */
  167. protected boolean createNextChildLMs(int pos) {
  168. List<LayoutManager> newLMs = createChildLMs(pos + 1 - listLMs.size());
  169. if (newLMs != null) {
  170. listLMs.addAll(newLMs);
  171. }
  172. return pos < listLMs.size();
  173. }
  174. }
  175. /** {@inheritDoc} */
  176. @Override
  177. public boolean createNextChildLMs(int pos) {
  178. while (proxyLMiter.hasNext()) {
  179. LayoutManager lm = proxyLMiter.next();
  180. if (lm instanceof InlineLevelLayoutManager) {
  181. LineLayoutManager lineLM = createLineManager(lm);
  182. addChildLM(lineLM);
  183. } else {
  184. addChildLM(lm);
  185. }
  186. if (pos < childLMs.size()) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. /**
  193. * Create a new LineLM, and collect all consecutive
  194. * inline generating LMs as its child LMs.
  195. * @param firstlm First LM in new LineLM
  196. * @return the newly created LineLM
  197. */
  198. private LineLayoutManager createLineManager(LayoutManager firstlm) {
  199. LineLayoutManager llm;
  200. llm = new LineLayoutManager(getBlockFO(), lineHeight, lead, follow);
  201. List<LayoutManager> inlines = new java.util.ArrayList<LayoutManager>();
  202. inlines.add(firstlm);
  203. while (proxyLMiter.hasNext()) {
  204. LayoutManager lm = proxyLMiter.next();
  205. if (lm instanceof InlineLevelLayoutManager) {
  206. inlines.add(lm);
  207. } else {
  208. proxyLMiter.previous();
  209. break;
  210. }
  211. }
  212. llm.addChildLMs(inlines);
  213. return llm;
  214. }
  215. /** {@inheritDoc} */
  216. @Override
  217. public KeepProperty getKeepTogetherProperty() {
  218. return getBlockFO().getKeepTogether();
  219. }
  220. /** {@inheritDoc} */
  221. @Override
  222. public KeepProperty getKeepWithPreviousProperty() {
  223. return getBlockFO().getKeepWithPrevious();
  224. }
  225. /** {@inheritDoc} */
  226. @Override
  227. public KeepProperty getKeepWithNextProperty() {
  228. return getBlockFO().getKeepWithNext();
  229. }
  230. /** {@inheritDoc} */
  231. @Override
  232. public void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {
  233. getParentArea(null);
  234. // if this will create the first block area in a page
  235. // and display-align is after or center, add space before
  236. if (layoutContext.getSpaceBefore() > 0) {
  237. addBlockSpacing(0.0, MinOptMax.getInstance(layoutContext.getSpaceBefore()));
  238. }
  239. LayoutManager childLM;
  240. LayoutManager lastLM = null;
  241. LayoutContext lc = LayoutContext.offspringOf(layoutContext);
  242. lc.setSpaceAdjust(layoutContext.getSpaceAdjust());
  243. // set space after in the LayoutContext for children
  244. if (layoutContext.getSpaceAfter() > 0) {
  245. lc.setSpaceAfter(layoutContext.getSpaceAfter());
  246. }
  247. PositionIterator childPosIter;
  248. // "unwrap" the NonLeafPositions stored in parentIter
  249. // and put them in a new list;
  250. LinkedList<Position> positionList = new LinkedList<Position>();
  251. Position pos;
  252. Position firstPos = null;
  253. Position lastPos = null;
  254. while (parentIter.hasNext()) {
  255. pos = parentIter.next();
  256. //log.trace("pos = " + pos.getClass().getName() + "; " + pos);
  257. if (pos.getIndex() >= 0) {
  258. if (firstPos == null) {
  259. firstPos = pos;
  260. }
  261. lastPos = pos;
  262. }
  263. Position innerPosition = pos;
  264. if (pos instanceof NonLeafPosition) {
  265. //Not all elements are wrapped
  266. innerPosition = pos.getPosition();
  267. }
  268. if (innerPosition != null
  269. && (innerPosition.getLM() != this
  270. || innerPosition instanceof MappingPosition)) {
  271. // innerPosition was created by another LM
  272. positionList.add(innerPosition);
  273. lastLM = innerPosition.getLM();
  274. }
  275. }
  276. addId();
  277. registerMarkers(true, isFirst(firstPos), isLast(lastPos));
  278. // the Positions in positionList were inside the elements
  279. // created by the LineLM
  280. childPosIter = new PositionIterator(positionList.listIterator());
  281. while ((childLM = childPosIter.getNextChildLM()) != null) {
  282. // set last area flag
  283. lc.setFlags(LayoutContext.LAST_AREA,
  284. (layoutContext.isLastArea() && childLM == lastLM));
  285. lc.setStackLimitBP(layoutContext.getStackLimitBP());
  286. // Add the line areas to Area
  287. childLM.addAreas(childPosIter, lc);
  288. }
  289. registerMarkers(false, isFirst(firstPos), isLast(lastPos));
  290. TraitSetter.addSpaceBeforeAfter(curBlockArea, layoutContext.getSpaceAdjust(),
  291. effSpaceBefore, effSpaceAfter);
  292. flush();
  293. curBlockArea = null;
  294. resetSpaces();
  295. //Notify end of block layout manager to the PSLM
  296. checkEndOfLayout(lastPos);
  297. }
  298. /**
  299. * Return an Area which can contain the passed childArea. The childArea
  300. * may not yet have any content, but it has essential traits set.
  301. * In general, if the LayoutManager already has an Area it simply returns
  302. * it. Otherwise, it makes a new Area of the appropriate class.
  303. * It gets a parent area for its area by calling its parent LM.
  304. * Finally, based on the dimensions of the parent area, it initializes
  305. * its own area. This includes setting the content IPD and the maximum
  306. * BPD.
  307. * @param childArea area to get the parent area for
  308. * @return the parent area
  309. */
  310. @Override
  311. public Area getParentArea(Area childArea) {
  312. if (curBlockArea == null) {
  313. curBlockArea = new Block();
  314. curBlockArea.setIPD(super.getContentAreaIPD());
  315. curBlockArea.setBidiLevel ( getBlockFO().getBidiLevel() );
  316. TraitSetter.addBreaks(curBlockArea,
  317. getBlockFO().getBreakBefore(), getBlockFO().getBreakAfter());
  318. // Must get dimensions from parent area
  319. //Don't optimize this line away. It can have ugly side-effects.
  320. /*Area parentArea =*/ parentLayoutManager.getParentArea(curBlockArea);
  321. // set traits
  322. TraitSetter.setProducerID(curBlockArea, getBlockFO().getId());
  323. TraitSetter.addBorders(curBlockArea,
  324. getBlockFO().getCommonBorderPaddingBackground(),
  325. discardBorderBefore, discardBorderAfter, false, false, this);
  326. TraitSetter.addPadding(curBlockArea,
  327. getBlockFO().getCommonBorderPaddingBackground(),
  328. discardPaddingBefore, discardPaddingAfter, false, false, this);
  329. TraitSetter.addMargins(curBlockArea,
  330. getBlockFO().getCommonBorderPaddingBackground(),
  331. startIndent, endIndent,
  332. this);
  333. setCurrentArea(curBlockArea); // ??? for generic operations
  334. }
  335. return curBlockArea;
  336. }
  337. /** {@inheritDoc} */
  338. @Override
  339. public void addChildArea(Area childArea) {
  340. if (curBlockArea != null) {
  341. if (childArea instanceof LineArea) {
  342. curBlockArea.addLineArea((LineArea) childArea);
  343. } else {
  344. curBlockArea.addBlock((Block) childArea);
  345. }
  346. }
  347. }
  348. /**
  349. * Force current area to be added to parent area.
  350. * {@inheritDoc}
  351. */
  352. @Override
  353. protected void flush() {
  354. if (curBlockArea != null) {
  355. TraitSetter.addBackground(curBlockArea,
  356. getBlockFO().getCommonBorderPaddingBackground(),
  357. this);
  358. super.flush();
  359. }
  360. }
  361. /**
  362. * convenience method that returns the Block node
  363. * @return the block node
  364. */
  365. protected org.apache.fop.fo.flow.Block getBlockFO() {
  366. return (org.apache.fop.fo.flow.Block) fobj;
  367. }
  368. // --------- Property Resolution related functions --------- //
  369. /**
  370. * Returns the IPD of the content area
  371. * @return the IPD of the content area
  372. */
  373. @Override
  374. public int getContentAreaIPD() {
  375. if (curBlockArea != null) {
  376. return curBlockArea.getIPD();
  377. }
  378. return super.getContentAreaIPD();
  379. }
  380. /**
  381. * Returns the BPD of the content area
  382. * @return the BPD of the content area
  383. */
  384. @Override
  385. public int getContentAreaBPD() {
  386. if (curBlockArea != null) {
  387. return curBlockArea.getBPD();
  388. }
  389. return -1;
  390. }
  391. /** {@inheritDoc} */
  392. @Override
  393. public boolean getGeneratesBlockArea() {
  394. return true;
  395. }
  396. /** {@inheritDoc} */
  397. public void notifySpace(RelSide side, MinOptMax effectiveLength) {
  398. if (RelSide.BEFORE == side) {
  399. if (log.isDebugEnabled()) {
  400. log.debug(this + ": Space " + side + ", "
  401. + this.effSpaceBefore + "-> " + effectiveLength);
  402. }
  403. this.effSpaceBefore = effectiveLength;
  404. } else {
  405. if (log.isDebugEnabled()) {
  406. log.debug(this + ": Space " + side + ", "
  407. + this.effSpaceAfter + "-> " + effectiveLength);
  408. }
  409. this.effSpaceAfter = effectiveLength;
  410. }
  411. }
  412. /** {@inheritDoc} */
  413. public void notifyBorder(RelSide side, MinOptMax effectiveLength) {
  414. if (effectiveLength == null) {
  415. if (RelSide.BEFORE == side) {
  416. this.discardBorderBefore = true;
  417. } else {
  418. this.discardBorderAfter = true;
  419. }
  420. }
  421. if (log.isDebugEnabled()) {
  422. log.debug(this + ": Border " + side + " -> " + effectiveLength);
  423. }
  424. }
  425. /** {@inheritDoc} */
  426. public void notifyPadding(RelSide side, MinOptMax effectiveLength) {
  427. if (effectiveLength == null) {
  428. if (RelSide.BEFORE == side) {
  429. this.discardPaddingBefore = true;
  430. } else {
  431. this.discardPaddingAfter = true;
  432. }
  433. }
  434. if (log.isDebugEnabled()) {
  435. log.debug(this + ": Padding " + side + " -> " + effectiveLength);
  436. }
  437. }
  438. /** {@inheritDoc} */
  439. @Override
  440. public boolean isRestartable() {
  441. return true;
  442. }
  443. }