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

PageBreakingAlgorithm.java 51KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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.ArrayList;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import java.util.ListIterator;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.apache.fop.fo.Constants;
  26. import org.apache.fop.fo.FObj;
  27. import org.apache.fop.layoutmgr.AbstractBreaker.PageBreakPosition;
  28. import org.apache.fop.layoutmgr.BestFitPenalty.Variant;
  29. import org.apache.fop.traits.MinOptMax;
  30. import org.apache.fop.util.ListUtil;
  31. class PageBreakingAlgorithm extends BreakingAlgorithm {
  32. /** the logger for the class */
  33. private static Log log = LogFactory.getLog(PageBreakingAlgorithm.class);
  34. private final LayoutManager topLevelLM;
  35. private final PageProvider pageProvider;
  36. private final PageBreakingLayoutListener layoutListener;
  37. /** List of PageBreakPosition elements. */
  38. private LinkedList<PageBreakPosition> pageBreaks = null;
  39. /** Footnotes which are cited between the currently considered active node (previous
  40. * break) and the current considered break. Its type is
  41. * List&lt;List&lt;KnuthElement&gt;&gt;, it contains the sequences of KnuthElement
  42. * representing the footnotes bodies.
  43. */
  44. private List<List<KnuthElement>> footnotesList = null;
  45. /** Cumulated bpd of unhandled footnotes. */
  46. private List<Integer> lengthList = null;
  47. /** Length of all the footnotes which will be put on the current page. */
  48. private int totalFootnotesLength = 0;
  49. /**
  50. * Length of all the footnotes which have already been inserted, up to the currently
  51. * considered element. That is, footnotes from the currently considered page plus
  52. * footnotes from its preceding pages.
  53. */
  54. private int insertedFootnotesLength = 0;
  55. /** True if footnote citations have been met since the beginning of the page sequence. */
  56. private boolean footnotesPending = false;
  57. /** True if the elements met after the previous break point contain footnote citations. */
  58. private boolean newFootnotes = false;
  59. /** Index of the first footnote met after the previous break point. */
  60. private int firstNewFootnoteIndex = 0;
  61. /** Index of the last footnote inserted on the current page. */
  62. private int footnoteListIndex = 0;
  63. /** Index of the last element of the last footnote inserted on the current page. */
  64. private int footnoteElementIndex = -1;
  65. // demerits for a page break that splits a footnote
  66. private final int splitFootnoteDemerits = 5000;
  67. // demerits for a page break that defers a whole footnote to the following page
  68. private final int deferredFootnoteDemerits = 10000;
  69. private MinOptMax footnoteSeparatorLength = null;
  70. // the method noBreakBetween(int, int) uses these variables
  71. // to store parameters and result of the last call, in order
  72. // to reuse them and take less time
  73. private int storedPrevBreakIndex = -1;
  74. private int storedBreakIndex = -1;
  75. private boolean storedValue = false;
  76. //Controls whether overflows should be warned about or not
  77. private boolean autoHeight = false;
  78. //Controls whether a single part should be forced if possible (ex. block-container)
  79. private boolean favorSinglePart = false;
  80. private int ipdDifference;
  81. private KnuthNode bestNodeForIPDChange;
  82. //Used to keep track of switches in keep-context
  83. private int currentKeepContext = Constants.EN_AUTO;
  84. private KnuthNode lastBeforeKeepContextSwitch;
  85. /** Holds the variant of a dynamic content that must be attached to the next page node */
  86. private Variant variant;
  87. /**
  88. * Construct a page breaking algorithm.
  89. * @param topLevelLM the top level layout manager
  90. * @param pageProvider the page provider
  91. * @param layoutListener the layout listener
  92. * @param alignment alignment of the paragraph/page. One of {@link Constants#EN_START},
  93. * {@link Constants#EN_JUSTIFY}, {@link Constants#EN_CENTER},
  94. * {@link Constants#EN_END}.
  95. * For pages, {@link Constants#EN_BEFORE} and {@link Constants#EN_AFTER}
  96. * are mapped to the corresponding inline properties,
  97. * {@link Constants#EN_START} and {@link Constants#EN_END}.
  98. * @param alignmentLast alignment of the paragraph's last line
  99. * @param footnoteSeparatorLength length of footnote separator
  100. * @param partOverflowRecovery {@code true} if too long elements should be moved to
  101. * the next line/part
  102. * @param autoHeight true if auto height
  103. * @param favorSinglePart true if favoring single part
  104. * @see BreakingAlgorithm
  105. */
  106. public PageBreakingAlgorithm(LayoutManager topLevelLM,
  107. PageProvider pageProvider,
  108. PageBreakingLayoutListener layoutListener,
  109. int alignment, int alignmentLast,
  110. MinOptMax footnoteSeparatorLength,
  111. boolean partOverflowRecovery, boolean autoHeight,
  112. boolean favorSinglePart) {
  113. super(alignment, alignmentLast, true, partOverflowRecovery, 0);
  114. this.topLevelLM = topLevelLM;
  115. this.pageProvider = pageProvider;
  116. this.layoutListener = layoutListener;
  117. best = new BestPageRecords();
  118. this.footnoteSeparatorLength = footnoteSeparatorLength;
  119. this.autoHeight = autoHeight;
  120. this.favorSinglePart = favorSinglePart;
  121. }
  122. /**
  123. * This class represents a feasible breaking point
  124. * with extra information about footnotes.
  125. */
  126. protected class KnuthPageNode extends KnuthNode {
  127. /** Additional length due to already inserted footnotes. */
  128. public int insertedFootnotes;
  129. /** Total length of the footnotes. */
  130. public int totalFootnotes;
  131. /** Index of the last inserted footnote. */
  132. public int footnoteListIndex;
  133. /** Index of the last inserted element of the last inserted footnote. */
  134. public int footnoteElementIndex;
  135. public final Variant variant;
  136. public KnuthPageNode(int position,
  137. int line, int fitness,
  138. int totalWidth, int totalStretch, int totalShrink,
  139. int insertedFootnotes, int totalFootnotes,
  140. int footnoteListIndex, int footnoteElementIndex,
  141. double adjustRatio, int availableShrink, int availableStretch,
  142. int difference, double totalDemerits, KnuthNode previous, Variant variant) {
  143. super(position, line, fitness,
  144. totalWidth, totalStretch, totalShrink,
  145. adjustRatio, availableShrink, availableStretch,
  146. difference, totalDemerits, previous);
  147. this.totalFootnotes = totalFootnotes;
  148. this.insertedFootnotes = insertedFootnotes;
  149. this.footnoteListIndex = footnoteListIndex;
  150. this.footnoteElementIndex = footnoteElementIndex;
  151. this.variant = variant;
  152. }
  153. }
  154. /**
  155. * this class stores information about how the nodes
  156. * which could start a line ending at the current element
  157. */
  158. protected class BestPageRecords extends BestRecords {
  159. private final int[] bestInsertedFootnotesLength = new int[4];
  160. private final int[] bestTotalFootnotesLength = new int[4];
  161. private final int[] bestFootnoteListIndex = new int[4];
  162. private final int[] bestFootnoteElementIndex = new int[4];
  163. private final Variant[] bestVariant = new Variant[4];
  164. @Override
  165. public void addRecord(double demerits, KnuthNode node, double adjust,
  166. int availableShrink, int availableStretch,
  167. int difference, int fitness) {
  168. super.addRecord(demerits, node, adjust,
  169. availableShrink, availableStretch,
  170. difference, fitness);
  171. bestInsertedFootnotesLength[fitness] = insertedFootnotesLength;
  172. bestTotalFootnotesLength[fitness] = totalFootnotesLength;
  173. bestFootnoteListIndex[fitness] = footnoteListIndex;
  174. bestFootnoteElementIndex[fitness] = footnoteElementIndex;
  175. bestVariant[fitness] = variant;
  176. }
  177. public int getInsertedFootnotesLength(int fitness) {
  178. return bestInsertedFootnotesLength[fitness];
  179. }
  180. public int getTotalFootnotesLength(int fitness) {
  181. return bestTotalFootnotesLength[fitness];
  182. }
  183. public int getFootnoteListIndex(int fitness) {
  184. return bestFootnoteListIndex[fitness];
  185. }
  186. public int getFootnoteElementIndex(int fitness) {
  187. return bestFootnoteElementIndex[fitness];
  188. }
  189. public Variant getVariant(int fitness) {
  190. return bestVariant[fitness];
  191. }
  192. }
  193. /** {@inheritDoc} */
  194. @Override
  195. protected void initialize() {
  196. super.initialize();
  197. insertedFootnotesLength = 0;
  198. footnoteListIndex = 0;
  199. footnoteElementIndex = -1;
  200. }
  201. /**
  202. * Overridden to defer a part to the next page, if it
  203. * must be kept within one page, but is too large to fit in
  204. * the last column.
  205. * {@inheritDoc}
  206. */
  207. @Override
  208. protected KnuthNode recoverFromTooLong(KnuthNode lastTooLong) {
  209. if (log.isDebugEnabled()) {
  210. log.debug("Recovering from too long: " + lastTooLong);
  211. log.debug("\tlastTooShort = " + getLastTooShort());
  212. log.debug("\tlastBeforeKeepContextSwitch = " + lastBeforeKeepContextSwitch);
  213. log.debug("\tcurrentKeepContext = "
  214. + AbstractBreaker.getBreakClassName(currentKeepContext));
  215. }
  216. if (lastBeforeKeepContextSwitch == null
  217. || currentKeepContext == Constants.EN_AUTO) {
  218. return super.recoverFromTooLong(lastTooLong);
  219. }
  220. KnuthNode node = lastBeforeKeepContextSwitch;
  221. lastBeforeKeepContextSwitch = null;
  222. // content would overflow, insert empty page/column(s) and try again
  223. while (!pageProvider.endPage(node.line - 1)) {
  224. log.trace("Adding node for empty column");
  225. node = createNode(
  226. node.position,
  227. node.line + 1, 1,
  228. 0, 0, 0,
  229. 0, 0, 0,
  230. 0, 0, node);
  231. }
  232. return node;
  233. }
  234. /**
  235. * Compare two KnuthNodes and return the node with the least demerit.
  236. *
  237. * @param node1 The first knuth node.
  238. * @param node2 The other knuth node.
  239. * @return the node with the least demerit.
  240. */
  241. @Override
  242. protected KnuthNode compareNodes(KnuthNode node1, KnuthNode node2) {
  243. /* if either node is null, return the other one */
  244. if (node1 == null || node2 == null) {
  245. return (node1 == null) ? node2 : node1;
  246. }
  247. /* if either one of the nodes corresponds to a mere column-break,
  248. * and the other one corresponds to a page-break, return the page-break node
  249. */
  250. if (pageProvider != null) {
  251. if (pageProvider.endPage(node1.line - 1)
  252. && !pageProvider.endPage(node2.line - 1)) {
  253. return node1;
  254. } else if (pageProvider.endPage(node2.line - 1)
  255. && !pageProvider.endPage(node1.line - 1)) {
  256. return node2;
  257. }
  258. }
  259. /* all other cases: use superclass implementation */
  260. return super.compareNodes(node1, node2);
  261. }
  262. /** {@inheritDoc} */
  263. @Override
  264. protected KnuthNode createNode(int position,
  265. int line, int fitness,
  266. int totalWidth, int totalStretch, int totalShrink,
  267. double adjustRatio, int availableShrink, int availableStretch,
  268. int difference, double totalDemerits, KnuthNode previous) {
  269. return new KnuthPageNode(position, line, fitness,
  270. totalWidth, totalStretch, totalShrink,
  271. insertedFootnotesLength, totalFootnotesLength,
  272. footnoteListIndex, footnoteElementIndex,
  273. adjustRatio, availableShrink, availableStretch,
  274. difference, totalDemerits, previous, variant);
  275. }
  276. /** {@inheritDoc} */
  277. @Override
  278. protected KnuthNode createNode(int position, int line, int fitness,
  279. int totalWidth, int totalStretch, int totalShrink) {
  280. return new KnuthPageNode(position, line, fitness,
  281. totalWidth, totalStretch, totalShrink,
  282. ((BestPageRecords) best).getInsertedFootnotesLength(fitness),
  283. ((BestPageRecords) best).getTotalFootnotesLength(fitness),
  284. ((BestPageRecords) best).getFootnoteListIndex(fitness),
  285. ((BestPageRecords) best).getFootnoteElementIndex(fitness),
  286. best.getAdjust(fitness), best.getAvailableShrink(fitness),
  287. best.getAvailableStretch(fitness), best.getDifference(fitness),
  288. best.getDemerits(fitness), best.getNode(fitness),
  289. ((BestPageRecords) best).getVariant(fitness));
  290. }
  291. /**
  292. * Page-breaking specific handling of the given box. Currently it adds the footnotes
  293. * cited in the given box to the list of to-be-handled footnotes.
  294. * {@inheritDoc}
  295. */
  296. @Override
  297. protected void handleBox(KnuthBox box) {
  298. super.handleBox(box);
  299. if (box instanceof KnuthBlockBox
  300. && ((KnuthBlockBox) box).hasAnchors()) {
  301. handleFootnotes(((KnuthBlockBox) box).getElementLists());
  302. if (!newFootnotes) {
  303. newFootnotes = true;
  304. firstNewFootnoteIndex = footnotesList.size() - 1;
  305. }
  306. }
  307. }
  308. /**
  309. * Overridden to consider penalties with value {@link KnuthElement#INFINITE}
  310. * as legal break-points, if the current keep-context allows this
  311. * (a keep-*.within-page="always" constraint still permits column-breaks)
  312. * {@inheritDoc}
  313. */
  314. @Override
  315. protected void handlePenaltyAt(KnuthPenalty penalty, int position,
  316. int allowedBreaks) {
  317. super.handlePenaltyAt(penalty, position, allowedBreaks);
  318. /* if the penalty had value INFINITE, default implementation
  319. * will not have considered it a legal break, but it could still
  320. * be one.
  321. */
  322. if (penalty.getPenalty() == KnuthPenalty.INFINITE) {
  323. int breakClass = penalty.getBreakClass();
  324. if (breakClass == Constants.EN_PAGE
  325. || breakClass == Constants.EN_COLUMN) {
  326. considerLegalBreak(penalty, position);
  327. }
  328. }
  329. }
  330. /**
  331. * Handles the footnotes cited inside a block-level box. Updates footnotesList and the
  332. * value of totalFootnotesLength with the lengths of the given footnotes.
  333. * @param elementLists list of KnuthElement sequences corresponding to the footnotes
  334. * bodies
  335. */
  336. private void handleFootnotes(List<List<KnuthElement>> elementLists) {
  337. // initialization
  338. if (!footnotesPending) {
  339. footnotesPending = true;
  340. footnotesList = new ArrayList<List<KnuthElement>>();
  341. lengthList = new ArrayList<Integer>();
  342. totalFootnotesLength = 0;
  343. }
  344. if (!newFootnotes) {
  345. newFootnotes = true;
  346. firstNewFootnoteIndex = footnotesList.size();
  347. }
  348. // compute the total length of the footnotes
  349. for (List<KnuthElement> noteList : elementLists) {
  350. //Space resolution (Note: this does not respect possible stacking constraints
  351. //between footnotes!)
  352. SpaceResolver.resolveElementList(noteList);
  353. int noteLength = 0;
  354. footnotesList.add(noteList);
  355. for (KnuthElement element : noteList) {
  356. if (element.isBox() || element.isGlue()) {
  357. noteLength += element.getWidth();
  358. }
  359. }
  360. int prevLength = (lengthList == null || lengthList.isEmpty())
  361. ? 0
  362. : ListUtil.getLast(lengthList);
  363. if (lengthList != null) {
  364. lengthList.add(prevLength + noteLength);
  365. }
  366. totalFootnotesLength += noteLength;
  367. }
  368. }
  369. /** {@inheritDoc} */
  370. @Override
  371. protected int restartFrom(KnuthNode restartingNode, int currentIndex) {
  372. int returnValue = super.restartFrom(restartingNode, currentIndex);
  373. newFootnotes = false;
  374. if (footnotesPending) {
  375. // remove from footnotesList the note lists that will be met
  376. // after the restarting point
  377. for (int j = currentIndex; j >= restartingNode.position; j--) {
  378. final KnuthElement resetElement = getElement(j);
  379. if (resetElement instanceof KnuthBlockBox
  380. && ((KnuthBlockBox) resetElement).hasAnchors()) {
  381. resetFootnotes(((KnuthBlockBox) resetElement).getElementLists());
  382. }
  383. }
  384. assert restartingNode instanceof KnuthPageNode;
  385. KnuthPageNode restartingPageNode = (KnuthPageNode) restartingNode;
  386. footnoteElementIndex = restartingPageNode.footnoteElementIndex;
  387. footnoteListIndex = restartingPageNode.footnoteListIndex;
  388. totalFootnotesLength = restartingPageNode.totalFootnotes;
  389. insertedFootnotesLength = restartingPageNode.insertedFootnotes;
  390. }
  391. return returnValue;
  392. }
  393. private void resetFootnotes(List<List<KnuthElement>> elementLists) {
  394. for (int i = 0; i < elementLists.size(); i++) {
  395. ListUtil.removeLast(footnotesList);
  396. ListUtil.removeLast(lengthList);
  397. }
  398. // update footnotesPending;
  399. if (footnotesList.size() == 0) {
  400. footnotesPending = false;
  401. }
  402. }
  403. /** {@inheritDoc} */
  404. @Override
  405. protected void considerLegalBreak(KnuthElement element, int elementIdx) {
  406. if (element.isPenalty()) {
  407. int breakClass = ((KnuthPenalty) element).getBreakClass();
  408. switch (breakClass) {
  409. case Constants.EN_PAGE:
  410. if (this.currentKeepContext != breakClass) {
  411. this.lastBeforeKeepContextSwitch = getLastTooShort();
  412. }
  413. this.currentKeepContext = breakClass;
  414. break;
  415. case Constants.EN_COLUMN:
  416. if (this.currentKeepContext != breakClass) {
  417. this.lastBeforeKeepContextSwitch = getLastTooShort();
  418. }
  419. this.currentKeepContext = breakClass;
  420. break;
  421. case Constants.EN_AUTO:
  422. this.currentKeepContext = breakClass;
  423. break;
  424. default:
  425. //nop
  426. }
  427. }
  428. super.considerLegalBreak(element, elementIdx);
  429. newFootnotes = false;
  430. }
  431. /** {@inheritDoc} */
  432. @Override
  433. protected boolean elementCanEndLine(KnuthElement element, int line, int difference) {
  434. if (!(element.isPenalty()) || pageProvider == null) {
  435. return true;
  436. } else {
  437. KnuthPenalty p = (KnuthPenalty) element;
  438. if (p.getPenalty() <= 0) {
  439. return true;
  440. } else {
  441. int context = p.getBreakClass();
  442. switch (context) {
  443. case Constants.EN_LINE:
  444. case Constants.EN_COLUMN:
  445. return p.getPenalty() < KnuthPenalty.INFINITE;
  446. case Constants.EN_PAGE:
  447. return p.getPenalty() < KnuthPenalty.INFINITE
  448. || !pageProvider.endPage(line - 1);
  449. case Constants.EN_AUTO:
  450. log.debug("keep is not auto but context is");
  451. return true;
  452. default:
  453. if (p.getPenalty() < KnuthPenalty.INFINITE) {
  454. log.debug("Non recognized keep context:" + context);
  455. return true;
  456. } else {
  457. return false;
  458. }
  459. }
  460. }
  461. }
  462. }
  463. /** {@inheritDoc} */
  464. @Override
  465. protected int computeDifference(KnuthNode activeNode, KnuthElement element,
  466. int elementIndex) {
  467. KnuthPageNode pageNode = (KnuthPageNode) activeNode;
  468. int actualWidth = totalWidth - pageNode.totalWidth;
  469. int footnoteSplit;
  470. boolean canDeferOldFN;
  471. variant = null;
  472. if (element.isPenalty()) {
  473. if (element instanceof BestFitPenalty) {
  474. actualWidth += handleBestFitPenalty(activeNode, (BestFitPenalty) element, elementIndex);
  475. } else {
  476. actualWidth += element.getWidth();
  477. }
  478. }
  479. if (footnotesPending) {
  480. // compute the total length of the footnotes not yet inserted
  481. int allFootnotes = totalFootnotesLength - pageNode.insertedFootnotes;
  482. if (allFootnotes > 0) {
  483. // this page contains some footnote citations
  484. // add the footnote separator width
  485. actualWidth += footnoteSeparatorLength.getOpt();
  486. if (actualWidth + allFootnotes <= getLineWidth(activeNode.line)) {
  487. // there is enough space to insert all footnotes:
  488. // add the whole allFootnotes length
  489. actualWidth += allFootnotes;
  490. insertedFootnotesLength = pageNode.insertedFootnotes + allFootnotes;
  491. footnoteListIndex = footnotesList.size() - 1;
  492. footnoteElementIndex
  493. = getFootnoteList(footnoteListIndex).size() - 1;
  494. } else if (((canDeferOldFN = canDeferOldFootnotes(
  495. pageNode, elementIndex))
  496. || newFootnotes)
  497. && (footnoteSplit = getFootnoteSplit(
  498. pageNode, getLineWidth(activeNode.line) - actualWidth,
  499. canDeferOldFN)) > 0) {
  500. // it is allowed to break or even defer footnotes if either:
  501. // - there are new footnotes in the last piece of content, and
  502. // there is space to add at least a piece of the first one
  503. // - or the previous page break deferred some footnote lines, and
  504. // this is the first feasible break; in this case it is allowed
  505. // to break and defer, if necessary, old and new footnotes
  506. actualWidth += footnoteSplit;
  507. insertedFootnotesLength = pageNode.insertedFootnotes + footnoteSplit;
  508. // footnoteListIndex has been set in getFootnoteSplit()
  509. // footnoteElementIndex has been set in getFootnoteSplit()
  510. } else {
  511. // there is no space to add the smallest piece of footnote,
  512. // or we are trying to add a piece of content with no footnotes and
  513. // it does not fit in the page, because of previous footnote bodies
  514. // that cannot be broken:
  515. // add the whole allFootnotes length, so this breakpoint will be discarded
  516. actualWidth += allFootnotes;
  517. insertedFootnotesLength = pageNode.insertedFootnotes + allFootnotes;
  518. footnoteListIndex = footnotesList.size() - 1;
  519. footnoteElementIndex
  520. = getFootnoteList(footnoteListIndex).size() - 1;
  521. }
  522. } else {
  523. // all footnotes have already been placed on previous pages
  524. }
  525. } else {
  526. // there are no footnotes
  527. }
  528. int diff = getLineWidth(activeNode.line) - actualWidth;
  529. if (autoHeight && diff < 0) {
  530. //getLineWidth() for auto-height parts return 0 so the diff will be negative
  531. return 0; //...but we don't want to shrink in this case. Stick to optimum.
  532. } else {
  533. return diff;
  534. }
  535. }
  536. private int handleBestFitPenalty(KnuthNode activeNode, BestFitPenalty penalty, int elementIndex) {
  537. for (Variant var : penalty.getVariants()) {
  538. int difference = computeDifference(activeNode, var.toPenalty(), elementIndex);
  539. double r = computeAdjustmentRatio(activeNode, difference);
  540. if (r >= -1.0) {
  541. variant = var;
  542. return variant.width;
  543. }
  544. }
  545. return 0;
  546. }
  547. /**
  548. * Checks whether footnotes from preceding pages may be deferred to the page after
  549. * the given element.
  550. * @param node active node for the preceding page break
  551. * @param contentElementIndex index of the Knuth element considered for the
  552. * current page break
  553. * @return true if footnotes can be deferred
  554. */
  555. private boolean canDeferOldFootnotes(KnuthPageNode node, int contentElementIndex) {
  556. return (noBreakBetween(node.position, contentElementIndex)
  557. && deferredFootnotes(node.footnoteListIndex,
  558. node.footnoteElementIndex, node.insertedFootnotes));
  559. }
  560. /**
  561. * Returns true if there may be no breakpoint between the two given elements.
  562. * @param prevBreakIndex index of the element from the currently considered active
  563. * node
  564. * @param breakIndex index of the currently considered breakpoint
  565. * @return true if no element between the two can be a breakpoint
  566. */
  567. private boolean noBreakBetween(int prevBreakIndex, int breakIndex) {
  568. // this method stores the parameters and the return value from previous calls
  569. // in order to avoid scanning the element list unnecessarily:
  570. // - if there is no break between element #i and element #j
  571. // there will not be a break between #(i+h) and #j too
  572. // - if there is a break between element #i and element #j
  573. // there will be a break between #(i-h) and #(j+k) too
  574. if (storedPrevBreakIndex != -1
  575. && ((prevBreakIndex >= storedPrevBreakIndex
  576. && breakIndex == storedBreakIndex
  577. && storedValue)
  578. || (prevBreakIndex <= storedPrevBreakIndex
  579. && breakIndex >= storedBreakIndex
  580. && !storedValue))) {
  581. // use the stored value, do nothing
  582. } else {
  583. // compute the new value
  584. int index;
  585. // ignore suppressed elements
  586. for (index = prevBreakIndex + 1;
  587. !par.getElement(index).isBox();
  588. index++) {
  589. //nop
  590. }
  591. // find the next break
  592. for (;
  593. index < breakIndex;
  594. index++) {
  595. if (par.getElement(index).isGlue() && par.getElement(index - 1).isBox()
  596. || par.getElement(index).isPenalty()
  597. && ((KnuthElement) par
  598. .getElement(index)).getPenalty() < KnuthElement.INFINITE) {
  599. // break found
  600. break;
  601. }
  602. }
  603. // update stored parameters and value
  604. storedPrevBreakIndex = prevBreakIndex;
  605. storedBreakIndex = breakIndex;
  606. storedValue = (index == breakIndex);
  607. }
  608. return storedValue;
  609. }
  610. /**
  611. * Returns true if their are (pieces of) footnotes to be typeset on the current page.
  612. * @param listIndex index of the last inserted footnote for the currently considered
  613. * active node
  614. * @param elementIndex index of the last element of the last inserted footnote
  615. * @param length total length of all footnotes inserted so far
  616. */
  617. private boolean deferredFootnotes(int listIndex, int elementIndex, int length) {
  618. return ((newFootnotes
  619. && firstNewFootnoteIndex != 0
  620. && (listIndex < firstNewFootnoteIndex - 1
  621. || elementIndex < getFootnoteList(listIndex).size() - 1))
  622. || length < totalFootnotesLength);
  623. }
  624. /**
  625. * Tries to split the flow of footnotes to put one part on the current page.
  626. * @param activeNode currently considered previous page break
  627. * @param availableLength available space for footnotes
  628. * @param canDeferOldFootnotes
  629. * @return ...
  630. */
  631. private int getFootnoteSplit(KnuthPageNode activeNode, int availableLength,
  632. boolean canDeferOldFootnotes) {
  633. return getFootnoteSplit(activeNode.footnoteListIndex,
  634. activeNode.footnoteElementIndex,
  635. activeNode.insertedFootnotes,
  636. availableLength, canDeferOldFootnotes);
  637. }
  638. /**
  639. * Tries to split the flow of footnotes to put one part on the current page.
  640. * @param prevListIndex index of the last footnote on the previous page
  641. * @param prevElementIndex index of the last element of the last footnote
  642. * @param prevLength total length of footnotes inserted so far
  643. * @param availableLength available space for footnotes on this page
  644. * @param canDeferOldFootnotes
  645. * @return ...
  646. */
  647. private int getFootnoteSplit(int prevListIndex, int prevElementIndex, int prevLength,
  648. int availableLength, boolean canDeferOldFootnotes) {
  649. if (availableLength <= 0) {
  650. return 0;
  651. } else {
  652. // the split should contain a piece of the last footnote
  653. // together with all previous, not yet inserted footnotes;
  654. // but if this is not possible, try adding as much content as possible
  655. int splitLength = 0;
  656. ListIterator<KnuthElement> noteListIterator;
  657. KnuthElement element;
  658. boolean somethingAdded = false;
  659. // prevListIndex and prevElementIndex points to the last footnote element
  660. // already placed in a page: advance to the next element
  661. int listIndex = prevListIndex;
  662. int elementIndex = prevElementIndex;
  663. if (elementIndex == getFootnoteList(listIndex).size() - 1) {
  664. listIndex++;
  665. elementIndex = 0;
  666. } else {
  667. elementIndex++;
  668. }
  669. // try adding whole notes
  670. if (footnotesList.size() - 1 > listIndex) {
  671. // add the previous footnotes: these cannot be broken or deferred
  672. if (!canDeferOldFootnotes && newFootnotes && firstNewFootnoteIndex > 0) {
  673. splitLength = lengthList.get(firstNewFootnoteIndex - 1) - prevLength;
  674. listIndex = firstNewFootnoteIndex;
  675. elementIndex = 0;
  676. }
  677. // try adding the new footnotes
  678. while (lengthList.get(listIndex) - prevLength
  679. <= availableLength) {
  680. splitLength = lengthList.get(listIndex) - prevLength;
  681. somethingAdded = true;
  682. listIndex++;
  683. elementIndex = 0;
  684. }
  685. // as this method is called only if it is not possible to insert
  686. // all footnotes, at this point listIndex and elementIndex points to
  687. // an existing element, the next one we will try to insert
  688. }
  689. // try adding a split of the next note
  690. noteListIterator = getFootnoteList(listIndex).listIterator(elementIndex);
  691. int prevSplitLength = 0;
  692. int prevIndex = -1;
  693. int index = -1;
  694. while (splitLength <= availableLength) {
  695. if (somethingAdded) {
  696. prevSplitLength = splitLength;
  697. prevIndex = index;
  698. }
  699. // get a sub-sequence from the note element list
  700. boolean boxPreceding = false;
  701. while (noteListIterator.hasNext()) {
  702. // as this method is called only if it is not possible to insert
  703. // all footnotes, and we have already tried (and failed) to insert
  704. // this whole footnote, the while loop will never reach the end
  705. // of the note sequence
  706. element = noteListIterator.next();
  707. if (element.isBox()) {
  708. // element is a box
  709. splitLength += element.getWidth();
  710. boxPreceding = true;
  711. if (splitLength > prevSplitLength) {
  712. // and it is non-empty
  713. somethingAdded = true;
  714. }
  715. } else if (element.isGlue()) {
  716. // element is a glue
  717. if (boxPreceding) {
  718. // end of the sub-sequence
  719. index = noteListIterator.previousIndex();
  720. break;
  721. }
  722. boxPreceding = false;
  723. splitLength += element.getWidth();
  724. } else {
  725. // element is a penalty
  726. if (element.getPenalty() < KnuthElement.INFINITE) {
  727. // end of the sub-sequence
  728. index = noteListIterator.previousIndex();
  729. break;
  730. }
  731. boxPreceding = false;
  732. }
  733. }
  734. }
  735. // if prevSplitLength is 0, this means that the available length isn't enough
  736. // to insert even the smallest split of the last footnote, so we cannot end a
  737. // page here
  738. // if prevSplitLength is > 0 we can insert some footnote content in this page
  739. // and insert the remaining in the following one
  740. if (!somethingAdded) {
  741. // there was not enough space to add a piece of the first new footnote
  742. // this is not a good break
  743. prevSplitLength = 0;
  744. } else if (prevSplitLength > 0) {
  745. // prevIndex is -1 if we have added only some whole footnotes
  746. footnoteListIndex = (prevIndex != -1) ? listIndex : listIndex - 1;
  747. footnoteElementIndex = (prevIndex != -1)
  748. ? prevIndex
  749. : getFootnoteList(footnoteListIndex).size() - 1;
  750. }
  751. return prevSplitLength;
  752. }
  753. }
  754. /** {@inheritDoc} */
  755. @Override
  756. protected double computeAdjustmentRatio(KnuthNode activeNode, int difference) {
  757. // compute the adjustment ratio
  758. if (difference > 0) {
  759. int maxAdjustment = totalStretch - activeNode.totalStretch;
  760. // add the footnote separator stretch if some footnote content will be added
  761. if (((KnuthPageNode) activeNode).insertedFootnotes < totalFootnotesLength) {
  762. maxAdjustment += footnoteSeparatorLength.getStretch();
  763. }
  764. if (maxAdjustment > 0) {
  765. return (double) difference / maxAdjustment;
  766. } else {
  767. return INFINITE_RATIO;
  768. }
  769. } else if (difference < 0) {
  770. int maxAdjustment = totalShrink - activeNode.totalShrink;
  771. // add the footnote separator shrink if some footnote content will be added
  772. if (((KnuthPageNode) activeNode).insertedFootnotes < totalFootnotesLength) {
  773. maxAdjustment += footnoteSeparatorLength.getShrink();
  774. }
  775. if (maxAdjustment > 0) {
  776. return (double) difference / maxAdjustment;
  777. } else {
  778. return -INFINITE_RATIO;
  779. }
  780. } else {
  781. return 0;
  782. }
  783. }
  784. /** {@inheritDoc} */
  785. @Override
  786. protected double computeDemerits(KnuthNode activeNode, KnuthElement element,
  787. int fitnessClass, double r) {
  788. double demerits = 0;
  789. // compute demerits
  790. double f = Math.abs(r);
  791. f = 1 + 100 * f * f * f;
  792. if (element.isPenalty()) {
  793. double penalty = element.getPenalty();
  794. if (penalty >= 0) {
  795. f += penalty;
  796. demerits = f * f;
  797. } else if (!element.isForcedBreak()) {
  798. demerits = f * f - penalty * penalty;
  799. } else {
  800. demerits = f * f;
  801. }
  802. } else {
  803. demerits = f * f;
  804. }
  805. if (element.isPenalty() && ((KnuthPenalty) element).isPenaltyFlagged()
  806. && getElement(activeNode.position).isPenalty()
  807. && ((KnuthPenalty) getElement(activeNode.position)).isPenaltyFlagged()) {
  808. // add demerit for consecutive breaks at flagged penalties
  809. demerits += repeatedFlaggedDemerit;
  810. }
  811. if (Math.abs(fitnessClass - activeNode.fitness) > 1) {
  812. // add demerit for consecutive breaks
  813. // with very different fitness classes
  814. demerits += incompatibleFitnessDemerit;
  815. }
  816. if (footnotesPending) {
  817. if (footnoteListIndex < footnotesList.size() - 1) {
  818. // add demerits for the deferred footnotes
  819. demerits += (footnotesList.size() - 1 - footnoteListIndex)
  820. * deferredFootnoteDemerits;
  821. }
  822. if (footnoteListIndex < footnotesList.size()) {
  823. if (footnoteElementIndex
  824. < getFootnoteList(footnoteListIndex).size() - 1) {
  825. // add demerits for the footnote split between pages
  826. demerits += splitFootnoteDemerits;
  827. }
  828. } else {
  829. //TODO Why can this happen in the first place? Does anybody know? See #44160
  830. }
  831. }
  832. demerits += activeNode.totalDemerits;
  833. return demerits;
  834. }
  835. /** {@inheritDoc} */
  836. @Override
  837. protected void finish() {
  838. for (int i = startLine; i < endLine; i++) {
  839. for (KnuthPageNode node = (KnuthPageNode) getNode(i);
  840. node != null;
  841. node = (KnuthPageNode) node.next) {
  842. if (node.insertedFootnotes < totalFootnotesLength) {
  843. // layout remaining footnote bodies
  844. createFootnotePages(node);
  845. }
  846. }
  847. }
  848. }
  849. private void createFootnotePages(KnuthPageNode lastNode) {
  850. insertedFootnotesLength = lastNode.insertedFootnotes;
  851. footnoteListIndex = lastNode.footnoteListIndex;
  852. footnoteElementIndex = lastNode.footnoteElementIndex;
  853. int availableBPD = getLineWidth(lastNode.line);
  854. int split = 0;
  855. KnuthPageNode prevNode = lastNode;
  856. // create pages containing the remaining footnote bodies
  857. while (insertedFootnotesLength < totalFootnotesLength) {
  858. if (totalFootnotesLength - insertedFootnotesLength <= availableBPD) {
  859. // All the remaining footnotes fit
  860. insertedFootnotesLength = totalFootnotesLength;
  861. footnoteListIndex = lengthList.size() - 1;
  862. footnoteElementIndex = getFootnoteList(footnoteListIndex).size() - 1;
  863. } else if ((split = getFootnoteSplit(
  864. footnoteListIndex, footnoteElementIndex,
  865. insertedFootnotesLength, availableBPD, true)) > 0) {
  866. // add a piece of a footnote
  867. availableBPD -= split;
  868. insertedFootnotesLength += split;
  869. // footnoteListIndex has already been set in getFootnoteSplit()
  870. // footnoteElementIndex has already been set in getFootnoteSplit()
  871. } else {
  872. // cannot add any content: create a new node and start again
  873. KnuthPageNode node = (KnuthPageNode)
  874. createNode(lastNode.position, prevNode.line + 1, 1,
  875. insertedFootnotesLength - prevNode.insertedFootnotes,
  876. 0, 0,
  877. 0, 0, 0,
  878. 0, 0, prevNode);
  879. addNode(node.line, node);
  880. removeNode(prevNode.line, prevNode);
  881. prevNode = node;
  882. availableBPD = getLineWidth(node.line);
  883. }
  884. }
  885. // create the last node
  886. KnuthPageNode node = (KnuthPageNode)
  887. createNode(lastNode.position, prevNode.line + 1, 1,
  888. totalFootnotesLength - prevNode.insertedFootnotes, 0, 0,
  889. 0, 0, 0,
  890. 0, 0, prevNode);
  891. addNode(node.line, node);
  892. removeNode(prevNode.line, prevNode);
  893. }
  894. /**
  895. * @return a list of {@link PageBreakPosition} elements
  896. * corresponding to the computed page- and column-breaks
  897. */
  898. public LinkedList<PageBreakPosition> getPageBreaks() {
  899. return pageBreaks;
  900. }
  901. /**
  902. * Insert the given {@link PageBreakPosition} as the first
  903. * element in the list of page-breaks
  904. *
  905. * @param pageBreak the position to insert
  906. */
  907. public void insertPageBreakAsFirst(PageBreakPosition pageBreak) {
  908. if (pageBreaks == null) {
  909. pageBreaks = new LinkedList<PageBreakPosition>();
  910. }
  911. pageBreaks.addFirst(pageBreak);
  912. }
  913. /**
  914. * Removes all page breaks from the result list. This is used by block-containers and
  915. * static-content when it is only desired to know where there is an overflow but later the
  916. * whole content should be painted as one part.
  917. */
  918. public void removeAllPageBreaks() {
  919. if (pageBreaks == null || pageBreaks.isEmpty()) {
  920. return;
  921. }
  922. pageBreaks.subList(0, pageBreaks.size() - 1).clear();
  923. }
  924. /** {@inheritDoc} */
  925. @Override
  926. public void updateData1(int total, double demerits) {
  927. }
  928. /** {@inheritDoc} */
  929. @Override
  930. public void updateData2(KnuthNode bestActiveNode,
  931. KnuthSequence sequence,
  932. int total) {
  933. //int difference = (bestActiveNode.line < total)
  934. // ? bestActiveNode.difference : bestActiveNode.difference + fillerMinWidth;
  935. // Check if the given node has an attached variant of a dynamic content
  936. KnuthPageNode pageNode = (KnuthPageNode) bestActiveNode;
  937. if (pageNode.variant != null) {
  938. BestFitPenalty penalty = (BestFitPenalty) par.get(pageNode.position);
  939. penalty.setActiveVariant(pageNode.variant);
  940. }
  941. int difference = bestActiveNode.difference;
  942. if (difference + bestActiveNode.availableShrink < 0) {
  943. if (!autoHeight) {
  944. if (layoutListener != null) {
  945. layoutListener.notifyOverflow(bestActiveNode.line - 1, -difference, getFObj());
  946. }
  947. }
  948. }
  949. boolean isNonLastPage = (bestActiveNode.line < total);
  950. int blockAlignment = isNonLastPage ? alignment : alignmentLast;
  951. // it is always allowed to adjust space, so the ratio must be set regardless of
  952. // the value of the property display-align; the ratio must be <= 1
  953. double ratio = bestActiveNode.adjustRatio;
  954. if (ratio < 0) {
  955. // page break with a negative difference:
  956. // spaces always have enough shrink
  957. difference = 0;
  958. } else if (ratio <= 1 && isNonLastPage) {
  959. // not-last page break with a positive difference smaller than the available stretch:
  960. // spaces can stretch to fill the whole difference
  961. difference = 0;
  962. } else if (ratio > 1) {
  963. // not-last page with a positive difference greater than the available stretch
  964. // spaces can stretch to fill the difference only partially
  965. ratio = 1;
  966. difference -= bestActiveNode.availableStretch;
  967. } else {
  968. // last page with a positive difference:
  969. // spaces do not need to stretch
  970. if (blockAlignment != Constants.EN_JUSTIFY) {
  971. ratio = 0;
  972. } else {
  973. //Stretch as much as possible on last page
  974. difference = 0;
  975. }
  976. }
  977. // compute the indexes of the first footnote list and the first element in that list
  978. int firstListIndex = ((KnuthPageNode) bestActiveNode.previous).footnoteListIndex;
  979. int firstElementIndex = ((KnuthPageNode) bestActiveNode.previous).footnoteElementIndex;
  980. if (footnotesList != null
  981. && firstElementIndex == getFootnoteList(firstListIndex).size() - 1) {
  982. // advance to the next list
  983. firstListIndex++;
  984. firstElementIndex = 0;
  985. } else {
  986. firstElementIndex++;
  987. }
  988. // add nodes at the beginning of the list, as they are found
  989. // backwards, from the last one to the first one
  990. if (log.isDebugEnabled()) {
  991. log.debug("BBA> difference=" + difference + " ratio=" + ratio
  992. + " position=" + bestActiveNode.position);
  993. }
  994. insertPageBreakAsFirst(new PageBreakPosition(this.topLevelLM,
  995. bestActiveNode.position,
  996. firstListIndex, firstElementIndex,
  997. ((KnuthPageNode) bestActiveNode).footnoteListIndex,
  998. ((KnuthPageNode) bestActiveNode).footnoteElementIndex,
  999. ratio, difference));
  1000. }
  1001. /** {@inheritDoc} */
  1002. @Override
  1003. protected int filterActiveNodes() {
  1004. // leave only the active node with fewest total demerits
  1005. KnuthNode bestActiveNode = null;
  1006. for (int i = startLine; i < endLine; i++) {
  1007. for (KnuthNode node = getNode(i); node != null; node = node.next) {
  1008. if (favorSinglePart
  1009. && node.line > 1
  1010. && bestActiveNode != null
  1011. && Math.abs(bestActiveNode.difference) < bestActiveNode.availableShrink) {
  1012. //favor current best node, so just skip the current node because it would
  1013. //result in more than one part
  1014. } else {
  1015. bestActiveNode = compareNodes(bestActiveNode, node);
  1016. }
  1017. if (node != bestActiveNode) {
  1018. removeNode(i, node);
  1019. }
  1020. }
  1021. }
  1022. assert (bestActiveNode != null);
  1023. return bestActiveNode.line;
  1024. }
  1025. /**
  1026. * Obtain the element-list corresponding to the footnote at the given index.
  1027. *
  1028. * @param index the index in the list of footnotes
  1029. * @return the element-list
  1030. */
  1031. protected final List<KnuthElement> getFootnoteList(int index) {
  1032. return footnotesList.get(index);
  1033. }
  1034. /** @return the associated top-level formatting object. */
  1035. public FObj getFObj() {
  1036. return topLevelLM.getFObj();
  1037. }
  1038. /** {@inheritDoc} */
  1039. @Override
  1040. protected int getLineWidth(int line) {
  1041. int bpd;
  1042. if (pageProvider != null) {
  1043. bpd = pageProvider.getAvailableBPD(line);
  1044. } else {
  1045. bpd = super.getLineWidth(line);
  1046. }
  1047. if (log.isTraceEnabled()) {
  1048. log.trace("getLineWidth(" + line + ") -> " + bpd);
  1049. }
  1050. return bpd;
  1051. }
  1052. /**
  1053. * Interface to notify about layout events during page breaking.
  1054. */
  1055. public interface PageBreakingLayoutListener {
  1056. /**
  1057. * Issued when an overflow is detected
  1058. * @param part the number of the part (page) this happens on
  1059. * @param amount the amount by which the area overflows (in mpt)
  1060. * @param obj the root FO object where this happens
  1061. */
  1062. void notifyOverflow(int part, int amount, FObj obj);
  1063. }
  1064. @Override
  1065. protected KnuthNode recoverFromOverflow() {
  1066. if (compareIPDs(getLastTooLong().line - 1) != 0) {
  1067. /**
  1068. * If the IPD of the next page changes, disable the recovery mechanism as the
  1069. * inline content has to be re-laid out according to the new IPD anyway.
  1070. */
  1071. return getLastTooLong();
  1072. } else {
  1073. return super.recoverFromOverflow();
  1074. }
  1075. }
  1076. /** {@inheritDoc} */
  1077. @Override
  1078. protected int getIPDdifference() {
  1079. return ipdDifference;
  1080. }
  1081. /** {@inheritDoc} */
  1082. @Override
  1083. protected int handleIpdChange() {
  1084. log.trace("Best node for ipd change:" + bestNodeForIPDChange);
  1085. // TODO finish()
  1086. /*
  1087. * The third parameter is used to determine if this is the last page, so
  1088. * if the content must be vertically justified or not. If we are here
  1089. * this means that there is further content and the next page has a
  1090. * different ipd. So tweak the parameter to fall into the non-last-page
  1091. * case.
  1092. */
  1093. calculateBreakPoints(bestNodeForIPDChange, par, bestNodeForIPDChange.line + 1);
  1094. activeLines = null;
  1095. return bestNodeForIPDChange.line;
  1096. }
  1097. /**
  1098. * Add a node at the end of the given line's existing active nodes.
  1099. * If this is the first node in the line, adjust endLine accordingly.
  1100. * @param line number of the line ending at the node's corresponding breakpoint
  1101. * @param node the active node to add
  1102. */
  1103. @Override
  1104. protected void addNode(int line, KnuthNode node) {
  1105. if (node.position < par.size() - 1 && line > 0
  1106. && (ipdDifference = compareIPDs(line - 1)) != 0) {
  1107. log.trace("IPD changes at page " + line);
  1108. if (bestNodeForIPDChange == null
  1109. || node.totalDemerits < bestNodeForIPDChange.totalDemerits) {
  1110. bestNodeForIPDChange = node;
  1111. }
  1112. } else {
  1113. if (node.position == par.size() - 1) {
  1114. /*
  1115. * The whole sequence could actually fit on the last page before
  1116. * the IPD change. No need to do any special handling.
  1117. */
  1118. ipdDifference = 0;
  1119. }
  1120. super.addNode(line, node);
  1121. }
  1122. }
  1123. KnuthNode getBestNodeBeforeIPDChange() {
  1124. return bestNodeForIPDChange;
  1125. }
  1126. private int compareIPDs(int line) {
  1127. if (pageProvider == null) {
  1128. return 0;
  1129. }
  1130. return pageProvider.compareIPDs(line);
  1131. }
  1132. }