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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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.FONode;
  30. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  31. import org.apache.fop.fo.properties.KeepProperty;
  32. import org.apache.fop.fonts.Font;
  33. import org.apache.fop.fonts.FontInfo;
  34. import org.apache.fop.fonts.FontTriplet;
  35. import org.apache.fop.layoutmgr.inline.InlineLevelLayoutManager;
  36. import org.apache.fop.layoutmgr.inline.LineLayoutManager;
  37. import org.apache.fop.traits.MinOptMax;
  38. import org.apache.fop.traits.SpaceVal;
  39. /**
  40. * LayoutManager for a block FO.
  41. */
  42. public class BlockLayoutManager extends SpacedBorderedPaddedBlockLayoutManager
  43. implements BreakOpportunity {
  44. /** logging instance */
  45. private static Log log = LogFactory.getLog(BlockLayoutManager.class);
  46. private Block curBlockArea;
  47. /** Iterator over the child layout managers. */
  48. protected ListIterator<LayoutManager> proxyLMiter;
  49. private int lead = 12000;
  50. private Length lineHeight;
  51. private int follow = 2000;
  52. //private int middleShift = 0;
  53. /**
  54. * Creates a new BlockLayoutManager.
  55. * @param inBlock the block FO object to create the layout manager for.
  56. */
  57. public BlockLayoutManager(org.apache.fop.fo.flow.Block inBlock) {
  58. super(inBlock);
  59. proxyLMiter = new ProxyLMiter();
  60. }
  61. /** {@inheritDoc} */
  62. @Override
  63. public void initialize() {
  64. super.initialize();
  65. org.apache.fop.fo.flow.Block fo = getBlockFO();
  66. FontInfo fi = fo.getFOEventHandler().getFontInfo();
  67. FontTriplet[] fontkeys = fo.getCommonFont().getFontState(fi);
  68. Font initFont = fi.getFontInstance(fontkeys[0],
  69. getBlockFO().getCommonFont().fontSize.getValue(this));
  70. lead = initFont.getAscender();
  71. follow = -initFont.getDescender();
  72. //middleShift = -fs.getXHeight() / 2;
  73. lineHeight = fo.getLineHeight().getOptimum(this).getLength();
  74. startIndent = fo.getCommonMarginBlock().startIndent.getValue(this);
  75. endIndent = fo.getCommonMarginBlock().endIndent.getValue(this);
  76. foSpaceBefore = new SpaceVal(fo.getCommonMarginBlock().spaceBefore, this).getSpace();
  77. foSpaceAfter = new SpaceVal(fo.getCommonMarginBlock().spaceAfter, this).getSpace();
  78. // use optimum space values
  79. adjustedSpaceBefore = fo.getCommonMarginBlock().spaceBefore.getSpace()
  80. .getOptimum(this).getLength().getValue(this);
  81. adjustedSpaceAfter = fo.getCommonMarginBlock().spaceAfter.getSpace()
  82. .getOptimum(this).getLength().getValue(this);
  83. }
  84. @Override
  85. protected CommonBorderPaddingBackground getCommonBorderPaddingBackground() {
  86. return getBlockFO().getCommonBorderPaddingBackground();
  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. TraitSetter.setVisibility(curBlockArea, getBlockFO().getVisibility());
  293. flush();
  294. curBlockArea = null;
  295. resetSpaces();
  296. //Notify end of block layout manager to the PSLM
  297. checkEndOfLayout(lastPos);
  298. }
  299. /**
  300. * Return an Area which can contain the passed childArea. The childArea
  301. * may not yet have any content, but it has essential traits set.
  302. * In general, if the LayoutManager already has an Area it simply returns
  303. * it. Otherwise, it makes a new Area of the appropriate class.
  304. * It gets a parent area for its area by calling its parent LM.
  305. * Finally, based on the dimensions of the parent area, it initializes
  306. * its own area. This includes setting the content IPD and the maximum
  307. * BPD.
  308. * @param childArea area to get the parent area for
  309. * @return the parent area
  310. */
  311. @Override
  312. public Area getParentArea(Area childArea) {
  313. if (curBlockArea == null) {
  314. curBlockArea = new Block();
  315. curBlockArea.setChangeBarList(getChangeBarList());
  316. curBlockArea.setIPD(super.getContentAreaIPD());
  317. curBlockArea.setBidiLevel(getBlockFO().getBidiLevelRecursive());
  318. TraitSetter.addBreaks(curBlockArea,
  319. getBlockFO().getBreakBefore(), getBlockFO().getBreakAfter());
  320. // Must get dimensions from parent area
  321. //Don't optimize this line away. It can have ugly side-effects.
  322. /*Area parentArea =*/ parentLayoutManager.getParentArea(curBlockArea);
  323. // set traits
  324. TraitSetter.setProducerID(curBlockArea, getBlockFO().getId());
  325. TraitSetter.addBorders(curBlockArea,
  326. getBlockFO().getCommonBorderPaddingBackground(),
  327. discardBorderBefore, discardBorderAfter, false, false, this);
  328. TraitSetter.addPadding(curBlockArea,
  329. getBlockFO().getCommonBorderPaddingBackground(),
  330. discardPaddingBefore, discardPaddingAfter, false, false, this);
  331. TraitSetter.addMargins(curBlockArea,
  332. getBlockFO().getCommonBorderPaddingBackground(),
  333. startIndent, endIndent,
  334. this);
  335. TraitSetter.setLayer(curBlockArea, getBlockFO().getLayer());
  336. curBlockArea.setLocale(getBlockFO().getCommonHyphenation().getLocale());
  337. curBlockArea.setLocation(FONode.getLocatorString(getBlockFO().getLocator()));
  338. setCurrentArea(curBlockArea); // ??? for generic operations
  339. }
  340. return curBlockArea;
  341. }
  342. /** {@inheritDoc} */
  343. @Override
  344. public void addChildArea(Area childArea) {
  345. if (curBlockArea != null) {
  346. if (childArea instanceof LineArea) {
  347. curBlockArea.addLineArea((LineArea) childArea);
  348. } else {
  349. curBlockArea.addBlock((Block) childArea);
  350. }
  351. }
  352. }
  353. /**
  354. * Force current area to be added to parent area.
  355. * {@inheritDoc}
  356. */
  357. @Override
  358. protected void flush() {
  359. if (curBlockArea != null) {
  360. TraitSetter.addBackground(curBlockArea,
  361. getBlockFO().getCommonBorderPaddingBackground(),
  362. this);
  363. super.flush();
  364. }
  365. }
  366. /**
  367. * convenience method that returns the Block node
  368. * @return the block node
  369. */
  370. protected org.apache.fop.fo.flow.Block getBlockFO() {
  371. return (org.apache.fop.fo.flow.Block) fobj;
  372. }
  373. // --------- Property Resolution related functions --------- //
  374. /**
  375. * Returns the IPD of the content area
  376. * @return the IPD of the content area
  377. */
  378. @Override
  379. public int getContentAreaIPD() {
  380. if (curBlockArea != null) {
  381. return curBlockArea.getIPD();
  382. }
  383. return super.getContentAreaIPD();
  384. }
  385. /**
  386. * Returns the BPD of the content area
  387. * @return the BPD of the content area
  388. */
  389. @Override
  390. public int getContentAreaBPD() {
  391. if (curBlockArea != null) {
  392. return curBlockArea.getBPD();
  393. }
  394. return -1;
  395. }
  396. /** {@inheritDoc} */
  397. @Override
  398. public boolean getGeneratesBlockArea() {
  399. return true;
  400. }
  401. /** {@inheritDoc} */
  402. @Override
  403. public boolean isRestartable() {
  404. return true;
  405. }
  406. }