You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BreakingAlgorithm.java 53KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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 org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.apache.fop.fo.Constants;
  22. /**
  23. * The set of nodes is sorted into lines indexed into activeLines.
  24. * The nodes in each line are linked together in a single linked list by the
  25. * {@link KnuthNode#next} field. The activeLines array contains a link to the head of
  26. * the linked list in index 'line*2' and a link to the tail at index 'line*2+1'.
  27. * <p>
  28. * The set of active nodes can be traversed by
  29. * <pre>
  30. * for (int line = startLine; line &lt; endLine; line++) {
  31. * for (KnuthNode node = getNode(line); node != null; node = node.next) {
  32. * // Do something with 'node'
  33. * }
  34. * }
  35. * </pre>
  36. */
  37. public abstract class BreakingAlgorithm {
  38. /** the logger for the class */
  39. protected static Log log = LogFactory.getLog(BreakingAlgorithm.class);
  40. /** Maximum adjustment ration */
  41. protected static final int INFINITE_RATIO = 1000;
  42. private static final int MAX_RECOVERY_ATTEMPTS = 5;
  43. // constants identifying a subset of the feasible breaks
  44. /** All feasible breaks are ok. */
  45. public static final int ALL_BREAKS = 0;
  46. /** This forbids hyphenation. */
  47. public static final int NO_FLAGGED_PENALTIES = 1;
  48. /** wrap-option = "no-wrap". */
  49. public static final int ONLY_FORCED_BREAKS = 2;
  50. /** Holder for symbolic literals for the fitness classes */
  51. static final class FitnessClasses {
  52. static final int VERY_TIGHT = 0;
  53. static final int TIGHT = 1;
  54. static final int LOOSE = 2;
  55. static final int VERY_LOOSE = 3;
  56. static final String[] NAMES = { "VERY TIGHT", "TIGHT", "LOOSE", "VERY LOOSE" };
  57. /**
  58. * Figure out the fitness class of this line (tight, loose,
  59. * very tight or very loose).
  60. * See the section on "More Bells and Whistles" in Knuth's
  61. * "Breaking Paragraphs Into Lines".
  62. *
  63. * @param adjustRatio the adjustment ratio
  64. * @return the fitness class
  65. */
  66. static int computeFitness(double adjustRatio) {
  67. if (adjustRatio < -0.5) {
  68. return FitnessClasses.VERY_TIGHT;
  69. } else if (adjustRatio <= 0.5) {
  70. return FitnessClasses.TIGHT;
  71. } else if (adjustRatio <= 1.0) {
  72. return FitnessClasses.LOOSE;
  73. } else {
  74. return FitnessClasses.VERY_LOOSE;
  75. }
  76. }
  77. }
  78. // parameters of Knuth's algorithm:
  79. /** Demerit for consecutive lines ending at flagged penalties. */
  80. protected int repeatedFlaggedDemerit = KnuthPenalty.FLAGGED_PENALTY;
  81. /** Demerit for consecutive lines belonging to incompatible fitness classes . */
  82. protected int incompatibleFitnessDemerit = KnuthPenalty.FLAGGED_PENALTY;
  83. /** Maximum number of consecutive lines ending with a flagged penalty.
  84. * Only a value >= 1 is a significant limit.
  85. */
  86. protected int maxFlaggedPenaltiesCount;
  87. /**
  88. * The threshold for considering breaks to be acceptable. The adjustment ratio must be
  89. * inferior to this threshold.
  90. */
  91. private double threshold;
  92. /**
  93. * The paragraph of KnuthElements.
  94. */
  95. protected KnuthSequence par;
  96. /**
  97. * The width of a line (or height of a column in page-breaking mode).
  98. * -1 indicates that the line widths are different for each line.
  99. */
  100. protected int lineWidth = -1;
  101. /** Force the algorithm to find a set of breakpoints, even if no feasible breakpoints
  102. * exist.
  103. */
  104. private boolean force = false;
  105. /** If set to true, doesn't ignore break possibilities which are definitely too short. */
  106. protected boolean considerTooShort = false;
  107. /** When in forced mode, the best node leading to a too long line. The line will be
  108. * too long anyway, but this one will lead to a paragraph with fewest demerits.
  109. */
  110. private KnuthNode lastTooLong;
  111. /** When in forced mode, the best node leading to a too short line. The line will be
  112. * too short anyway, but this one will lead to a paragraph with fewest demerits.
  113. */
  114. private KnuthNode lastTooShort;
  115. /** The node to be reactivated if no set of feasible breakpoints can be found for this
  116. * paragraph.
  117. */
  118. private KnuthNode lastDeactivated;
  119. /** Alignment of the paragraph/page. One of EN_START, EN_JUSTIFY, etc. */
  120. protected int alignment;
  121. /** Alignment of the paragraph's last line. */
  122. protected int alignmentLast;
  123. /** Used to handle the text-indent property (indent the first line of a paragraph). */
  124. protected boolean indentFirstPart;
  125. /**
  126. * The set of active nodes in ascending line order. For each line l, activeLines[2l] contains a
  127. * link to l's first active node, and activeLines[2l+1] a link to l's last active node. The
  128. * line number l corresponds to the number of the line ending at the node's breakpoint.
  129. */
  130. protected KnuthNode[] activeLines;
  131. /**
  132. * The number of active nodes.
  133. */
  134. protected int activeNodeCount;
  135. /**
  136. * The lowest available line in the set of active nodes.
  137. */
  138. protected int startLine = 0;
  139. /**
  140. * The highest + 1 available line in the set of active nodes.
  141. */
  142. protected int endLine = 0;
  143. /**
  144. * The total width of all elements handled so far.
  145. */
  146. protected int totalWidth;
  147. /**
  148. * The total stretch of all elements handled so far.
  149. */
  150. protected int totalStretch = 0;
  151. /**
  152. * The total shrink of all elements handled so far.
  153. */
  154. protected int totalShrink = 0;
  155. protected BestRecords best;
  156. private boolean partOverflowRecoveryActivated = true;
  157. private KnuthNode lastRecovered;
  158. /**
  159. * Create a new instance.
  160. *
  161. * @param align alignment of the paragraph/page. One of {@link Constants#EN_START},
  162. * {@link Constants#EN_JUSTIFY}, {@link Constants#EN_CENTER},
  163. * {@link Constants#EN_END}.
  164. * For pages, {@link Constants#EN_BEFORE} and {@link Constants#EN_AFTER}
  165. * are mapped to the corresponding inline properties,
  166. * {@link Constants#EN_START} and {@link Constants#EN_END}.
  167. * @param alignLast alignment of the paragraph's last line
  168. * @param first for the text-indent property ({@code true} if the first line
  169. * of a paragraph should be indented)
  170. * @param partOverflowRecovery {@code true} if too long elements should be moved to
  171. * the next line/part
  172. * @param maxFlagCount maximum allowed number of consecutive lines ending at a flagged penalty
  173. * item
  174. */
  175. public BreakingAlgorithm(int align, int alignLast,
  176. boolean first, boolean partOverflowRecovery,
  177. int maxFlagCount) {
  178. this.alignment = align;
  179. this.alignmentLast = alignLast;
  180. this.indentFirstPart = first;
  181. this.partOverflowRecoveryActivated = partOverflowRecovery;
  182. this.best = new BestRecords();
  183. this.maxFlaggedPenaltiesCount = maxFlagCount;
  184. }
  185. /**
  186. * Class recording all the informations of a feasible breaking point.
  187. */
  188. public class KnuthNode {
  189. /** index of the breakpoint represented by this node */
  190. public final int position;
  191. /** number of the line ending at this breakpoint */
  192. public final int line;
  193. /** fitness class of the line ending at this breakpoint. One of 0, 1, 2, 3. */
  194. public final int fitness;
  195. /** accumulated width of the KnuthElements up to after this breakpoint. */
  196. public final int totalWidth;
  197. /** accumulated stretchability of the KnuthElements up to after this breakpoint. */
  198. public final int totalStretch;
  199. /** accumulated shrinkability of the KnuthElements up to after this breakpoint. */
  200. public final int totalShrink;
  201. /** adjustment ratio if the line ends at this breakpoint */
  202. public final double adjustRatio;
  203. /** available stretch of the line ending at this breakpoint */
  204. public final int availableShrink;
  205. /** available shrink of the line ending at this breakpoint */
  206. public final int availableStretch;
  207. /** difference between target and actual line width */
  208. public final int difference;
  209. /** minimum total demerits up to this breakpoint */
  210. public double totalDemerits;
  211. /** best node for the preceding breakpoint */
  212. public KnuthNode previous;
  213. /** next possible node in the same line */
  214. public KnuthNode next;
  215. /**
  216. * Holds the number of subsequent recovery attempty that are made to get content fit
  217. * into a line.
  218. */
  219. public int fitRecoveryCounter = 0;
  220. public KnuthNode(int position, int line, int fitness,
  221. int totalWidth, int totalStretch, int totalShrink,
  222. double adjustRatio, int availableShrink, int availableStretch,
  223. int difference, double totalDemerits, KnuthNode previous) {
  224. this.position = position;
  225. this.line = line;
  226. this.fitness = fitness;
  227. this.totalWidth = totalWidth;
  228. this.totalStretch = totalStretch;
  229. this.totalShrink = totalShrink;
  230. this.adjustRatio = adjustRatio;
  231. this.availableShrink = availableShrink;
  232. this.availableStretch = availableStretch;
  233. this.difference = difference;
  234. this.totalDemerits = totalDemerits;
  235. this.previous = previous;
  236. }
  237. public String toString() {
  238. return "<KnuthNode at " + position + " "
  239. + totalWidth + "+" + totalStretch + "-" + totalShrink
  240. + " line:" + line + " prev:" + (previous != null ? previous.position : -1)
  241. + " dem:" + totalDemerits
  242. + " fitness:" + FitnessClasses.NAMES[fitness] + ">";
  243. }
  244. }
  245. /** Class that stores, for each fitness class, the best active node that could start
  246. * a line of the corresponding fitness ending at the current element.
  247. */
  248. protected class BestRecords {
  249. private static final double INFINITE_DEMERITS = Double.POSITIVE_INFINITY;
  250. private double[] bestDemerits = new double[4];
  251. private KnuthNode[] bestNode = new KnuthNode[4];
  252. private double[] bestAdjust = new double[4];
  253. private int[] bestDifference = new int[4];
  254. private int[] bestAvailableShrink = new int[4];
  255. private int[] bestAvailableStretch = new int[4];
  256. /** Points to the fitness class which currently leads to the best demerits. */
  257. private int bestIndex = -1;
  258. public BestRecords() {
  259. reset();
  260. }
  261. /** Registers the new best active node for the given fitness class.
  262. * @param demerits the total demerits of the new optimal set of breakpoints
  263. * @param node the node starting the line ending at the current element
  264. * @param adjust adjustment ratio of the current line
  265. * @param availableShrink how much the current line can be shrinked
  266. * @param availableStretch how much the current line can be stretched
  267. * @param difference difference between the width of the considered line and the
  268. * width of the "real" line
  269. * @param fitness fitness class of the current line
  270. */
  271. public void addRecord(double demerits, KnuthNode node, double adjust,
  272. int availableShrink, int availableStretch,
  273. int difference, int fitness) {
  274. if (demerits > bestDemerits[fitness]) {
  275. log.error("New demerits value greater than the old one");
  276. }
  277. bestDemerits[fitness] = demerits;
  278. bestNode[fitness] = node;
  279. bestAdjust[fitness] = adjust;
  280. bestAvailableShrink[fitness] = availableShrink;
  281. bestAvailableStretch[fitness] = availableStretch;
  282. bestDifference[fitness] = difference;
  283. if (bestIndex == -1 || demerits < bestDemerits[bestIndex]) {
  284. bestIndex = fitness;
  285. }
  286. }
  287. public boolean hasRecords() {
  288. return (bestIndex != -1);
  289. }
  290. /**
  291. * @param fitness fitness class (0, 1, 2 or 3, i.e. "tight" to "very loose")
  292. * @return true if there is a set of feasible breakpoints registered for the
  293. * given fitness.
  294. */
  295. public boolean notInfiniteDemerits(int fitness) {
  296. return (bestDemerits[fitness] != INFINITE_DEMERITS);
  297. }
  298. public double getDemerits(int fitness) {
  299. return bestDemerits[fitness];
  300. }
  301. public KnuthNode getNode(int fitness) {
  302. return bestNode[fitness];
  303. }
  304. public double getAdjust(int fitness) {
  305. return bestAdjust[fitness];
  306. }
  307. public int getAvailableShrink(int fitness) {
  308. return bestAvailableShrink[fitness];
  309. }
  310. public int getAvailableStretch(int fitness) {
  311. return bestAvailableStretch[fitness];
  312. }
  313. public int getDifference(int fitness) {
  314. return bestDifference[fitness];
  315. }
  316. public double getMinDemerits() {
  317. if (bestIndex != -1) {
  318. return getDemerits(bestIndex);
  319. } else {
  320. // anyway, this should never happen
  321. return INFINITE_DEMERITS;
  322. }
  323. }
  324. /** Reset when a new breakpoint is being considered. */
  325. public void reset() {
  326. for (int i = 0; i < 4; i++) {
  327. bestDemerits[i] = INFINITE_DEMERITS;
  328. // there is no need to reset the other arrays
  329. }
  330. bestIndex = -1;
  331. }
  332. }
  333. /**
  334. * @return the number of times the algorithm should try to move overflowing content to the
  335. * next line/page.
  336. */
  337. protected int getMaxRecoveryAttempts() {
  338. return MAX_RECOVERY_ATTEMPTS;
  339. }
  340. /**
  341. * Controls the behaviour of the algorithm in cases where the first element of a part
  342. * overflows a line/page.
  343. * @return true if the algorithm should try to send the element to the next line/page.
  344. */
  345. protected boolean isPartOverflowRecoveryActivated() {
  346. return this.partOverflowRecoveryActivated;
  347. }
  348. /**
  349. * Empty method, hook for subclasses. Called before determining the optimal
  350. * breakpoints corresponding to a given active node.
  351. * @param total number of lines for the active node
  352. * @param demerits total demerits of the paragraph for the active node
  353. */
  354. public abstract void updateData1(int total, double demerits);
  355. /**
  356. * Empty method, hook for subclasses. Called when determining the optimal breakpoints
  357. * for a given active node.
  358. * @param bestActiveNode a node in the chain of best active nodes, corresponding to
  359. * one of the optimal breakpoints
  360. * @param sequence the corresponding paragraph
  361. * @param total the number of lines into which the paragraph will be broken
  362. */
  363. public abstract void updateData2(KnuthNode bestActiveNode,
  364. KnuthSequence sequence,
  365. int total);
  366. public void setConstantLineWidth(int lineWidth) {
  367. this.lineWidth = lineWidth;
  368. }
  369. /** @see #findBreakingPoints(KnuthSequence, int, double, boolean, int) */
  370. public int findBreakingPoints(KnuthSequence par,
  371. double threshold,
  372. boolean force,
  373. int allowedBreaks) {
  374. return findBreakingPoints(par, 0, threshold, force, allowedBreaks);
  375. }
  376. /**
  377. * Finds an optimal set of breakpoints for the given paragraph.
  378. *
  379. * @param par the paragraph to break
  380. * @param startIndex index of the Knuth element at which the breaking must start
  381. * @param threshold upper bound of the adjustment ratio
  382. * @param force {@code true} if a set of breakpoints must be found, even
  383. * if there are no feasible ones
  384. * @param allowedBreaks the type(s) of breaks allowed. One of {@link #ONLY_FORCED_BREAKS},
  385. * {@link #NO_FLAGGED_PENALTIES} or {@link #ALL_BREAKS}.
  386. *
  387. * @return the number of effective breaks
  388. */
  389. public int findBreakingPoints(KnuthSequence par, int startIndex,
  390. double threshold, boolean force,
  391. int allowedBreaks) {
  392. this.par = par;
  393. this.threshold = threshold;
  394. this.force = force;
  395. // initialize the algorithm
  396. initialize();
  397. // previous element in the paragraph is a KnuthBox?
  398. boolean previousIsBox = false;
  399. // index of the first KnuthBox in the sequence, in case of non-centered
  400. // alignment. For centered alignment, we need to take into account preceding
  401. // penalties+glues used for the filler spaces
  402. int firstBoxIndex = startIndex;
  403. if (alignment != Constants.EN_CENTER) {
  404. firstBoxIndex = par.getFirstBoxIndex(startIndex);
  405. }
  406. firstBoxIndex = (firstBoxIndex < 0) ? 0 : firstBoxIndex;
  407. // create an active node representing the starting point
  408. addNode(0, createNode(firstBoxIndex, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, null));
  409. KnuthNode lastForced = getNode(0);
  410. if (log.isTraceEnabled()) {
  411. log.trace("Looping over " + (par.size() - startIndex) + " elements");
  412. log.trace(par);
  413. }
  414. // main loop
  415. for (int elementIndex = startIndex; elementIndex < par.size(); elementIndex++) {
  416. previousIsBox = handleElementAt(
  417. elementIndex, previousIsBox, allowedBreaks).isBox();
  418. if (activeNodeCount == 0) {
  419. if (ipdChanged()) {
  420. return handleIpdChange();
  421. }
  422. if (!force) {
  423. log.debug("Could not find a set of breaking points " + threshold);
  424. return 0;
  425. }
  426. // lastDeactivated was a "good" break, while lastTooShort and lastTooLong
  427. // were "bad" breaks since the beginning;
  428. // if it is not the node we just restarted from, lastDeactivated can
  429. // replace either lastTooShort or lastTooLong
  430. if (lastDeactivated != null
  431. && lastDeactivated != lastForced) {
  432. replaceLastDeactivated();
  433. }
  434. if (lastTooShort == null
  435. || lastForced.position == lastTooShort.position) {
  436. lastForced = recoverFromOverflow();
  437. } else {
  438. lastForced = lastTooShort;
  439. this.lastRecovered = null;
  440. }
  441. elementIndex = restartFrom(lastForced, elementIndex);
  442. }
  443. }
  444. finish();
  445. // there is at least one set of breaking points
  446. // select one or more active nodes, removing the others from the list
  447. int line = filterActiveNodes();
  448. // for each active node, create a set of breaking points
  449. for (int i = startLine; i < endLine; i++) {
  450. for (KnuthNode node = getNode(i); node != null; node = node.next) {
  451. updateData1(node.line, node.totalDemerits);
  452. calculateBreakPoints(node, par, node.line);
  453. }
  454. }
  455. activeLines = null;
  456. return line;
  457. }
  458. protected boolean ipdChanged() {
  459. return false;
  460. }
  461. protected int handleIpdChange() {
  462. throw new IllegalStateException();
  463. }
  464. /**
  465. * Recover from a {@link KnuthNode} leading to a line that is too long.
  466. * The default implementation creates a new node corresponding to a break
  467. * point after the previous node that led to a line that was too short.
  468. *
  469. * @param lastTooLong the node that leads to a "too long" line
  470. * @return node corresponding to a breakpoint after the previous "too short" line
  471. */
  472. protected KnuthNode recoverFromTooLong(KnuthNode lastTooLong) {
  473. if (log.isDebugEnabled()) {
  474. log.debug("Recovering from too long: " + lastTooLong);
  475. }
  476. // content would overflow, insert empty line/page and try again
  477. return createNode(
  478. lastTooLong.previous.position, lastTooLong.previous.line + 1, 1,
  479. 0, 0, 0,
  480. 0, 0, 0,
  481. 0, 0, lastTooLong.previous);
  482. }
  483. /** Initializes the algorithm's variables. */
  484. protected void initialize() {
  485. this.totalWidth = 0;
  486. this.totalStretch = 0;
  487. this.totalShrink = 0;
  488. this.lastTooShort = this.lastTooLong = null;
  489. this.startLine = this.endLine = 0;
  490. this.activeLines = new KnuthNode[20];
  491. }
  492. /**
  493. * Creates a new active node for a feasible breakpoint at the given position. Only
  494. * called in forced mode.
  495. *
  496. * @param position index of the element in the Knuth sequence
  497. * @param line number of the line ending at the breakpoint
  498. * @param fitness fitness class of the line ending at the breakpoint. One of 0, 1, 2, 3.
  499. * @param totalWidth accumulated width of the KnuthElements up to after the breakpoint
  500. * @param totalStretch accumulated stretchability of the KnuthElements up to after the
  501. * breakpoint
  502. * @param totalShrink accumulated shrinkability of the KnuthElements up to after the
  503. * breakpoint
  504. * @param adjustRatio adjustment ratio if the line ends at this breakpoint
  505. * @param availableShrink available stretch of the line ending at this breakpoint
  506. * @param availableStretch available shrink of the line ending at this breakpoint
  507. * @param difference difference between target and actual line width
  508. * @param totalDemerits minimum total demerits up to the breakpoint
  509. * @param previous active node for the preceding breakpoint
  510. * @return a new node
  511. */
  512. protected KnuthNode createNode(int position, int line, int fitness,
  513. int totalWidth, int totalStretch, int totalShrink,
  514. double adjustRatio, int availableShrink, int availableStretch,
  515. int difference, double totalDemerits, KnuthNode previous) {
  516. return new KnuthNode(position, line, fitness,
  517. totalWidth, totalStretch, totalShrink,
  518. adjustRatio, availableShrink, availableStretch,
  519. difference, totalDemerits, previous);
  520. }
  521. /** Creates a new active node for a break from the best active node of the given
  522. * fitness class to the element at the given position.
  523. * @see #createNode(int, int, int, int, int, int, double, int, int, int, double, org.apache.fop.layoutmgr.BreakingAlgorithm.KnuthNode)
  524. * @see BreakingAlgorithm.BestRecords
  525. */
  526. protected KnuthNode createNode(int position, int line, int fitness,
  527. int totalWidth, int totalStretch, int totalShrink) {
  528. return new KnuthNode(position, line, fitness,
  529. totalWidth, totalStretch, totalShrink, best.getAdjust(fitness),
  530. best.getAvailableShrink(fitness), best.getAvailableStretch(fitness),
  531. best.getDifference(fitness), best.getDemerits(fitness),
  532. best.getNode(fitness));
  533. }
  534. /**
  535. * Return the last node that yielded a too short line.
  536. * @return the node corresponding to the last too short line
  537. */
  538. protected final KnuthNode getLastTooShort() {
  539. return this.lastTooShort;
  540. }
  541. /**
  542. * Generic handler for a {@link KnuthElement} at the given {@code position},
  543. * taking into account whether the preceding element was a box, and which
  544. * type(s) of breaks are allowed.
  545. * Non-overridable. This method simply serves to route the call to one of the
  546. * more specific handlers ({@link #handleBox(KnuthBox)},
  547. * {@link #handleGlueAt(KnuthGlue,int,boolean,int)} or
  548. * {@link #handlePenaltyAt(KnuthPenalty,int,int)}. The specialized handlers
  549. * can be overridden by subclasses to add to or modify the default behavior
  550. * for the different types of elements.
  551. *
  552. * @param position the position index of the element in the paragraph
  553. * @param previousIsBox {@code true} if the previous element is a box
  554. * @param allowedBreaks the type(s) of breaks allowed; should be one
  555. * of {@link #ALL_BREAKS}, {@link #NO_FLAGGED_PENALTIES}
  556. * or {@link #ONLY_FORCED_BREAKS}
  557. * @return the handled element
  558. */
  559. protected final KnuthElement handleElementAt(int position,
  560. boolean previousIsBox,
  561. int allowedBreaks) {
  562. KnuthElement element = getElement(position);
  563. if (element.isBox()) {
  564. handleBox((KnuthBox) element);
  565. } else if (element.isGlue()) {
  566. handleGlueAt((KnuthGlue) element, position, previousIsBox, allowedBreaks);
  567. } else if (element.isPenalty()){
  568. handlePenaltyAt((KnuthPenalty) element, position, allowedBreaks);
  569. } else {
  570. throw new IllegalArgumentException(
  571. "Unknown KnuthElement type: expecting KnuthBox, KnuthGlue or KnuthPenalty");
  572. }
  573. return element;
  574. }
  575. /**
  576. * Handle a {@link KnuthBox}.
  577. * <br/><em>Note: default implementation just adds the box's width
  578. * to the total content width. Subclasses that do not keep track
  579. * of this themselves, but override this method, should remember
  580. * to call {@code super.handleBox(box)} to avoid unwanted side-effects.</em>
  581. *
  582. * @param box the {@link KnuthBox} to handle
  583. */
  584. protected void handleBox(KnuthBox box) {
  585. // a KnuthBox object is not a legal line break,
  586. // just add the width to the total
  587. totalWidth += box.getW();
  588. }
  589. /**
  590. * Handle a {@link KnuthGlue} at the given position,
  591. * taking into account the additional parameters.
  592. *
  593. * @param glue the {@link KnuthGlue} to handle
  594. * @param position the position of the glue in the list
  595. * @param previousIsBox {@code true} if the preceding element is a box
  596. * @param allowedBreaks the type of breaks that are allowed
  597. */
  598. protected void handleGlueAt(KnuthGlue glue, int position,
  599. boolean previousIsBox, int allowedBreaks) {
  600. // a KnuthGlue object is a legal line break
  601. // only if the previous object is a KnuthBox
  602. // consider these glues according to the value of allowedBreaks
  603. if (previousIsBox
  604. && !(allowedBreaks == ONLY_FORCED_BREAKS)) {
  605. considerLegalBreak(glue, position);
  606. }
  607. totalWidth += glue.getW();
  608. totalStretch += glue.getY();
  609. totalShrink += glue.getZ();
  610. }
  611. /**
  612. * Handle a {@link KnuthPenalty} at the given position,
  613. * taking into account the type of breaks allowed.
  614. *
  615. * @param penalty the {@link KnuthPenalty} to handle
  616. * @param position the position of the penalty in the list
  617. * @param allowedBreaks the type of breaks that are allowed
  618. */
  619. protected void handlePenaltyAt(KnuthPenalty penalty, int position,
  620. int allowedBreaks) {
  621. // a KnuthPenalty is a legal line break
  622. // only if its penalty is not infinite;
  623. // consider all penalties, non-flagged penalties or non-forcing penalties
  624. // according to the value of allowedBreaks
  625. if (((penalty.getP() < KnuthElement.INFINITE)
  626. && (!(allowedBreaks == NO_FLAGGED_PENALTIES) || !penalty.isFlagged())
  627. && (!(allowedBreaks == ONLY_FORCED_BREAKS)
  628. || penalty.isForcedBreak()))) {
  629. considerLegalBreak(penalty, position);
  630. }
  631. }
  632. /**
  633. * Replace the last too-long or too-short node by the last deactivated
  634. * node, if applicable.
  635. */
  636. protected final void replaceLastDeactivated() {
  637. if (lastDeactivated.adjustRatio > 0) {
  638. //last deactivated was too short
  639. lastTooShort = lastDeactivated;
  640. } else {
  641. //last deactivated was too long or exactly the right width
  642. lastTooLong = lastDeactivated;
  643. }
  644. }
  645. /**
  646. * Recover from an overflow condition.
  647. *
  648. * @return the new {@code lastForced} node
  649. */
  650. protected KnuthNode recoverFromOverflow() {
  651. KnuthNode lastForced;
  652. if (isPartOverflowRecoveryActivated()) {
  653. if (lastRecovered == null) {
  654. lastRecovered = lastTooLong;
  655. if (log.isDebugEnabled()) {
  656. log.debug("Recovery point: " + lastRecovered);
  657. }
  658. }
  659. KnuthNode node = recoverFromTooLong(lastTooLong);
  660. lastForced = node;
  661. node.fitRecoveryCounter = lastTooLong.previous.fitRecoveryCounter + 1;
  662. if (log.isDebugEnabled()) {
  663. log.debug("first part doesn't fit into line, recovering: "
  664. + node.fitRecoveryCounter);
  665. }
  666. if (node.fitRecoveryCounter > getMaxRecoveryAttempts()) {
  667. while (lastForced.fitRecoveryCounter > 0
  668. && lastForced.previous != null) {
  669. lastForced = lastForced.previous;
  670. lastDeactivated = lastForced.previous;
  671. }
  672. lastForced = lastRecovered;
  673. lastRecovered = null;
  674. startLine = lastForced.line;
  675. endLine = lastForced.line;
  676. log.debug("rolled back...");
  677. }
  678. } else {
  679. lastForced = lastTooLong;
  680. }
  681. return lastForced;
  682. }
  683. /**
  684. * Restart from the given node at the given index.
  685. *
  686. * @param restartingNode the {@link KnuthNode} to restart from
  687. * @param currentIndex the current position index
  688. * @return the index of the restart point
  689. */
  690. protected int restartFrom(KnuthNode restartingNode, int currentIndex) {
  691. if (log.isDebugEnabled()) {
  692. log.debug("Restarting at node " + restartingNode);
  693. }
  694. restartingNode.totalDemerits = 0;
  695. addNode(restartingNode.line, restartingNode);
  696. startLine = restartingNode.line;
  697. endLine = startLine + 1;
  698. totalWidth = restartingNode.totalWidth;
  699. totalStretch = restartingNode.totalStretch;
  700. totalShrink = restartingNode.totalShrink;
  701. lastTooShort = null;
  702. lastTooLong = null;
  703. // the width, stretch and shrink already include the width,
  704. // stretch and shrink of the suppressed glues;
  705. // advance in the sequence in order to avoid taking into account
  706. // these elements twice
  707. int restartingIndex = restartingNode.position;
  708. while (restartingIndex + 1 < par.size()
  709. && !(getElement(restartingIndex + 1).isBox())) {
  710. restartingIndex++;
  711. }
  712. return restartingIndex;
  713. }
  714. /**
  715. * Determines if the given breakpoint is a feasible breakpoint. That is, if a decent
  716. * line may be built between one of the currently active nodes and this breakpoint.
  717. * @param element the paragraph's element to consider
  718. * @param elementIdx the element's index inside the paragraph
  719. */
  720. protected void considerLegalBreak(KnuthElement element, int elementIdx) {
  721. if (log.isTraceEnabled()) {
  722. log.trace("considerLegalBreak() at " + elementIdx
  723. + " (" + totalWidth + "+" + totalStretch + "-" + totalShrink
  724. + "), parts/lines: " + startLine + "-" + endLine);
  725. log.trace("\tCurrent active node list: " + activeNodeCount + " " + this.toString("\t"));
  726. }
  727. lastDeactivated = null;
  728. lastTooLong = null;
  729. for (int line = startLine; line < endLine; line++) {
  730. for (KnuthNode node = getNode(line); node != null; node = node.next) {
  731. if (node.position == elementIdx) {
  732. continue;
  733. }
  734. int difference = computeDifference(node, element, elementIdx);
  735. if (!elementCanEndLine(element, endLine, difference)) {
  736. log.trace("Skipping legal break");
  737. break;
  738. }
  739. double r = computeAdjustmentRatio(node, difference);
  740. int availableShrink = totalShrink - node.totalShrink;
  741. int availableStretch = totalStretch - node.totalStretch;
  742. if (log.isTraceEnabled()) {
  743. log.trace("\tr=" + r + " difference=" + difference);
  744. log.trace("\tline=" + line);
  745. }
  746. // The line would be too long.
  747. if (r < -1 || element.isForcedBreak()) {
  748. deactivateNode(node, line);
  749. }
  750. int fitnessClass = FitnessClasses.computeFitness(r);
  751. double demerits = computeDemerits(node, element, fitnessClass, r);
  752. // The line is within the available shrink and the threshold.
  753. if (r >= -1 && r <= threshold) {
  754. activateNode(node, difference, r,
  755. demerits, fitnessClass, availableShrink, availableStretch);
  756. }
  757. // The line is way too short/long, but we are in forcing mode, so a node is
  758. // calculated and stored in lastValidNode.
  759. if (force && (r <= -1 || r > threshold)) {
  760. forceNode(node, line, elementIdx, difference, r,
  761. demerits, fitnessClass, availableShrink, availableStretch);
  762. }
  763. }
  764. addBreaks(line, elementIdx);
  765. }
  766. }
  767. /**
  768. * Check if the given {@link KnuthElement} can end the line with the given
  769. * number.
  770. * @param element the element
  771. * @param line the line number
  772. * @param difference
  773. * @return {@code true} if the element can end the line
  774. */
  775. protected boolean elementCanEndLine(KnuthElement element, int line, int difference) {
  776. return (!element.isPenalty()
  777. || element.getP() < KnuthElement.INFINITE);
  778. }
  779. /**
  780. * Force the given {@link KnuthNode}, and register it.
  781. *
  782. * @param node the node
  783. * @param line the line number
  784. * @param elementIdx the position index of the element
  785. * @param difference the difference between content-length and avaialable width
  786. * @param r the adjustment ratio
  787. * @param demerits demerits produced by the node
  788. * @param fitnessClass the fitness class
  789. * @param availableShrink the available amount of shrink
  790. * @param availableStretch tha available amount of stretch
  791. */
  792. protected void forceNode(KnuthNode node,
  793. int line,
  794. int elementIdx,
  795. int difference,
  796. double r,
  797. double demerits,
  798. int fitnessClass,
  799. int availableShrink,
  800. int availableStretch) {
  801. int newWidth = totalWidth;
  802. int newStretch = totalStretch;
  803. int newShrink = totalShrink;
  804. // add the width, stretch and shrink of glue elements after
  805. // the break
  806. // this does not affect the dimension of the line / page, only
  807. // the values stored in the node; these would be as if the break
  808. // was just before the next box element, thus ignoring glues and
  809. // penalties between the "real" break and the following box
  810. for (int i = elementIdx; i < par.size(); i++) {
  811. KnuthElement tempElement = getElement(i);
  812. if (tempElement.isBox()) {
  813. break;
  814. } else if (tempElement.isGlue()) {
  815. newWidth += tempElement.getW();
  816. newStretch += tempElement.getY();
  817. newShrink += tempElement.getZ();
  818. } else if (tempElement.isForcedBreak() && i != elementIdx) {
  819. break;
  820. }
  821. }
  822. if (r <= -1) {
  823. log.debug("Considering tooLong, demerits=" + demerits);
  824. if (lastTooLong == null || demerits < lastTooLong.totalDemerits) {
  825. lastTooLong = createNode(elementIdx, line + 1, fitnessClass,
  826. newWidth, newStretch, newShrink,
  827. r, availableShrink, availableStretch,
  828. difference, demerits, node);
  829. if (log.isTraceEnabled()) {
  830. log.trace("Picking tooLong " + lastTooLong);
  831. }
  832. }
  833. } else {
  834. if (lastTooShort == null || demerits <= lastTooShort.totalDemerits) {
  835. if (considerTooShort) {
  836. //consider possibilities which are too short
  837. best.addRecord(demerits, node, r,
  838. availableShrink, availableStretch,
  839. difference, fitnessClass);
  840. }
  841. lastTooShort = createNode(elementIdx, line + 1, fitnessClass,
  842. newWidth, newStretch, newShrink,
  843. r, availableShrink, availableStretch,
  844. difference, demerits, node);
  845. if (log.isTraceEnabled()) {
  846. log.trace("Picking tooShort " + lastTooShort);
  847. }
  848. }
  849. }
  850. }
  851. /**
  852. * Activate the given node. Will result in the given {@link KnuthNode}
  853. * being registered as a feasible breakpoint, if the {@code demerits} are better
  854. * than that of the best node registered for the given {@code fitnessClass}.
  855. *
  856. * @param node the node
  857. * @param difference the difference between content-length and available width
  858. * @param r the adjustment ratio
  859. * @param demerits demerits produced by the node
  860. * @param fitnessClass the fitness class
  861. * @param availableShrink the available amount of shrink
  862. * @param availableStretch the available amount of stretch
  863. */
  864. protected void activateNode(KnuthNode node,
  865. int difference,
  866. double r,
  867. double demerits,
  868. int fitnessClass,
  869. int availableShrink,
  870. int availableStretch) {
  871. if (log.isTraceEnabled()) {
  872. log.trace("\tDemerits=" + demerits);
  873. log.trace("\tFitness class=" + FitnessClasses.NAMES[fitnessClass]);
  874. }
  875. if (demerits < best.getDemerits(fitnessClass)) {
  876. // updates best demerits data
  877. best.addRecord(demerits, node, r, availableShrink, availableStretch,
  878. difference, fitnessClass);
  879. lastTooShort = null;
  880. }
  881. }
  882. /**
  883. * Deactivate the given node
  884. *
  885. * @param node the node
  886. * @param line the line number
  887. */
  888. protected void deactivateNode(KnuthNode node, int line) {
  889. // Deactivate node...
  890. if (log.isTraceEnabled()) {
  891. log.trace("Removing " + node);
  892. }
  893. removeNode(line, node);
  894. // ... and remember it, if it was a good candidate
  895. lastDeactivated = compareNodes(lastDeactivated, node);
  896. }
  897. /**
  898. * Adds new active nodes for breaks at the given element.
  899. * @param line number of the previous line; this element will end line number (line+1)
  900. * @param elementIdx the element's index
  901. */
  902. private void addBreaks(int line, int elementIdx) {
  903. if (!best.hasRecords()) {
  904. return;
  905. }
  906. int newWidth = totalWidth;
  907. int newStretch = totalStretch;
  908. int newShrink = totalShrink;
  909. // add the width, stretch and shrink of glue elements after
  910. // the break
  911. // this does not affect the dimension of the line / page, only
  912. // the values stored in the node; these would be as if the break
  913. // was just before the next box element, thus ignoring glues and
  914. // penalties between the "real" break and the following box
  915. for (int i = elementIdx; i < par.size(); i++) {
  916. KnuthElement tempElement = getElement(i);
  917. if (tempElement.isBox()) {
  918. break;
  919. } else if (tempElement.isGlue()) {
  920. newWidth += tempElement.getW();
  921. newStretch += tempElement.getY();
  922. newShrink += tempElement.getZ();
  923. } else if (tempElement.isForcedBreak() && i != elementIdx) {
  924. break;
  925. }
  926. }
  927. // add nodes to the active nodes list
  928. double minimumDemerits = best.getMinDemerits() + incompatibleFitnessDemerit;
  929. for (int i = 0; i <= 3; i++) {
  930. if (best.notInfiniteDemerits(i) && best.getDemerits(i) <= minimumDemerits) {
  931. // the nodes in activeList must be ordered
  932. // by line number and position;
  933. if (log.isTraceEnabled()) {
  934. log.trace("\tInsert new break in list of " + activeNodeCount
  935. + " from fitness class " + FitnessClasses.NAMES[i]);
  936. }
  937. KnuthNode newNode = createNode(elementIdx, line + 1, i,
  938. newWidth, newStretch, newShrink);
  939. addNode(line + 1, newNode);
  940. }
  941. }
  942. best.reset();
  943. }
  944. /**
  945. * Return the difference between the natural width of a line that would be made
  946. * between the given active node and the given element, and the available width of the
  947. * real line.
  948. * @param activeNode node for the previous breakpoint
  949. * @param element currently considered breakpoint
  950. * @param elementIndex index of the element that is considered as a breakpoint
  951. * @return The difference in width. Positive numbers mean extra space in the line,
  952. * negative number that the line overflows.
  953. */
  954. protected int computeDifference(KnuthNode activeNode, KnuthElement element,
  955. int elementIndex) {
  956. // compute the adjustment ratio
  957. int actualWidth = totalWidth - activeNode.totalWidth;
  958. if (element.isPenalty()) {
  959. actualWidth += element.getW();
  960. }
  961. return getLineWidth() - actualWidth;
  962. }
  963. /**
  964. * Return the adjustment ratio needed to make up for the difference. A ratio of
  965. * <ul>
  966. * <li>0 means that the break has the exact right width</li>
  967. * <li>&gt;= -1 &amp;&amp; &lt; 0 means that the break is wider than the line,
  968. * but within the minimim values of the glues.</li>
  969. * <li>&gt;0 &amp;&amp; &lt; 1 means that the break is smaller than the line width,
  970. * but within the maximum values of the glues.</li>
  971. * <li>&gt; 1 means that the break is too small to make up for the glues.</li>
  972. * </ul>
  973. * @param activeNode the currently active node
  974. * @param difference the difference between content-length and available width
  975. * @return The adjustment ratio.
  976. */
  977. protected double computeAdjustmentRatio(KnuthNode activeNode, int difference) {
  978. // compute the adjustment ratio
  979. if (difference > 0) {
  980. int maxAdjustment = totalStretch - activeNode.totalStretch;
  981. if (maxAdjustment > 0) {
  982. return (double) difference / maxAdjustment;
  983. } else {
  984. return INFINITE_RATIO;
  985. }
  986. } else if (difference < 0) {
  987. int maxAdjustment = totalShrink - activeNode.totalShrink;
  988. if (maxAdjustment > 0) {
  989. return (double) difference / maxAdjustment;
  990. } else {
  991. return -INFINITE_RATIO;
  992. }
  993. } else {
  994. return 0;
  995. }
  996. }
  997. /**
  998. * Computes the demerits of the current breaking (that is, up to the given element),
  999. * if the next-to-last chosen breakpoint is the given active node. This adds to the
  1000. * total demerits of the given active node, the demerits of a line starting at this
  1001. * node and ending at the given element.
  1002. * @param activeNode considered preceding line break
  1003. * @param element considered current line break
  1004. * @param fitnessClass fitness of the current line
  1005. * @param r adjustment ratio for the current line
  1006. * @return the demerit of the current line
  1007. */
  1008. protected double computeDemerits(KnuthNode activeNode, KnuthElement element,
  1009. int fitnessClass, double r) {
  1010. double demerits = 0;
  1011. // compute demerits
  1012. double f = Math.abs(r);
  1013. f = 1 + 100 * f * f * f;
  1014. if (element.isPenalty()) {
  1015. double penalty = element.getP();
  1016. if (penalty >= 0) {
  1017. f += penalty;
  1018. demerits = f * f;
  1019. } else if (!element.isForcedBreak()) {
  1020. demerits = f * f - penalty * penalty;
  1021. } else {
  1022. demerits = f * f;
  1023. }
  1024. } else {
  1025. demerits = f * f;
  1026. }
  1027. if (element.isPenalty() && ((KnuthPenalty) element).isFlagged()
  1028. && getElement(activeNode.position).isPenalty()
  1029. && ((KnuthPenalty) getElement(activeNode.position)).isFlagged()) {
  1030. // add demerit for consecutive breaks at flagged penalties
  1031. demerits += repeatedFlaggedDemerit;
  1032. // there are at least two consecutive lines ending with a flagged penalty;
  1033. // check if the previous line end with a flagged penalty too,
  1034. // and if this situation is allowed
  1035. int flaggedPenaltiesCount = 2;
  1036. for (KnuthNode prevNode = activeNode.previous;
  1037. prevNode != null && flaggedPenaltiesCount <= maxFlaggedPenaltiesCount;
  1038. prevNode = prevNode.previous) {
  1039. KnuthElement prevElement = getElement(prevNode.position);
  1040. if (prevElement.isPenalty()
  1041. && ((KnuthPenalty) prevElement).isFlagged()) {
  1042. // the previous line ends with a flagged penalty too
  1043. flaggedPenaltiesCount++;
  1044. } else {
  1045. // the previous line does not end with a flagged penalty,
  1046. // exit the loop
  1047. break;
  1048. }
  1049. }
  1050. if (maxFlaggedPenaltiesCount >= 1
  1051. && flaggedPenaltiesCount > maxFlaggedPenaltiesCount) {
  1052. // add infinite demerits, so this break will not be chosen
  1053. // unless there isn't any alternative break
  1054. demerits += BestRecords.INFINITE_DEMERITS;
  1055. }
  1056. }
  1057. if (Math.abs(fitnessClass - activeNode.fitness) > 1) {
  1058. // add demerit for consecutive breaks
  1059. // with very different fitness classes
  1060. demerits += incompatibleFitnessDemerit;
  1061. }
  1062. demerits += activeNode.totalDemerits;
  1063. return demerits;
  1064. }
  1065. /**
  1066. * Hook for subclasses to trigger special behavior after ending the
  1067. * main loop in {@link #findBreakingPoints(KnuthSequence,int,double,boolean,int)}
  1068. */
  1069. protected void finish() {
  1070. if (log.isTraceEnabled()) {
  1071. log.trace("Main loop completed " + activeNodeCount);
  1072. log.trace("Active nodes=" + toString(""));
  1073. }
  1074. }
  1075. /**
  1076. * Return the element at index idx in the paragraph.
  1077. * @param idx index of the element.
  1078. * @return the element at index idx in the paragraph.
  1079. */
  1080. protected KnuthElement getElement(int idx) {
  1081. return (KnuthElement) par.get(idx);
  1082. }
  1083. /**
  1084. * Compare two KnuthNodes and return the node with the least demerit.
  1085. * @param node1 The first knuth node.
  1086. * @param node2 The other knuth node.
  1087. * @return the node with the least demerit.
  1088. */
  1089. protected KnuthNode compareNodes(KnuthNode node1, KnuthNode node2) {
  1090. if (node1 == null || node2.position > node1.position) {
  1091. return node2;
  1092. }
  1093. if (node2.position == node1.position) {
  1094. if (node2.totalDemerits < node1.totalDemerits) {
  1095. return node2;
  1096. }
  1097. }
  1098. return node1;
  1099. }
  1100. /**
  1101. * Add a node at the end of the given line's existing active nodes.
  1102. * If this is the first node in the line, adjust endLine accordingly.
  1103. * @param line number of the line ending at the node's corresponding breakpoint
  1104. * @param node the active node to add
  1105. */
  1106. protected void addNode(int line, KnuthNode node) {
  1107. int headIdx = line * 2;
  1108. if (headIdx >= activeLines.length) {
  1109. KnuthNode[] oldList = activeLines;
  1110. activeLines = new KnuthNode[headIdx + headIdx];
  1111. System.arraycopy(oldList, 0, activeLines, 0, oldList.length);
  1112. }
  1113. node.next = null;
  1114. if (activeLines[headIdx + 1] != null) {
  1115. activeLines[headIdx + 1].next = node;
  1116. } else {
  1117. activeLines[headIdx] = node;
  1118. endLine = line + 1;
  1119. }
  1120. activeLines[headIdx + 1] = node;
  1121. activeNodeCount++;
  1122. }
  1123. /**
  1124. * Remove the given active node registered for the given line. If there are no more active nodes
  1125. * for this line, adjust the startLine accordingly.
  1126. * @param line number of the line ending at the node's corresponding breakpoint
  1127. * @param node the node to deactivate
  1128. */
  1129. protected void removeNode(int line, KnuthNode node) {
  1130. int headIdx = line * 2;
  1131. KnuthNode n = getNode(line);
  1132. if (n != node) {
  1133. // nodes could be rightly deactivated in a different order
  1134. KnuthNode prevNode = null;
  1135. while (n != node) {
  1136. prevNode = n;
  1137. n = n.next;
  1138. }
  1139. prevNode.next = n.next;
  1140. if (prevNode.next == null) {
  1141. activeLines[headIdx + 1] = prevNode;
  1142. }
  1143. } else {
  1144. activeLines[headIdx] = node.next;
  1145. if (node.next == null) {
  1146. activeLines[headIdx + 1] = null;
  1147. }
  1148. while (startLine < endLine && getNode(startLine) == null) {
  1149. startLine++;
  1150. }
  1151. }
  1152. activeNodeCount--;
  1153. }
  1154. /**
  1155. * Returns the first active node for the given line.
  1156. * @param line the line/part number
  1157. * @return the requested active node
  1158. */
  1159. protected KnuthNode getNode(int line) {
  1160. return activeLines[line * 2];
  1161. }
  1162. /**
  1163. * Returns the line/part width of a given line/part.
  1164. * @param line the line/part number
  1165. * @return the width/length in millipoints
  1166. */
  1167. protected int getLineWidth(int line) {
  1168. assert lineWidth >= 0;
  1169. return this.lineWidth;
  1170. }
  1171. /** @return the constant line/part width or -1 if there is no such value */
  1172. protected int getLineWidth() {
  1173. return this.lineWidth;
  1174. }
  1175. /**
  1176. * Creates a string representation of the active nodes. Used for debugging.
  1177. * @param prepend a string to prepend on each entry
  1178. * @return the requested string
  1179. */
  1180. public String toString(String prepend) {
  1181. StringBuffer sb = new StringBuffer();
  1182. sb.append("[\n");
  1183. for (int i = startLine; i < endLine; i++) {
  1184. for (KnuthNode node = getNode(i); node != null; node = node.next) {
  1185. sb.append(prepend).append('\t').append(node).append(",\n");
  1186. }
  1187. }
  1188. sb.append(prepend).append("]");
  1189. return sb.toString();
  1190. }
  1191. protected abstract int filterActiveNodes();
  1192. /**
  1193. * Determines the set of optimal breakpoints corresponding to the given active node.
  1194. * @param node the active node
  1195. * @param par the corresponding paragraph
  1196. * @param total the number of lines into which the paragraph will be broken
  1197. */
  1198. protected void calculateBreakPoints(KnuthNode node, KnuthSequence par,
  1199. int total) {
  1200. KnuthNode bestActiveNode = node;
  1201. // use bestActiveNode to determine the optimum breakpoints
  1202. for (int i = node.line; i > 0; i--) {
  1203. updateData2(bestActiveNode, par, total);
  1204. bestActiveNode = bestActiveNode.previous;
  1205. }
  1206. }
  1207. /** @return the alignment for normal lines/parts */
  1208. public int getAlignment() {
  1209. return this.alignment;
  1210. }
  1211. /** @return the alignment for the last line/part */
  1212. public int getAlignmentLast() {
  1213. return this.alignmentLast;
  1214. }
  1215. }