Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

BlockStackingLayoutManager.java 69KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. /*
  2. * Copyright 1999-2005 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.layoutmgr;
  18. import java.util.Iterator;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import java.util.ListIterator;
  22. import org.apache.fop.area.Area;
  23. import org.apache.fop.area.BlockParent;
  24. import org.apache.fop.area.Block;
  25. import org.apache.fop.fo.FObj;
  26. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  27. import org.apache.fop.fo.properties.SpaceProperty;
  28. import org.apache.fop.layoutmgr.inline.LineLayoutManager;
  29. import org.apache.fop.traits.MinOptMax;
  30. /**
  31. * Base LayoutManager class for all areas which stack their child
  32. * areas in the block-progression direction, such as Flow, Block, ListBlock.
  33. */
  34. public abstract class BlockStackingLayoutManager extends AbstractLayoutManager
  35. implements BlockLevelLayoutManager {
  36. /**
  37. * Reference to FO whose areas it's managing or to the traits
  38. * of the FO.
  39. */
  40. //protected LayoutManager curChildLM = null; AbstractLayoutManager also defines this!
  41. protected BlockParent parentArea = null;
  42. /** Value of the block-progression-unit (non-standard property) */
  43. protected int bpUnit = 0;
  44. /** space-before value adjusted for block-progression-unit handling */
  45. protected int adjustedSpaceBefore = 0;
  46. /** space-after value adjusted for block-progression-unit handling */
  47. protected int adjustedSpaceAfter = 0;
  48. /** Only used to store the original list when createUnitElements is called */
  49. protected LinkedList storedList = null;
  50. /** Indicates whether break before has been served or not */
  51. protected boolean bBreakBeforeServed = false;
  52. /** Indicates whether the first visible mark has been returned by this LM, yet */
  53. protected boolean firstVisibleMarkServed = false;
  54. /** Reference IPD available */
  55. protected int referenceIPD = 0;
  56. /**
  57. * Holds the (one-time use) fo:block space-before
  58. * and -after properties. Large fo:blocks are split
  59. * into multiple Area. Blocks to accomodate the subsequent
  60. * regions (pages) they are placed on. space-before
  61. * is applied at the beginning of the first
  62. * Block and space-after at the end of the last Block
  63. * used in rendering the fo:block.
  64. */
  65. protected MinOptMax foSpaceBefore = null;
  66. /** see foSpaceBefore */
  67. protected MinOptMax foSpaceAfter = null;
  68. private Position auxiliaryPosition;
  69. private int contentAreaIPD = 0;
  70. /**
  71. * @param node the fo this LM deals with
  72. */
  73. public BlockStackingLayoutManager(FObj node) {
  74. super(node);
  75. setGeneratesBlockArea(true);
  76. }
  77. /**
  78. * @return current area being filled
  79. */
  80. protected BlockParent getCurrentArea() {
  81. return this.parentArea;
  82. }
  83. /**
  84. * Set the current area being filled.
  85. * @param parentArea the current area to be filled
  86. */
  87. protected void setCurrentArea(BlockParent parentArea) {
  88. this.parentArea = parentArea;
  89. }
  90. /**
  91. * Add a block spacer for space before and space after a block.
  92. * This adds an empty Block area that acts as a block space.
  93. *
  94. * @param adjust the adjustment value
  95. * @param minoptmax the min/opt/max value of the spacing
  96. */
  97. public void addBlockSpacing(double adjust, MinOptMax minoptmax) {
  98. int sp = TraitSetter.getEffectiveSpace(adjust, minoptmax);
  99. if (sp != 0) {
  100. Block spacer = new Block();
  101. spacer.setBPD(sp);
  102. parentLM.addChildArea(spacer);
  103. }
  104. }
  105. /**
  106. * Add the childArea to the passed area.
  107. * Called by child LayoutManager when it has filled one of its areas.
  108. * The LM should already have an Area in which to put the child.
  109. * See if the area will fit in the current area.
  110. * If so, add it. Otherwise initiate breaking.
  111. * @param childArea the area to add: will be some block-stacked Area.
  112. * @param parentArea the area in which to add the childArea
  113. */
  114. protected void addChildToArea(Area childArea,
  115. BlockParent parentArea) {
  116. // This should be a block-level Area (Block in the generic sense)
  117. if (!(childArea instanceof Block)) {
  118. //log.error("Child not a Block in BlockStackingLM!");
  119. }
  120. parentArea.addBlock((Block) childArea);
  121. flush(); // hand off current area to parent
  122. }
  123. /**
  124. * Add the childArea to the current area.
  125. * Called by child LayoutManager when it has filled one of its areas.
  126. * The LM should already have an Area in which to put the child.
  127. * See if the area will fit in the current area.
  128. * If so, add it. Otherwise initiate breaking.
  129. * @param childArea the area to add: will be some block-stacked Area.
  130. */
  131. public void addChildArea(Area childArea) {
  132. addChildToArea(childArea, getCurrentArea());
  133. }
  134. /**
  135. * Force current area to be added to parent area.
  136. */
  137. protected void flush() {
  138. if (getCurrentArea() != null) {
  139. parentLM.addChildArea(getCurrentArea());
  140. }
  141. }
  142. /** @return a cached auxiliary Position instance used for things like spaces. */
  143. protected Position getAuxiliaryPosition() {
  144. if (this.auxiliaryPosition == null) {
  145. this.auxiliaryPosition = new NonLeafPosition(this, null);
  146. }
  147. return this.auxiliaryPosition;
  148. }
  149. /**
  150. * @param len length in millipoints to span with bp units
  151. * @return the minimum integer n such that n * bpUnit >= len
  152. */
  153. protected int neededUnits(int len) {
  154. return (int) Math.ceil((float)len / bpUnit);
  155. }
  156. /**
  157. * @see LayoutManager#getNextKnuthElements(LayoutContext, int)
  158. */
  159. public LinkedList getNextKnuthElements(LayoutContext context, int alignment) {
  160. /* LF *///System.err.println("BLM.getNextKnuthElements> keep-together = "
  161. // + layoutProps.keepTogether.getType());
  162. /* LF *///System.err.println(" keep-with-previous = " +
  163. // layoutProps.keepWithPrevious.getType());
  164. /* LF *///System.err.println(" keep-with-next = " +
  165. // layoutProps.keepWithNext.getType());
  166. BlockLevelLayoutManager curLM; // currently active LM
  167. BlockLevelLayoutManager prevLM = null; // previously active LM
  168. referenceIPD = context.getRefIPD();
  169. int iIndents = 0;
  170. int bIndents = 0;
  171. if (fobj instanceof org.apache.fop.fo.flow.Block) {
  172. org.apache.fop.fo.flow.Block block = (org.apache.fop.fo.flow.Block)fobj;
  173. iIndents = block.getCommonMarginBlock().startIndent.getValue(this)
  174. + block.getCommonMarginBlock().endIndent.getValue(this);
  175. bIndents = block.getCommonBorderPaddingBackground().getBPPaddingAndBorder(false, this);
  176. }
  177. int ipd = referenceIPD - iIndents;
  178. MinOptMax stackSize = new MinOptMax();
  179. // Set context for percentage property values.
  180. setContentAreaIPD(ipd);
  181. LinkedList returnedList = null;
  182. LinkedList contentList = new LinkedList();
  183. LinkedList returnList = new LinkedList();
  184. if (!bBreakBeforeServed) {
  185. try {
  186. if (addKnuthElementsForBreakBefore(returnList, context)) {
  187. return returnList;
  188. }
  189. } finally {
  190. bBreakBeforeServed = true;
  191. }
  192. }
  193. addKnuthElementsForSpaceBefore(returnList, alignment);
  194. addKnuthElementsForBorderPaddingBefore(returnList, !firstVisibleMarkServed);
  195. firstVisibleMarkServed = true;
  196. //Spaces, border and padding to be repeated at each break
  197. addPendingMarks(context);
  198. while ((curLM = (BlockLevelLayoutManager) getChildLM()) != null) {
  199. LayoutContext childLC = new LayoutContext(0);
  200. childLC.copyPendingMarksFrom(context);
  201. if (curLM instanceof LineLayoutManager) {
  202. // curLM is a LineLayoutManager
  203. // set stackLimit for lines
  204. childLC.setStackLimit(new MinOptMax(ipd/*
  205. * - iIndents -
  206. * iTextIndent
  207. */));
  208. childLC.setRefIPD(ipd);
  209. } else {
  210. // curLM is a ?
  211. //childLC.setStackLimit(MinOptMax.subtract(context
  212. // .getStackLimit(), stackSize));
  213. childLC.setStackLimit(context.getStackLimit());
  214. childLC.setRefIPD(referenceIPD);
  215. }
  216. // get elements from curLM
  217. returnedList = curLM.getNextKnuthElements(childLC, alignment);
  218. if (contentList.size() == 0 && childLC.isKeepWithPreviousPending()) {
  219. context.setFlags(LayoutContext.KEEP_WITH_PREVIOUS_PENDING);
  220. childLC.setFlags(LayoutContext.KEEP_WITH_PREVIOUS_PENDING, false);
  221. }
  222. if (returnedList != null
  223. && returnedList.size() == 1
  224. && ((ListElement) returnedList.getFirst()).isForcedBreak()) {
  225. // a descendant of this block has break-before
  226. /*
  227. if (returnList.size() == 0) {
  228. // the first child (or its first child ...) has
  229. // break-before;
  230. // all this block, including space before, will be put in
  231. // the
  232. // following page
  233. bSpaceBeforeServed = false;
  234. }*/
  235. contentList.addAll(returnedList);
  236. /* extension: conversione di tutta la sequenza fin'ora ottenuta */
  237. if (bpUnit > 0) {
  238. storedList = contentList;
  239. contentList = createUnitElements(contentList);
  240. }
  241. /* end of extension */
  242. // "wrap" the Position inside each element
  243. // moving the elements from contentList to returnList
  244. returnedList = new LinkedList();
  245. wrapPositionElements(contentList, returnList);
  246. return returnList;
  247. } else {
  248. if (prevLM != null) {
  249. // there is a block handled by prevLM
  250. // before the one handled by curLM
  251. if (mustKeepTogether()
  252. || context.isKeepWithNextPending()
  253. || childLC.isKeepWithPreviousPending()) {
  254. //Clear keep pending flag
  255. context.setFlags(LayoutContext.KEEP_WITH_NEXT_PENDING, false);
  256. childLC.setFlags(LayoutContext.KEEP_WITH_PREVIOUS_PENDING, false);
  257. // add an infinite penalty to forbid a break between
  258. // blocks
  259. contentList.add(new BreakElement(
  260. new Position(this), KnuthElement.INFINITE, context));
  261. /*
  262. contentList.add(new KnuthPenalty(0,
  263. KnuthElement.INFINITE, false,
  264. new Position(this), false));
  265. */
  266. } else if (!((ListElement) contentList.getLast()).isGlue()) {
  267. // add a null penalty to allow a break between blocks
  268. contentList.add(new BreakElement(
  269. new Position(this), 0, context));
  270. /*
  271. contentList.add(new KnuthPenalty(0, 0, false,
  272. new Position(this), false));*/
  273. } else {
  274. // the last element in contentList is a glue;
  275. // it is a feasible breakpoint, there is no need to add
  276. // a penalty
  277. log.warn("glue-type break possibility not handled properly, yet");
  278. //TODO Does this happen? If yes, need to deal with border and padding
  279. //at the break possibility
  280. }
  281. }
  282. if (returnedList == null || returnedList.size() == 0) {
  283. //Avoid NoSuchElementException below (happens with empty blocks)
  284. continue;
  285. }
  286. contentList.addAll(returnedList);
  287. if (((ListElement) returnedList.getLast()).isForcedBreak()) {
  288. // a descendant of this block has break-after
  289. if (curLM.isFinished()) {
  290. // there is no other content in this block;
  291. // it's useless to add space after before a page break
  292. setFinished(true);
  293. }
  294. /* extension: conversione di tutta la sequenza fin'ora ottenuta */
  295. if (bpUnit > 0) {
  296. storedList = contentList;
  297. contentList = createUnitElements(contentList);
  298. }
  299. /* end of extension */
  300. returnedList = new LinkedList();
  301. wrapPositionElements(contentList, returnList);
  302. return returnList;
  303. }
  304. /*
  305. if (allocatedSpace.min > context.getStackLimit().max) {
  306. log.debug("Allocated space exceeds stack limit, returning early.");
  307. return returnList;
  308. }*/
  309. }
  310. if (childLC.isKeepWithNextPending()) {
  311. //Clear and propagate
  312. childLC.setFlags(LayoutContext.KEEP_WITH_NEXT_PENDING, false);
  313. context.setFlags(LayoutContext.KEEP_WITH_NEXT_PENDING);
  314. }
  315. prevLM = curLM;
  316. }
  317. /* Extension: conversione di tutta la sequenza fin'ora ottenuta */
  318. if (bpUnit > 0) {
  319. storedList = contentList;
  320. contentList = createUnitElements(contentList);
  321. }
  322. /* end of extension */
  323. returnedList = new LinkedList();
  324. if (contentList.size() > 0) {
  325. wrapPositionElements(contentList, returnList);
  326. } else {
  327. //Empty fo:block, zero-length box makes sure the IDs are registered.
  328. returnList.add(new KnuthBox(0, notifyPos(new Position(this)), true));
  329. }
  330. addKnuthElementsForBorderPaddingAfter(returnList, true);
  331. addKnuthElementsForSpaceAfter(returnList, alignment);
  332. addKnuthElementsForBreakAfter(returnList, context);
  333. if (mustKeepWithNext()) {
  334. context.setFlags(LayoutContext.KEEP_WITH_NEXT_PENDING);
  335. }
  336. if (mustKeepWithPrevious()) {
  337. context.setFlags(LayoutContext.KEEP_WITH_PREVIOUS_PENDING);
  338. }
  339. setFinished(true);
  340. return returnList;
  341. }
  342. /**
  343. * @see org.apache.fop.layoutmgr.BlockLevelLayoutManager#negotiateBPDAdjustment(int, org.apache.fop.layoutmgr.KnuthElement)
  344. */
  345. public int negotiateBPDAdjustment(int adj, KnuthElement lastElement) {
  346. /*LF*/ //System.out.println(" BLM.negotiateBPDAdjustment> " + adj);
  347. /*LF*/ //System.out.println(" lastElement e' " + (lastElement.isPenalty()
  348. // ? "penalty" : (lastElement.isGlue() ? "glue" : "box" )));
  349. /*LF*/ //System.out.println(" position e' " + lastElement.getPosition().getClass().getName());
  350. /*LF*/ //System.out.println(" " + (bpUnit > 0 ? "unit" : ""));
  351. Position innerPosition = ((NonLeafPosition) lastElement.getPosition()).getPosition();
  352. if (innerPosition == null && lastElement.isGlue()) {
  353. // this adjustment applies to space-before or space-after of this block
  354. if (((KnuthGlue) lastElement).getAdjustmentClass() == SPACE_BEFORE_ADJUSTMENT) {
  355. // this adjustment applies to space-before
  356. adjustedSpaceBefore += adj;
  357. /*LF*/ //System.out.println(" BLM.negotiateBPDAdjustment> spazio prima: " + adj);
  358. } else {
  359. // this adjustment applies to space-after
  360. adjustedSpaceAfter += adj;
  361. /*LF*/ //System.out.println(" BLM.negotiateBPDAdjustment> spazio dopo: " + adj);
  362. }
  363. return adj;
  364. } else if (innerPosition instanceof MappingPosition) {
  365. // this block has block-progression-unit > 0: the adjustment can concern
  366. // - the space-before or space-after of this block,
  367. // - the line number of a descendant of this block
  368. MappingPosition mappingPos = (MappingPosition)innerPosition;
  369. if (lastElement.isGlue()) {
  370. // lastElement is a glue
  371. /*LF*/ //System.out.println(" BLM.negotiateBPDAdjustment> bpunit con glue");
  372. ListIterator storedListIterator = storedList.listIterator(
  373. mappingPos.getFirstIndex());
  374. int newAdjustment = 0;
  375. while (storedListIterator.nextIndex() <= mappingPos.getLastIndex()) {
  376. KnuthElement storedElement = (KnuthElement)storedListIterator.next();
  377. if (storedElement.isGlue()) {
  378. newAdjustment += ((BlockLevelLayoutManager)storedElement
  379. .getLayoutManager()).negotiateBPDAdjustment(
  380. adj - newAdjustment, storedElement);
  381. /*LF*/ //System.out.println(" BLM.negotiateBPDAdjustment> (progressivo) righe: "
  382. // + newAdjustment);
  383. }
  384. }
  385. newAdjustment = (newAdjustment > 0 ? bpUnit * neededUnits(newAdjustment)
  386. : -bpUnit * neededUnits(-newAdjustment));
  387. return newAdjustment;
  388. } else {
  389. // lastElement is a penalty: this means that the paragraph
  390. // has been split between consecutive pages:
  391. // this may involve a change in the number of lines
  392. /*LF*/ //System.out.println(" BLM.negotiateBPDAdjustment> bpunit con penalty");
  393. KnuthPenalty storedPenalty = (KnuthPenalty)
  394. storedList.get(mappingPos.getLastIndex());
  395. if (storedPenalty.getW() > 0) {
  396. // the original penalty has width > 0
  397. /*LF*/ //System.out.println(" BLM.negotiateBPDAdjustment> chiamata passata");
  398. return ((BlockLevelLayoutManager)storedPenalty.getLayoutManager())
  399. .negotiateBPDAdjustment(storedPenalty.getW(),
  400. (KnuthElement)storedPenalty);
  401. } else {
  402. // the original penalty has width = 0
  403. // the adjustment involves only the spaces before and after
  404. /*LF*/ //System.out.println(" BLM.negotiateBPDAdjustment> chiamata gestita");
  405. return adj;
  406. }
  407. }
  408. } else if (innerPosition.getLM() != this) {
  409. // this adjustment concerns another LM
  410. NonLeafPosition savedPos = (NonLeafPosition) lastElement.getPosition();
  411. lastElement.setPosition(innerPosition);
  412. int returnValue = ((BlockLevelLayoutManager)lastElement.getLayoutManager())
  413. .negotiateBPDAdjustment(adj, lastElement);
  414. lastElement.setPosition(savedPos);
  415. /*LF*/ //System.out.println(" BLM.negotiateBPDAdjustment> righe: " + returnValue);
  416. return returnValue;
  417. } else {
  418. // this should never happen
  419. log.error("BlockLayoutManager.negotiateBPDAdjustment(): unexpected Position");
  420. return 0;
  421. }
  422. }
  423. /**
  424. * @see BlockLevelLayoutManager#discardSpace(KnuthGlue)
  425. */
  426. public void discardSpace(KnuthGlue spaceGlue) {
  427. //System.out.println(" BLM.discardSpace> " + spaceGlue.getPosition().getClass().getName());
  428. Position innerPosition = ((NonLeafPosition) spaceGlue.getPosition()).getPosition();
  429. if (innerPosition == null || innerPosition.getLM() == this) {
  430. // if this block has block-progression-unit > 0, innerPosition can be
  431. // a MappingPosition
  432. // spaceGlue represents space before or space after of this block
  433. if (spaceGlue.getAdjustmentClass() == SPACE_BEFORE_ADJUSTMENT) {
  434. // space-before must be discarded
  435. adjustedSpaceBefore = 0;
  436. foSpaceBefore = new MinOptMax(0);
  437. } else {
  438. // space-after must be discarded
  439. adjustedSpaceAfter = 0;
  440. foSpaceAfter = new MinOptMax(0);
  441. //TODO Why are both cases handled in the same way?
  442. }
  443. } else {
  444. // this element was not created by this BlockLM
  445. NonLeafPosition savedPos = (NonLeafPosition)spaceGlue.getPosition();
  446. spaceGlue.setPosition(innerPosition);
  447. ((BlockLevelLayoutManager) spaceGlue.getLayoutManager()).discardSpace(spaceGlue);
  448. spaceGlue.setPosition(savedPos);
  449. }
  450. }
  451. /**
  452. * @see LayoutManager#getChangedKnuthElements(List, int)
  453. */
  454. public LinkedList getChangedKnuthElements(List oldList, int alignment) {
  455. /*LF*/ //System.out.println("");
  456. /*LF*/ //System.out.println(" BLM.getChangedKnuthElements> inizio: oldList.size() = "
  457. // + oldList.size());
  458. ListIterator oldListIterator = oldList.listIterator();
  459. KnuthElement returnedElement;
  460. KnuthElement currElement = null;
  461. KnuthElement prevElement = null;
  462. LinkedList returnedList = new LinkedList();
  463. LinkedList returnList = new LinkedList();
  464. int fromIndex = 0;
  465. // "unwrap" the Positions stored in the elements
  466. KnuthElement oldElement = null;
  467. while (oldListIterator.hasNext()) {
  468. oldElement = (KnuthElement)oldListIterator.next();
  469. Position innerPosition = ((NonLeafPosition) oldElement.getPosition()).getPosition();
  470. //System.out.println(" BLM> unwrapping: "
  471. // + (oldElement.isBox() ? "box " : (oldElement.isGlue() ? "glue " : "penalty"))
  472. // + " creato da " + oldElement.getLayoutManager().getClass().getName());
  473. //System.out.println(" BLM> unwrapping: "
  474. // + oldElement.getPosition().getClass().getName());
  475. if (innerPosition != null) {
  476. // oldElement was created by a descendant of this BlockLM
  477. oldElement.setPosition(innerPosition);
  478. } else {
  479. // thisElement was created by this BlockLM
  480. // modify its position in order to recognize it was not created
  481. // by a child
  482. oldElement.setPosition(new Position(this));
  483. }
  484. }
  485. // create the iterator
  486. List workList;
  487. if (bpUnit == 0) {
  488. workList = oldList;
  489. } else {
  490. // the storedList must be used instead of oldList;
  491. // find the index of the first element of returnedList
  492. // corresponding to the first element of oldList
  493. oldListIterator = oldList.listIterator();
  494. KnuthElement el = (KnuthElement) oldListIterator.next();
  495. while (!(el.getPosition() instanceof MappingPosition)) {
  496. el = (KnuthElement) oldListIterator.next();
  497. }
  498. int iFirst = ((MappingPosition) el.getPosition()).getFirstIndex();
  499. // find the index of the last element of returnedList
  500. // corresponding to the last element of oldList
  501. oldListIterator = oldList.listIterator(oldList.size());
  502. el = (KnuthElement) oldListIterator.previous();
  503. while (!(el.getPosition() instanceof MappingPosition)) {
  504. el = (KnuthElement) oldListIterator.previous();
  505. }
  506. int iLast = ((MappingPosition) el.getPosition()).getLastIndex();
  507. //System.out.println(" si usa storedList da " + iFirst + " a " + iLast
  508. // + " compresi su " + storedList.size() + " elementi totali");
  509. workList = storedList.subList(iFirst, iLast + 1);
  510. }
  511. ListIterator workListIterator = workList.listIterator();
  512. //System.out.println("");
  513. //System.out.println(" BLM.getChangedKnuthElements> workList.size() = "
  514. // + workList.size() + " da 0 a " + (workList.size() - 1));
  515. while (workListIterator.hasNext()) {
  516. currElement = (KnuthElement) workListIterator.next();
  517. //System.out.println("elemento n. " + workListIterator.previousIndex()
  518. // + " nella workList");
  519. if (prevElement != null
  520. && prevElement.getLayoutManager() != currElement.getLayoutManager()) {
  521. // prevElement is the last element generated by the same LM
  522. BlockLevelLayoutManager prevLM = (BlockLevelLayoutManager)
  523. prevElement.getLayoutManager();
  524. BlockLevelLayoutManager currLM = (BlockLevelLayoutManager)
  525. currElement.getLayoutManager();
  526. boolean bSomethingAdded = false;
  527. if (prevLM != this) {
  528. //System.out.println(" BLM.getChangedKnuthElements> chiamata da "
  529. // + fromIndex + " a " + workListIterator.previousIndex() + " su "
  530. // + prevLM.getClass().getName());
  531. returnedList.addAll(prevLM.getChangedKnuthElements(workList.subList(
  532. fromIndex, workListIterator.previousIndex()), alignment));
  533. bSomethingAdded = true;
  534. } else {
  535. // prevLM == this
  536. // do nothing
  537. //System.out.println(" BLM.getChangedKnuthElements> elementi propri, "
  538. // + "ignorati, da " + fromIndex + " a " + workListIterator.previousIndex()
  539. // + " su " + prevLM.getClass().getName());
  540. }
  541. fromIndex = workListIterator.previousIndex();
  542. // there is another block after this one
  543. if (bSomethingAdded
  544. && (this.mustKeepTogether()
  545. || prevLM.mustKeepWithNext()
  546. || currLM.mustKeepWithPrevious())) {
  547. // add an infinite penalty to forbid a break between blocks
  548. returnedList.add(new KnuthPenalty(0, KnuthElement.INFINITE, false,
  549. new Position(this), false));
  550. } else if (bSomethingAdded && !((KnuthElement) returnedList.getLast()).isGlue()) {
  551. // add a null penalty to allow a break between blocks
  552. returnedList.add(new KnuthPenalty(0, 0, false, new Position(this), false));
  553. }
  554. }
  555. prevElement = currElement;
  556. }
  557. if (currElement != null) {
  558. BlockLevelLayoutManager currLM = (BlockLevelLayoutManager)
  559. currElement.getLayoutManager();
  560. if (currLM != this) {
  561. //System.out.println(" BLM.getChangedKnuthElements> chiamata da " + fromIndex
  562. // + " a " + oldList.size() + " su " + currLM.getClass().getName());
  563. returnedList.addAll(currLM.getChangedKnuthElements(
  564. workList.subList(fromIndex, workList.size()), alignment));
  565. } else {
  566. // currLM == this
  567. // there are no more elements to add
  568. // remove the last penalty added to returnedList
  569. if (returnedList.size() > 0) {
  570. returnedList.removeLast();
  571. }
  572. //System.out.println(" BLM.getChangedKnuthElements> elementi propri, ignorati, da "
  573. // + fromIndex + " a " + workList.size());
  574. }
  575. }
  576. // append elements representing space-before
  577. boolean spaceBeforeIsConditional = true;
  578. if (fobj instanceof org.apache.fop.fo.flow.Block) {
  579. spaceBeforeIsConditional = ((org.apache.fop.fo.flow.Block)fobj)
  580. .getCommonMarginBlock().spaceBefore.getSpace().isDiscard();
  581. }
  582. if (bpUnit > 0
  583. || adjustedSpaceBefore != 0) {
  584. if (!spaceBeforeIsConditional) {
  585. // add elements to prevent the glue to be discarded
  586. returnList.add(new KnuthBox(0,
  587. new NonLeafPosition(this, null), false));
  588. returnList.add(new KnuthPenalty(0, KnuthElement.INFINITE, false,
  589. new NonLeafPosition(this, null), false));
  590. }
  591. if (bpUnit > 0) {
  592. returnList.add(new KnuthGlue(0, 0, 0,
  593. SPACE_BEFORE_ADJUSTMENT, new NonLeafPosition(this, null), true));
  594. } else {
  595. returnList.add(new KnuthGlue(adjustedSpaceBefore, 0, 0,
  596. SPACE_BEFORE_ADJUSTMENT, new NonLeafPosition(this, null), true));
  597. }
  598. }
  599. //System.out.println(" BLM.getChangedKnuthElements> intermedio: returnedList.size() = "
  600. // + returnedList.size());
  601. /* estensione: conversione complessiva */
  602. /*LF*/ if (bpUnit > 0) {
  603. /*LF*/ storedList = returnedList;
  604. /*LF*/ returnedList = createUnitElements(returnedList);
  605. /*LF*/ }
  606. /* estensione */
  607. // "wrap" the Position stored in each element of returnedList
  608. // and add elements to returnList
  609. ListIterator listIter = returnedList.listIterator();
  610. while (listIter.hasNext()) {
  611. returnedElement = (KnuthElement)listIter.next();
  612. returnedElement.setPosition(new NonLeafPosition(this, returnedElement.getPosition()));
  613. returnList.add(returnedElement);
  614. }
  615. // append elements representing space-after
  616. boolean spaceAfterIsConditional = true;
  617. if (fobj instanceof org.apache.fop.fo.flow.Block) {
  618. spaceAfterIsConditional = ((org.apache.fop.fo.flow.Block)fobj)
  619. .getCommonMarginBlock().spaceAfter.getSpace().isDiscard();
  620. }
  621. if (bpUnit > 0 || adjustedSpaceAfter != 0) {
  622. if (!spaceAfterIsConditional) {
  623. returnList.add(new KnuthPenalty(0,
  624. KnuthElement.INFINITE, false,
  625. new NonLeafPosition(this, null), false));
  626. }
  627. if (bpUnit > 0) {
  628. returnList.add(new KnuthGlue(0, 0, 0,
  629. SPACE_AFTER_ADJUSTMENT,
  630. new NonLeafPosition(this, null),
  631. (!spaceAfterIsConditional) ? false : true));
  632. } else {
  633. returnList.add(new KnuthGlue(adjustedSpaceAfter, 0, 0,
  634. SPACE_AFTER_ADJUSTMENT,
  635. new NonLeafPosition(this, null),
  636. (!spaceAfterIsConditional) ? false : true));
  637. }
  638. if (!spaceAfterIsConditional) {
  639. returnList.add(new KnuthBox(0,
  640. new NonLeafPosition(this, null), true));
  641. }
  642. }
  643. //System.out.println(" BLM.getChangedKnuthElements> finished: returnList.size() = "
  644. // + returnList.size());
  645. return returnList;
  646. }
  647. /**
  648. * @see org.apache.fop.layoutmgr.BlockLevelLayoutManager#mustKeepTogether()
  649. */
  650. public boolean mustKeepTogether() {
  651. return false;
  652. }
  653. /**
  654. * @see org.apache.fop.layoutmgr.BlockLevelLayoutManager#mustKeepWithPrevious()
  655. */
  656. public boolean mustKeepWithPrevious() {
  657. return false;
  658. }
  659. /**
  660. * @see org.apache.fop.layoutmgr.BlockLevelLayoutManager#mustKeepWithNext()
  661. */
  662. public boolean mustKeepWithNext() {
  663. return false;
  664. }
  665. /**
  666. * Adds the unresolved elements for border and padding to a layout context so break
  667. * possibilities can be properly constructed.
  668. * @param context the layout context
  669. */
  670. protected void addPendingMarks(LayoutContext context) {
  671. SpaceProperty spaceBefore = getSpaceBeforeProperty();
  672. if (spaceBefore != null
  673. && !(spaceBefore.getMinimum(this).getLength().getValue(this) == 0
  674. && spaceBefore.getMaximum(this).getLength().getValue(this) == 0)) {
  675. context.addPendingBeforeMark(new SpaceElement(getAuxiliaryPosition(), spaceBefore,
  676. RelSide.BEFORE,
  677. true, false, this));
  678. }
  679. SpaceProperty spaceAfter = getSpaceAfterProperty();
  680. if (spaceAfter != null
  681. && !(spaceAfter.getMinimum(this).getLength().getValue(this) == 0
  682. && spaceAfter.getMaximum(this).getLength().getValue(this) == 0)) {
  683. context.addPendingAfterMark(new SpaceElement(getAuxiliaryPosition(), spaceAfter,
  684. RelSide.AFTER,
  685. false, true, this));
  686. }
  687. CommonBorderPaddingBackground borderAndPadding = getBorderPaddingBackground();
  688. if (borderAndPadding != null) {
  689. if (borderAndPadding.getBorderBeforeWidth(false) > 0) {
  690. context.addPendingBeforeMark(new BorderElement(
  691. getAuxiliaryPosition(),
  692. borderAndPadding.getBorderInfo(
  693. CommonBorderPaddingBackground.BEFORE).getWidth(),
  694. RelSide.BEFORE,
  695. false, false, this));
  696. }
  697. if (borderAndPadding.getPaddingBefore(false, this) > 0) {
  698. context.addPendingBeforeMark(new PaddingElement(
  699. getAuxiliaryPosition(),
  700. borderAndPadding.getPaddingLengthProperty(
  701. CommonBorderPaddingBackground.BEFORE),
  702. RelSide.BEFORE,
  703. false, false, this));
  704. }
  705. if (borderAndPadding.getBorderAfterWidth(false) > 0) {
  706. context.addPendingAfterMark(new BorderElement(
  707. getAuxiliaryPosition(),
  708. borderAndPadding.getBorderInfo(
  709. CommonBorderPaddingBackground.AFTER).getWidth(),
  710. RelSide.AFTER,
  711. false, false, this));
  712. }
  713. if (borderAndPadding.getPaddingAfter(false, this) > 0) {
  714. context.addPendingAfterMark(new PaddingElement(
  715. getAuxiliaryPosition(),
  716. borderAndPadding.getPaddingLengthProperty(
  717. CommonBorderPaddingBackground.AFTER),
  718. RelSide.AFTER,
  719. false, false, this));
  720. }
  721. }
  722. }
  723. /** @return the border, padding and background info structure */
  724. private CommonBorderPaddingBackground getBorderPaddingBackground() {
  725. if (fobj instanceof org.apache.fop.fo.flow.Block) {
  726. return ((org.apache.fop.fo.flow.Block)fobj)
  727. .getCommonBorderPaddingBackground();
  728. } else if (fobj instanceof org.apache.fop.fo.flow.BlockContainer) {
  729. return ((org.apache.fop.fo.flow.BlockContainer)fobj)
  730. .getCommonBorderPaddingBackground();
  731. } else if (fobj instanceof org.apache.fop.fo.flow.ListBlock) {
  732. return ((org.apache.fop.fo.flow.ListBlock)fobj)
  733. .getCommonBorderPaddingBackground();
  734. } else if (fobj instanceof org.apache.fop.fo.flow.ListItem) {
  735. return ((org.apache.fop.fo.flow.ListItem)fobj)
  736. .getCommonBorderPaddingBackground();
  737. } else if (fobj instanceof org.apache.fop.fo.flow.Table) {
  738. return ((org.apache.fop.fo.flow.Table)fobj)
  739. .getCommonBorderPaddingBackground();
  740. } else {
  741. return null;
  742. }
  743. }
  744. /** @return the space-before property */
  745. private SpaceProperty getSpaceBeforeProperty() {
  746. if (fobj instanceof org.apache.fop.fo.flow.Block) {
  747. return ((org.apache.fop.fo.flow.Block)fobj)
  748. .getCommonMarginBlock().spaceBefore;
  749. } else if (fobj instanceof org.apache.fop.fo.flow.BlockContainer) {
  750. return ((org.apache.fop.fo.flow.BlockContainer)fobj)
  751. .getCommonMarginBlock().spaceBefore;
  752. } else if (fobj instanceof org.apache.fop.fo.flow.ListBlock) {
  753. return ((org.apache.fop.fo.flow.ListBlock)fobj)
  754. .getCommonMarginBlock().spaceBefore;
  755. } else if (fobj instanceof org.apache.fop.fo.flow.ListItem) {
  756. return ((org.apache.fop.fo.flow.ListItem)fobj)
  757. .getCommonMarginBlock().spaceBefore;
  758. } else if (fobj instanceof org.apache.fop.fo.flow.Table) {
  759. return ((org.apache.fop.fo.flow.Table)fobj)
  760. .getCommonMarginBlock().spaceBefore;
  761. } else {
  762. return null;
  763. }
  764. }
  765. /** @return the space-after property */
  766. private SpaceProperty getSpaceAfterProperty() {
  767. if (fobj instanceof org.apache.fop.fo.flow.Block) {
  768. return ((org.apache.fop.fo.flow.Block)fobj)
  769. .getCommonMarginBlock().spaceAfter;
  770. } else if (fobj instanceof org.apache.fop.fo.flow.BlockContainer) {
  771. return ((org.apache.fop.fo.flow.BlockContainer)fobj)
  772. .getCommonMarginBlock().spaceAfter;
  773. } else if (fobj instanceof org.apache.fop.fo.flow.ListBlock) {
  774. return ((org.apache.fop.fo.flow.ListBlock)fobj)
  775. .getCommonMarginBlock().spaceAfter;
  776. } else if (fobj instanceof org.apache.fop.fo.flow.ListItem) {
  777. return ((org.apache.fop.fo.flow.ListItem)fobj)
  778. .getCommonMarginBlock().spaceAfter;
  779. } else if (fobj instanceof org.apache.fop.fo.flow.Table) {
  780. return ((org.apache.fop.fo.flow.Table)fobj)
  781. .getCommonMarginBlock().spaceAfter;
  782. } else {
  783. return null;
  784. }
  785. }
  786. /**
  787. * Creates Knuth elements for before border padding and adds them to the return list.
  788. * @param returnList return list to add the additional elements to
  789. */
  790. protected void addKnuthElementsForBorderPaddingBefore(LinkedList returnList, boolean isFirst) {
  791. //Border and Padding (before)
  792. CommonBorderPaddingBackground borderAndPadding = getBorderPaddingBackground();
  793. if (borderAndPadding != null) {
  794. if (borderAndPadding.getBorderBeforeWidth(false) > 0) {
  795. returnList.add(new BorderElement(
  796. getAuxiliaryPosition(),
  797. borderAndPadding.getBorderInfo(CommonBorderPaddingBackground.BEFORE)
  798. .getWidth(),
  799. RelSide.BEFORE, isFirst, false, this));
  800. }
  801. if (borderAndPadding.getPaddingBefore(false, this) > 0) {
  802. returnList.add(new PaddingElement(
  803. getAuxiliaryPosition(),
  804. borderAndPadding.getPaddingLengthProperty(
  805. CommonBorderPaddingBackground.BEFORE),
  806. RelSide.BEFORE, isFirst, false, this));
  807. }
  808. //TODO Handle conditionality
  809. /*
  810. int bpBefore = borderAndPadding.getBorderBeforeWidth(false)
  811. + borderAndPadding.getPaddingBefore(false, this);
  812. if (bpBefore > 0) {
  813. returnList.add(new KnuthBox(bpBefore, getAuxiliaryPosition(), true));
  814. }*/
  815. }
  816. }
  817. /**
  818. * Creates Knuth elements for after border padding and adds them to the return list.
  819. * @param returnList return list to add the additional elements to
  820. */
  821. protected void addKnuthElementsForBorderPaddingAfter(LinkedList returnList, boolean isLast) {
  822. //Border and Padding (after)
  823. CommonBorderPaddingBackground borderAndPadding = getBorderPaddingBackground();
  824. if (borderAndPadding != null) {
  825. if (borderAndPadding.getPaddingAfter(false, this) > 0) {
  826. returnList.add(new PaddingElement(
  827. getAuxiliaryPosition(),
  828. borderAndPadding.getPaddingLengthProperty(
  829. CommonBorderPaddingBackground.AFTER),
  830. RelSide.AFTER, false, isLast, this));
  831. }
  832. if (borderAndPadding.getBorderAfterWidth(false) > 0) {
  833. returnList.add(new BorderElement(
  834. getAuxiliaryPosition(),
  835. borderAndPadding.getBorderInfo(CommonBorderPaddingBackground.AFTER)
  836. .getWidth(),
  837. RelSide.AFTER, false, isLast, this));
  838. }
  839. //TODO Handle conditionality
  840. /*
  841. int bpAfter = borderAndPadding.getBorderAfterWidth(false)
  842. + borderAndPadding.getPaddingAfter(false, this);
  843. if (bpAfter > 0) {
  844. returnList.add(new KnuthBox(bpAfter, getAuxiliaryPosition(), true));
  845. }*/
  846. }
  847. }
  848. /**
  849. * Creates Knuth elements for break-before and adds them to the return list.
  850. * @param returnList return list to add the additional elements to
  851. * @return true if an element has been added due to a break-before.
  852. */
  853. protected boolean addKnuthElementsForBreakBefore(LinkedList returnList,
  854. LayoutContext context) {
  855. int breakBefore = -1;
  856. if (fobj instanceof org.apache.fop.fo.flow.Block) {
  857. breakBefore = ((org.apache.fop.fo.flow.Block) fobj).getBreakBefore();
  858. } else if (fobj instanceof org.apache.fop.fo.flow.BlockContainer) {
  859. breakBefore = ((org.apache.fop.fo.flow.BlockContainer) fobj).getBreakBefore();
  860. } else if (fobj instanceof org.apache.fop.fo.flow.Table) {
  861. breakBefore = ((org.apache.fop.fo.flow.Table) fobj).getBreakBefore();
  862. }
  863. if (breakBefore == EN_PAGE
  864. || breakBefore == EN_COLUMN
  865. || breakBefore == EN_EVEN_PAGE
  866. || breakBefore == EN_ODD_PAGE) {
  867. // return a penalty element, representing a forced page break
  868. returnList.add(new BreakElement(getAuxiliaryPosition(),
  869. 0, -KnuthElement.INFINITE, breakBefore, context));
  870. //returnList.add(new KnuthPenalty(0, -KnuthElement.INFINITE, false,
  871. // breakBefore, getAuxiliaryPosition(), false));
  872. return true;
  873. } else {
  874. return false;
  875. }
  876. }
  877. /**
  878. * Creates Knuth elements for break-after and adds them to the return list.
  879. * @param returnList return list to add the additional elements to
  880. * @return true if an element has been added due to a break-after.
  881. */
  882. protected boolean addKnuthElementsForBreakAfter(LinkedList returnList,
  883. LayoutContext context) {
  884. int breakAfter = -1;
  885. if (fobj instanceof org.apache.fop.fo.flow.Block) {
  886. breakAfter = ((org.apache.fop.fo.flow.Block) fobj).getBreakAfter();
  887. } else if (fobj instanceof org.apache.fop.fo.flow.BlockContainer) {
  888. breakAfter = ((org.apache.fop.fo.flow.BlockContainer) fobj).getBreakAfter();
  889. } else if (fobj instanceof org.apache.fop.fo.flow.Table) {
  890. breakAfter = ((org.apache.fop.fo.flow.Table) fobj).getBreakAfter();
  891. }
  892. if (breakAfter == EN_PAGE
  893. || breakAfter == EN_COLUMN
  894. || breakAfter == EN_EVEN_PAGE
  895. || breakAfter == EN_ODD_PAGE) {
  896. // add a penalty element, representing a forced page break
  897. returnList.add(new BreakElement(getAuxiliaryPosition(),
  898. 0, -KnuthElement.INFINITE, breakAfter, context));
  899. //returnList.add(new KnuthPenalty(0, -KnuthElement.INFINITE, false,
  900. // breakAfter, getAuxiliaryPosition(), false));
  901. return true;
  902. } else {
  903. return false;
  904. }
  905. }
  906. /**
  907. * Creates Knuth elements for space-before and adds them to the return list.
  908. * @param returnList return list to add the additional elements to
  909. * @param alignment vertical alignment
  910. */
  911. protected void addKnuthElementsForSpaceBefore(LinkedList returnList/*,
  912. Position returnPosition*/, int alignment) {
  913. SpaceProperty spaceBefore = getSpaceBeforeProperty();
  914. // append elements representing space-before
  915. if (spaceBefore != null
  916. && !(spaceBefore.getMinimum(this).getLength().getValue(this) == 0
  917. && spaceBefore.getMaximum(this).getLength().getValue(this) == 0)) {
  918. returnList.add(new SpaceElement(getAuxiliaryPosition(), spaceBefore,
  919. RelSide.BEFORE,
  920. true, false, this));
  921. }
  922. /*
  923. if (bpUnit > 0
  924. || spaceBefore != null
  925. && !(spaceBefore.getMinimum(this).getLength().getValue(this) == 0
  926. && spaceBefore.getMaximum(this).getLength().getValue(this) == 0)) {
  927. if (spaceBefore != null && !spaceBefore.getSpace().isDiscard()) {
  928. // add elements to prevent the glue to be discarded
  929. returnList.add(new KnuthBox(0, getAuxiliaryPosition(), false));
  930. returnList.add(new KnuthPenalty(0, KnuthElement.INFINITE,
  931. false, getAuxiliaryPosition(), false));
  932. }
  933. if (bpUnit > 0) {
  934. returnList.add(new KnuthGlue(0, 0, 0,
  935. BlockLevelLayoutManager.SPACE_BEFORE_ADJUSTMENT,
  936. getAuxiliaryPosition(), true));
  937. } else { //if (alignment == EN_JUSTIFY) {
  938. returnList.add(new KnuthGlue(
  939. spaceBefore.getOptimum(this).getLength().getValue(this),
  940. spaceBefore.getMaximum(this).getLength().getValue(this)
  941. - spaceBefore.getOptimum(this).getLength().getValue(this),
  942. spaceBefore.getOptimum(this).getLength().getValue(this)
  943. - spaceBefore.getMinimum(this).getLength().getValue(this),
  944. BlockLevelLayoutManager.SPACE_BEFORE_ADJUSTMENT,
  945. getAuxiliaryPosition(), true));
  946. // } else {
  947. // returnList.add(new KnuthGlue(
  948. // spaceBefore.getOptimum().getLength().getValue(this),
  949. // 0, 0, BlockLevelLayoutManager.SPACE_BEFORE_ADJUSTMENT,
  950. // returnPosition, true));
  951. }
  952. }*/
  953. }
  954. /**
  955. * Creates Knuth elements for space-after and adds them to the return list.
  956. * @param returnList return list to add the additional elements to
  957. * @param alignment vertical alignment
  958. */
  959. protected void addKnuthElementsForSpaceAfter(LinkedList returnList/*, Position returnPosition*/,
  960. int alignment) {
  961. SpaceProperty spaceAfter = getSpaceAfterProperty();
  962. // append elements representing space-after
  963. if (spaceAfter != null
  964. && !(spaceAfter.getMinimum(this).getLength().getValue(this) == 0
  965. && spaceAfter.getMaximum(this).getLength().getValue(this) == 0)) {
  966. returnList.add(new SpaceElement(getAuxiliaryPosition(), spaceAfter,
  967. RelSide.AFTER,
  968. false, true, this));
  969. }
  970. /*
  971. if (bpUnit > 0
  972. || spaceAfter != null
  973. && !(spaceAfter.getMinimum(this).getLength().getValue(this) == 0
  974. && spaceAfter.getMaximum(this).getLength().getValue(this) == 0)) {
  975. if (spaceAfter != null && !spaceAfter.getSpace().isDiscard()) {
  976. returnList.add(new KnuthPenalty(0, KnuthElement.INFINITE,
  977. false, getAuxiliaryPosition(), false));
  978. }
  979. if (bpUnit > 0) {
  980. returnList.add(new KnuthGlue(0, 0, 0,
  981. BlockLevelLayoutManager.SPACE_AFTER_ADJUSTMENT,
  982. getAuxiliaryPosition(), true));
  983. } else { //if (alignment == EN_JUSTIFY) {
  984. returnList.add(new KnuthGlue(
  985. spaceAfter.getOptimum(this).getLength().getValue(this),
  986. spaceAfter.getMaximum(this).getLength().getValue(this)
  987. - spaceAfter.getOptimum(this).getLength().getValue(this),
  988. spaceAfter.getOptimum(this).getLength().getValue(this)
  989. - spaceAfter.getMinimum(this).getLength().getValue(this),
  990. BlockLevelLayoutManager.SPACE_AFTER_ADJUSTMENT, getAuxiliaryPosition(),
  991. (!spaceAfter.getSpace().isDiscard()) ? false : true));
  992. // } else {
  993. // returnList.add(new KnuthGlue(
  994. // spaceAfter.getOptimum().getLength().getValue(this), 0, 0,
  995. // BlockLevelLayoutManager.SPACE_AFTER_ADJUSTMENT, returnPosition,
  996. // (!spaceAfter.getSpace().isDiscard()) ? false : true));
  997. }
  998. if (spaceAfter != null && !spaceAfter.getSpace().isDiscard()) {
  999. returnList.add(new KnuthBox(0, getAuxiliaryPosition(), true));
  1000. }
  1001. }*/
  1002. }
  1003. protected LinkedList createUnitElements(LinkedList oldList) {
  1004. //System.out.println(" ");
  1005. //System.out.println("Start conversion: " + oldList.size()
  1006. // + " elements, space-before.min=" + layoutProps.spaceBefore.getSpace().min
  1007. // + " space-after.min=" + layoutProps.spaceAfter.getSpace().min);
  1008. // add elements at the beginning and at the end of oldList
  1009. // representing minimum spaces
  1010. LayoutManager lm = ((KnuthElement)oldList.getFirst()).getLayoutManager();
  1011. boolean bAddedBoxBefore = false;
  1012. boolean bAddedBoxAfter = false;
  1013. if (adjustedSpaceBefore > 0) {
  1014. oldList.addFirst(new KnuthBox(adjustedSpaceBefore,
  1015. new Position(lm), true));
  1016. bAddedBoxBefore = true;
  1017. }
  1018. if (adjustedSpaceAfter > 0) {
  1019. oldList.addLast(new KnuthBox(adjustedSpaceAfter,
  1020. new Position(lm), true));
  1021. bAddedBoxAfter = true;
  1022. }
  1023. MinOptMax totalLength = new MinOptMax(0);
  1024. MinOptMax totalUnits = new MinOptMax(0);
  1025. LinkedList newList = new LinkedList();
  1026. //System.out.println(" ");
  1027. //System.out.println(" Prima scansione");
  1028. // scan the list once to compute total min, opt and max length
  1029. ListIterator oldListIterator = oldList.listIterator();
  1030. while (oldListIterator.hasNext()) {
  1031. KnuthElement element = (KnuthElement) oldListIterator.next();
  1032. if (element.isBox()) {
  1033. totalLength.add(new MinOptMax(element.getW()));
  1034. //System.out.println("box " + element.getW());
  1035. } else if (element.isGlue()) {
  1036. totalLength.min -= ((KnuthGlue) element).getZ();
  1037. totalLength.max += ((KnuthGlue) element).getY();
  1038. //leafValue = ((LeafPosition) element.getPosition()).getLeafPos();
  1039. //System.out.println("glue " + element.getW() + " + "
  1040. // + ((KnuthGlue) element).getY() + " - " + ((KnuthGlue) element).getZ());
  1041. } else {
  1042. //System.out.println((((KnuthPenalty)element).getP() == KnuthElement.INFINITE
  1043. // ? "PENALTY " : "penalty ") + element.getW());
  1044. }
  1045. }
  1046. // compute the total amount of "units"
  1047. totalUnits = new MinOptMax(neededUnits(totalLength.min),
  1048. neededUnits(totalLength.opt),
  1049. neededUnits(totalLength.max));
  1050. //System.out.println(" totalLength= " + totalLength);
  1051. //System.out.println(" unita'= " + totalUnits);
  1052. //System.out.println(" ");
  1053. //System.out.println(" Seconda scansione");
  1054. // scan the list once more, stopping at every breaking point
  1055. // in order to compute partial min, opt and max length
  1056. // and create the new elements
  1057. oldListIterator = oldList.listIterator();
  1058. boolean bPrevIsBox = false;
  1059. MinOptMax lengthBeforeBreak = new MinOptMax(0);
  1060. MinOptMax lengthAfterBreak = (MinOptMax) totalLength.clone();
  1061. MinOptMax unitsBeforeBreak;
  1062. MinOptMax unitsAfterBreak;
  1063. MinOptMax unsuppressibleUnits = new MinOptMax(0);
  1064. int firstIndex = 0;
  1065. int lastIndex = -1;
  1066. while (oldListIterator.hasNext()) {
  1067. KnuthElement element = (KnuthElement) oldListIterator.next();
  1068. lastIndex++;
  1069. if (element.isBox()) {
  1070. lengthBeforeBreak.add(new MinOptMax(element.getW()));
  1071. lengthAfterBreak.subtract(new MinOptMax(element.getW()));
  1072. bPrevIsBox = true;
  1073. } else if (element.isGlue()) {
  1074. lengthBeforeBreak.min -= ((KnuthGlue) element).getZ();
  1075. lengthAfterBreak.min += ((KnuthGlue) element).getZ();
  1076. lengthBeforeBreak.max += ((KnuthGlue) element).getY();
  1077. lengthAfterBreak.max -= ((KnuthGlue) element).getY();
  1078. bPrevIsBox = false;
  1079. } else {
  1080. lengthBeforeBreak.add(new MinOptMax(element.getW()));
  1081. bPrevIsBox = false;
  1082. }
  1083. // create the new elements
  1084. if (element.isPenalty() && ((KnuthPenalty) element).getP() < KnuthElement.INFINITE
  1085. || element.isGlue() && bPrevIsBox
  1086. || !oldListIterator.hasNext()) {
  1087. // suppress elements after the breaking point
  1088. int iStepsForward = 0;
  1089. while (oldListIterator.hasNext()) {
  1090. KnuthElement el = (KnuthElement) oldListIterator.next();
  1091. iStepsForward++;
  1092. if (el.isGlue()) {
  1093. // suppressed glue
  1094. lengthAfterBreak.min += ((KnuthGlue) el).getZ();
  1095. lengthAfterBreak.max -= ((KnuthGlue) el).getY();
  1096. } else if (el.isPenalty()) {
  1097. // suppressed penalty, do nothing
  1098. } else {
  1099. // box, end of suppressions
  1100. break;
  1101. }
  1102. }
  1103. // compute the partial amount of "units" before and after the break
  1104. unitsBeforeBreak = new MinOptMax(neededUnits(lengthBeforeBreak.min),
  1105. neededUnits(lengthBeforeBreak.opt),
  1106. neededUnits(lengthBeforeBreak.max));
  1107. unitsAfterBreak = new MinOptMax(neededUnits(lengthAfterBreak.min),
  1108. neededUnits(lengthAfterBreak.opt),
  1109. neededUnits(lengthAfterBreak.max));
  1110. // rewind the iterator and lengthAfterBreak
  1111. for (int i = 0; i < iStepsForward; i++) {
  1112. KnuthElement el = (KnuthElement) oldListIterator.previous();
  1113. if (el.isGlue()) {
  1114. lengthAfterBreak.min -= ((KnuthGlue) el).getZ();
  1115. lengthAfterBreak.max += ((KnuthGlue) el).getY();
  1116. }
  1117. }
  1118. // compute changes in length, stretch and shrink
  1119. int uLengthChange = unitsBeforeBreak.opt + unitsAfterBreak.opt - totalUnits.opt;
  1120. int uStretchChange = (unitsBeforeBreak.max + unitsAfterBreak.max - totalUnits.max)
  1121. - (unitsBeforeBreak.opt + unitsAfterBreak.opt - totalUnits.opt);
  1122. int uShrinkChange = (unitsBeforeBreak.opt + unitsAfterBreak.opt - totalUnits.opt)
  1123. - (unitsBeforeBreak.min + unitsAfterBreak.min - totalUnits.min);
  1124. // compute the number of normal, stretch and shrink unit
  1125. // that must be added to the new sequence
  1126. int uNewNormal = unitsBeforeBreak.opt - unsuppressibleUnits.opt;
  1127. int uNewStretch = (unitsBeforeBreak.max - unitsBeforeBreak.opt)
  1128. - (unsuppressibleUnits.max - unsuppressibleUnits.opt);
  1129. int uNewShrink = (unitsBeforeBreak.opt - unitsBeforeBreak.min)
  1130. - (unsuppressibleUnits.opt - unsuppressibleUnits.min);
  1131. //System.out.println("("
  1132. // + unsuppressibleUnits.min + "-" + unsuppressibleUnits.opt + "-"
  1133. // + unsuppressibleUnits.max + ") "
  1134. // + " -> " + unitsBeforeBreak.min + "-" + unitsBeforeBreak.opt + "-"
  1135. // + unitsBeforeBreak.max
  1136. // + " + " + unitsAfterBreak.min + "-" + unitsAfterBreak.opt + "-"
  1137. // + unitsAfterBreak.max
  1138. // + (uLengthChange != 0 ? " [length " + uLengthChange + "] " : "")
  1139. // + (uStretchChange != 0 ? " [stretch " + uStretchChange + "] " : "")
  1140. // + (uShrinkChange != 0 ? " [shrink " + uShrinkChange + "]" : ""));
  1141. // create the MappingPosition which will be stored in the new elements
  1142. // correct firstIndex and lastIndex
  1143. int firstIndexCorrection = 0;
  1144. int lastIndexCorrection = 0;
  1145. if (bAddedBoxBefore) {
  1146. if (firstIndex != 0) {
  1147. firstIndexCorrection++;
  1148. }
  1149. lastIndexCorrection++;
  1150. }
  1151. if (bAddedBoxAfter && lastIndex == (oldList.size() - 1)) {
  1152. lastIndexCorrection++;
  1153. }
  1154. MappingPosition mappingPos = new MappingPosition(this,
  1155. firstIndex - firstIndexCorrection,
  1156. lastIndex - lastIndexCorrection);
  1157. // new box
  1158. newList.add(new KnuthBox((uNewNormal - uLengthChange) * bpUnit,
  1159. mappingPos,
  1160. false));
  1161. unsuppressibleUnits.add(new MinOptMax(uNewNormal - uLengthChange));
  1162. //System.out.println(" box " + (uNewNormal - uLengthChange));
  1163. // new infinite penalty, glue and box, if necessary
  1164. if (uNewStretch - uStretchChange > 0
  1165. || uNewShrink - uShrinkChange > 0) {
  1166. int iStretchUnits = (uNewStretch - uStretchChange > 0
  1167. ? (uNewStretch - uStretchChange) : 0);
  1168. int iShrinkUnits = (uNewShrink - uShrinkChange > 0
  1169. ? (uNewShrink - uShrinkChange) : 0);
  1170. newList.add(new KnuthPenalty(0, KnuthElement.INFINITE, false,
  1171. mappingPos,
  1172. false));
  1173. newList.add(new KnuthGlue(0,
  1174. iStretchUnits * bpUnit,
  1175. iShrinkUnits * bpUnit,
  1176. LINE_NUMBER_ADJUSTMENT,
  1177. mappingPos,
  1178. false));
  1179. //System.out.println(" PENALTY");
  1180. //System.out.println(" glue 0 " + iStretchUnits + " " + iShrinkUnits);
  1181. unsuppressibleUnits.max += iStretchUnits;
  1182. unsuppressibleUnits.min -= iShrinkUnits;
  1183. if (!oldListIterator.hasNext()) {
  1184. newList.add(new KnuthBox(0,
  1185. mappingPos,
  1186. false));
  1187. //System.out.println(" box 0");
  1188. }
  1189. }
  1190. // new breaking sequence
  1191. if (uStretchChange != 0
  1192. || uShrinkChange != 0) {
  1193. // new infinite penalty, glue, penalty and glue
  1194. newList.add(new KnuthPenalty(0, KnuthElement.INFINITE, false,
  1195. mappingPos,
  1196. false));
  1197. newList.add(new KnuthGlue(0,
  1198. uStretchChange * bpUnit,
  1199. uShrinkChange * bpUnit,
  1200. LINE_NUMBER_ADJUSTMENT,
  1201. mappingPos,
  1202. false));
  1203. newList.add(new KnuthPenalty(uLengthChange * bpUnit,
  1204. 0, false, element.getPosition(), false));
  1205. newList.add(new KnuthGlue(0,
  1206. -uStretchChange * bpUnit,
  1207. -uShrinkChange * bpUnit,
  1208. LINE_NUMBER_ADJUSTMENT,
  1209. mappingPos,
  1210. false));
  1211. //System.out.println(" PENALTY");
  1212. //System.out.println(" glue 0 " + uStretchChange + " " + uShrinkChange);
  1213. //System.out.println(" penalty " + uLengthChange + " * unit");
  1214. //System.out.println(" glue 0 " + (- uStretchChange) + " "
  1215. // + (- uShrinkChange));
  1216. } else if (oldListIterator.hasNext()) {
  1217. // new penalty
  1218. newList.add(new KnuthPenalty(uLengthChange * bpUnit,
  1219. 0, false,
  1220. mappingPos,
  1221. false));
  1222. //System.out.println(" penalty " + uLengthChange + " * unit");
  1223. }
  1224. // update firstIndex
  1225. firstIndex = lastIndex + 1;
  1226. }
  1227. if (element.isPenalty()) {
  1228. lengthBeforeBreak.add(new MinOptMax(-element.getW()));
  1229. }
  1230. }
  1231. // remove elements at the beginning and at the end of oldList
  1232. // representing minimum spaces
  1233. if (adjustedSpaceBefore > 0) {
  1234. oldList.removeFirst();
  1235. }
  1236. if (adjustedSpaceAfter > 0) {
  1237. oldList.removeLast();
  1238. }
  1239. // if space-before.conditionality is "discard", correct newList
  1240. boolean correctFirstElement = false;
  1241. if (fobj instanceof org.apache.fop.fo.flow.Block) {
  1242. correctFirstElement = ((org.apache.fop.fo.flow.Block)fobj)
  1243. .getCommonMarginBlock().spaceBefore.getSpace().isDiscard();
  1244. }
  1245. if (correctFirstElement) {
  1246. // remove the wrong element
  1247. KnuthBox wrongBox = (KnuthBox) newList.removeFirst();
  1248. // if this paragraph is at the top of a page, the space before
  1249. // must be ignored; compute the length change
  1250. int decreasedLength = (neededUnits(totalLength.opt)
  1251. - neededUnits(totalLength.opt - adjustedSpaceBefore))
  1252. * bpUnit;
  1253. // insert the correct elements
  1254. newList.addFirst(new KnuthBox(wrongBox.getW() - decreasedLength,
  1255. wrongBox.getPosition(), false));
  1256. newList.addFirst(new KnuthGlue(decreasedLength, 0, 0, SPACE_BEFORE_ADJUSTMENT,
  1257. wrongBox.getPosition(), false));
  1258. //System.out.println(" rimosso box " + neededUnits(wrongBox.getW()));
  1259. //System.out.println(" aggiunto glue " + neededUnits(decreasedLength) + " 0 0");
  1260. //System.out.println(" aggiunto box " + neededUnits(
  1261. // wrongBox.getW() - decreasedLength));
  1262. }
  1263. // if space-after.conditionality is "discard", correct newList
  1264. boolean correctLastElement = false;
  1265. if (fobj instanceof org.apache.fop.fo.flow.Block) {
  1266. correctLastElement = ((org.apache.fop.fo.flow.Block)fobj)
  1267. .getCommonMarginBlock().spaceAfter.getSpace().isDiscard();
  1268. }
  1269. if (correctLastElement) {
  1270. // remove the wrong element
  1271. KnuthBox wrongBox = (KnuthBox) newList.removeLast();
  1272. // if the old sequence is box(h) penalty(inf) glue(x,y,z) box(0)
  1273. // (it cannot be parted and has some stretch or shrink)
  1274. // the wrong box is the first one, not the last one
  1275. LinkedList preserveList = new LinkedList();
  1276. if (wrongBox.getW() == 0) {
  1277. preserveList.add(wrongBox);
  1278. preserveList.addFirst((KnuthGlue) newList.removeLast());
  1279. preserveList.addFirst((KnuthPenalty) newList.removeLast());
  1280. wrongBox = (KnuthBox) newList.removeLast();
  1281. }
  1282. // if this paragraph is at the bottom of a page, the space after
  1283. // must be ignored; compute the length change
  1284. int decreasedLength = (neededUnits(totalLength.opt)
  1285. - neededUnits(totalLength.opt - adjustedSpaceAfter))
  1286. * bpUnit;
  1287. // insert the correct box
  1288. newList.addLast(new KnuthBox(wrongBox.getW() - decreasedLength,
  1289. wrongBox.getPosition(), false));
  1290. // add preserved elements
  1291. if (preserveList.size() > 0) {
  1292. newList.addAll(preserveList);
  1293. }
  1294. // insert the correct glue
  1295. newList.addLast(new KnuthGlue(decreasedLength, 0, 0, SPACE_AFTER_ADJUSTMENT,
  1296. wrongBox.getPosition(), false));
  1297. //System.out.println(" rimosso box " + neededUnits(wrongBox.getW()));
  1298. //System.out.println(" aggiunto box " + neededUnits(
  1299. // wrongBox.getW() - decreasedLength));
  1300. //System.out.println(" aggiunto glue " + neededUnits(decreasedLength) + " 0 0");
  1301. }
  1302. return newList;
  1303. }
  1304. protected static class StackingIter extends PositionIterator {
  1305. StackingIter(Iterator parentIter) {
  1306. super(parentIter);
  1307. }
  1308. protected LayoutManager getLM(Object nextObj) {
  1309. return ((Position) nextObj).getLM();
  1310. }
  1311. protected Position getPos(Object nextObj) {
  1312. return ((Position) nextObj);
  1313. }
  1314. }
  1315. protected static class MappingPosition extends Position {
  1316. private int iFirstIndex;
  1317. private int iLastIndex;
  1318. public MappingPosition(LayoutManager lm, int first, int last) {
  1319. super(lm);
  1320. iFirstIndex = first;
  1321. iLastIndex = last;
  1322. }
  1323. public int getFirstIndex() {
  1324. return iFirstIndex;
  1325. }
  1326. public int getLastIndex() {
  1327. return iLastIndex;
  1328. }
  1329. }
  1330. /**
  1331. * "wrap" the Position inside each element moving the elements from
  1332. * SourceList to targetList
  1333. * @param sourceList source list
  1334. * @param targetList target list receiving the wrapped position elements
  1335. */
  1336. protected void wrapPositionElements(List sourceList, List targetList) {
  1337. wrapPositionElements(sourceList, targetList, false);
  1338. }
  1339. /**
  1340. * "wrap" the Position inside each element moving the elements from
  1341. * SourceList to targetList
  1342. * @param sourceList source list
  1343. * @param targetList target list receiving the wrapped position elements
  1344. * @param force if true, every Position is wrapper regardless of its LM of origin
  1345. */
  1346. protected void wrapPositionElements(List sourceList, List targetList, boolean force) {
  1347. ListIterator listIter = sourceList.listIterator();
  1348. while (listIter.hasNext()) {
  1349. ListElement tempElement;
  1350. tempElement = (ListElement) listIter.next();
  1351. if (force || tempElement.getLayoutManager() != this) {
  1352. tempElement.setPosition(notifyPos(new NonLeafPosition(this,
  1353. tempElement.getPosition())));
  1354. }
  1355. targetList.add(tempElement);
  1356. }
  1357. }
  1358. /**
  1359. * Returns the IPD of the content area
  1360. * @return the IPD of the content area
  1361. */
  1362. public int getContentAreaIPD() {
  1363. return contentAreaIPD;
  1364. }
  1365. /**
  1366. * Sets the IPD of the content area
  1367. * @param contentAreaIPD the IPD of the content area
  1368. */
  1369. protected void setContentAreaIPD(int contentAreaIPD) {
  1370. this.contentAreaIPD = contentAreaIPD;
  1371. }
  1372. /**
  1373. * Returns the BPD of the content area
  1374. * @return the BPD of the content area
  1375. */
  1376. public int getContentAreaBPD() {
  1377. return -1;
  1378. }
  1379. }