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.

LineLayoutManager.java 72KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  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.inline;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import java.util.ListIterator;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.fop.area.Area;
  27. import org.apache.fop.area.LineArea;
  28. import org.apache.fop.area.Trait;
  29. import org.apache.fop.area.inline.InlineArea;
  30. import org.apache.fop.complexscripts.bidi.BidiResolver;
  31. import org.apache.fop.datatypes.Length;
  32. import org.apache.fop.datatypes.Numeric;
  33. import org.apache.fop.fo.Constants;
  34. import org.apache.fop.fo.flow.Block;
  35. import org.apache.fop.fo.properties.CommonHyphenation;
  36. import org.apache.fop.fo.properties.KeepProperty;
  37. import org.apache.fop.fonts.Font;
  38. import org.apache.fop.fonts.FontInfo;
  39. import org.apache.fop.fonts.FontTriplet;
  40. import org.apache.fop.hyphenation.Hyphenation;
  41. import org.apache.fop.hyphenation.Hyphenator;
  42. import org.apache.fop.layoutmgr.Adjustment;
  43. import org.apache.fop.layoutmgr.BlockLayoutManager;
  44. import org.apache.fop.layoutmgr.BlockLevelLayoutManager;
  45. import org.apache.fop.layoutmgr.BreakElement;
  46. import org.apache.fop.layoutmgr.BreakingAlgorithm;
  47. import org.apache.fop.layoutmgr.ElementListObserver;
  48. import org.apache.fop.layoutmgr.InlineKnuthSequence;
  49. import org.apache.fop.layoutmgr.Keep;
  50. import org.apache.fop.layoutmgr.KnuthBlockBox;
  51. import org.apache.fop.layoutmgr.KnuthBox;
  52. import org.apache.fop.layoutmgr.KnuthElement;
  53. import org.apache.fop.layoutmgr.KnuthGlue;
  54. import org.apache.fop.layoutmgr.KnuthPenalty;
  55. import org.apache.fop.layoutmgr.KnuthPossPosIter;
  56. import org.apache.fop.layoutmgr.KnuthSequence;
  57. import org.apache.fop.layoutmgr.LayoutContext;
  58. import org.apache.fop.layoutmgr.LayoutManager;
  59. import org.apache.fop.layoutmgr.LeafPosition;
  60. import org.apache.fop.layoutmgr.ListElement;
  61. import org.apache.fop.layoutmgr.NonLeafPosition;
  62. import org.apache.fop.layoutmgr.Position;
  63. import org.apache.fop.layoutmgr.PositionIterator;
  64. import org.apache.fop.layoutmgr.SpaceSpecifier;
  65. import org.apache.fop.traits.MinOptMax;
  66. /**
  67. * LayoutManager for lines. It builds one or more lines containing
  68. * inline areas generated by its sub layout managers.
  69. * A break is found for each line which may contain one of more
  70. * breaks from the child layout managers.
  71. * Once a break is found then it is return for the parent layout
  72. * manager to handle.
  73. * When the areas are being added to the page this manager
  74. * creates a line area to contain the inline areas added by the
  75. * child layout managers.
  76. */
  77. public class LineLayoutManager extends InlineStackingLayoutManager
  78. implements BlockLevelLayoutManager {
  79. /**
  80. * this constant is used to create elements when text-align is center:
  81. * every TextLM descendant of LineLM must use the same value,
  82. * otherwise the line breaking algorithm does not find the right
  83. * break point
  84. */
  85. public static final int DEFAULT_SPACE_WIDTH = 3336;
  86. /**
  87. * logging instance
  88. */
  89. private static Log log = LogFactory.getLog(LineLayoutManager.class);
  90. private final Block fobj;
  91. private boolean isFirstInBlock;
  92. /**
  93. * Private class to store information about inline breaks.
  94. * Each value holds the start and end indexes into a List of
  95. * inline break positions.
  96. */
  97. private static class LineBreakPosition extends LeafPosition {
  98. private final int parIndex; // index of the Paragraph this Position refers to
  99. private final int startIndex; //index of the first element this Position refers to
  100. private final int availableShrink;
  101. private final int availableStretch;
  102. private final int difference;
  103. private final double dAdjust; // Percentage to adjust (stretch or shrink)
  104. private final double ipdAdjust; // Percentage to adjust (stretch or shrink)
  105. private final int startIndent;
  106. private final int endIndent;
  107. private final int lineHeight;
  108. private final int lineWidth;
  109. private final int spaceBefore;
  110. private final int spaceAfter;
  111. private final int baseline;
  112. LineBreakPosition( // CSOK: ParameterNumber
  113. LayoutManager lm, int index, int startIndex, int breakIndex,
  114. int shrink, int stretch, int diff, double ipdA, double adjust, int si,
  115. int ei, int lh, int lw, int sb, int sa, int bl) {
  116. super(lm, breakIndex);
  117. availableShrink = shrink;
  118. availableStretch = stretch;
  119. difference = diff;
  120. parIndex = index;
  121. this.startIndex = startIndex;
  122. ipdAdjust = ipdA;
  123. dAdjust = adjust;
  124. startIndent = si;
  125. endIndent = ei;
  126. lineHeight = lh;
  127. lineWidth = lw;
  128. spaceBefore = sb;
  129. spaceAfter = sa;
  130. baseline = bl;
  131. }
  132. }
  133. private int bidiLevel = -1;
  134. private int textAlignment = EN_JUSTIFY;
  135. private int textAlignmentLast;
  136. private int effectiveAlignment;
  137. private Length textIndent;
  138. private Length lastLineEndIndent;
  139. private CommonHyphenation hyphenationProperties;
  140. private Numeric hyphenationLadderCount;
  141. private int wrapOption = EN_WRAP;
  142. private int whiteSpaceTreament;
  143. //private LayoutProps layoutProps;
  144. private final Length lineHeight;
  145. private final int lead;
  146. private final int follow;
  147. private AlignmentContext alignmentContext;
  148. private List<KnuthSequence> knuthParagraphs;
  149. private LineLayoutPossibilities lineLayouts;
  150. private LineLayoutPossibilities[] lineLayoutsList;
  151. private int ipd = 0;
  152. /**
  153. * When layout must be re-started due to a change of IPD, there is no need
  154. * to perform hyphenation on the remaining Knuth sequence once again.
  155. */
  156. private boolean hyphenationPerformed;
  157. /**
  158. * This class is used to remember
  159. * which was the first element in the paragraph
  160. * returned by each LM.
  161. */
  162. private final class Update {
  163. private final InlineLevelLayoutManager inlineLM;
  164. private final int firstIndex;
  165. private Update(InlineLevelLayoutManager lm, int index) {
  166. inlineLM = lm;
  167. firstIndex = index;
  168. }
  169. }
  170. // this class represents a paragraph
  171. private class Paragraph extends InlineKnuthSequence {
  172. private static final long serialVersionUID = 5862072380375189105L;
  173. /** Number of elements to ignore at the beginning of the list. */
  174. private int ignoreAtStart = 0;
  175. /** Number of elements to ignore at the end of the list. */
  176. private int ignoreAtEnd = 0;
  177. // space at the end of the last line (in millipoints)
  178. private MinOptMax lineFiller;
  179. private final int textAlignment;
  180. private final int textAlignmentLast;
  181. private final int textIndent;
  182. private final int lastLineEndIndent;
  183. // the LM which created the paragraph
  184. private final LineLayoutManager layoutManager;
  185. Paragraph(LineLayoutManager llm, int alignment, int alignmentLast,
  186. int indent, int endIndent) {
  187. super();
  188. layoutManager = llm;
  189. textAlignment = alignment;
  190. textAlignmentLast = alignmentLast;
  191. textIndent = indent;
  192. lastLineEndIndent = endIndent;
  193. }
  194. @Override
  195. public void startSequence() {
  196. // set the minimum amount of empty space at the end of the
  197. // last line
  198. if (textAlignment == EN_CENTER) {
  199. lineFiller = MinOptMax.getInstance(lastLineEndIndent);
  200. } else {
  201. lineFiller = MinOptMax.getInstance(lastLineEndIndent, lastLineEndIndent,
  202. layoutManager.ipd);
  203. }
  204. // add auxiliary elements at the beginning of the paragraph
  205. if (textAlignment == EN_CENTER && textAlignmentLast != EN_JUSTIFY) {
  206. this.add(new KnuthGlue(0, 3 * DEFAULT_SPACE_WIDTH, 0,
  207. null, false));
  208. ignoreAtStart++;
  209. }
  210. // add the element representing text indentation
  211. // at the beginning of the first paragraph
  212. if (isFirstInBlock && knuthParagraphs.size() == 0
  213. && textIndent != 0) {
  214. this.add(new KnuthInlineBox(textIndent, null,
  215. null, false));
  216. ignoreAtStart++;
  217. }
  218. }
  219. public void endParagraph() {
  220. KnuthSequence finishedPar = this.endSequence();
  221. if (finishedPar != null) {
  222. knuthParagraphs.add(finishedPar);
  223. }
  224. }
  225. @Override
  226. public KnuthSequence endSequence() {
  227. if (this.size() > ignoreAtStart) {
  228. if (textAlignment == EN_CENTER
  229. && textAlignmentLast != EN_JUSTIFY) {
  230. this.add(new KnuthGlue(0, 3 * DEFAULT_SPACE_WIDTH, 0,
  231. null, false));
  232. this.add(new KnuthPenalty(lineFiller.getOpt(), -KnuthElement.INFINITE,
  233. false, null, false));
  234. ignoreAtEnd = 2;
  235. } else if (textAlignmentLast != EN_JUSTIFY) {
  236. // add the elements representing the space
  237. // at the end of the last line
  238. // and the forced break
  239. this.add(new KnuthPenalty(0, KnuthElement.INFINITE,
  240. false, null, false));
  241. this.add(new KnuthGlue(0,
  242. lineFiller.getStretch(),
  243. lineFiller.getShrink(), null, false));
  244. this.add(new KnuthPenalty(lineFiller.getOpt(), -KnuthElement.INFINITE,
  245. false, null, false));
  246. ignoreAtEnd = 3;
  247. } else {
  248. // add only the element representing the forced break
  249. this.add(new KnuthPenalty(lineFiller.getOpt(), -KnuthElement.INFINITE,
  250. false, null, false));
  251. ignoreAtEnd = 1;
  252. }
  253. return this;
  254. } else {
  255. this.clear();
  256. return null;
  257. }
  258. }
  259. /**
  260. * @return true if the sequence contains a box
  261. */
  262. public boolean containsBox() {
  263. for (int i = 0; i < this.size(); i++) {
  264. KnuthElement el = (KnuthElement)this.get(i);
  265. if (el.isBox()) {
  266. return true;
  267. }
  268. }
  269. return false;
  270. }
  271. }
  272. private class LineBreakingAlgorithm extends BreakingAlgorithm {
  273. private final LineLayoutManager thisLLM;
  274. private final int pageAlignment;
  275. private int activePossibility;
  276. private int addedPositions;
  277. private final int textIndent;
  278. private final int lineHeight;
  279. private final int lead;
  280. private final int follow;
  281. private static final double MAX_DEMERITS = 10e6;
  282. public LineBreakingAlgorithm( // CSOK: ParameterNumber
  283. int pageAlign, int textAlign, int textAlignLast, int indent, int fillerWidth,
  284. int lh, int ld, int fl, boolean first, int maxFlagCount, LineLayoutManager llm) {
  285. super(textAlign, textAlignLast, first, false, maxFlagCount);
  286. pageAlignment = pageAlign;
  287. textIndent = indent;
  288. lineHeight = lh;
  289. lead = ld;
  290. follow = fl;
  291. thisLLM = llm;
  292. activePossibility = -1;
  293. }
  294. @Override
  295. public void updateData1(int lineCount, double demerits) {
  296. lineLayouts.addPossibility(lineCount, demerits);
  297. if (log.isTraceEnabled()) {
  298. log.trace("Layout possibility in " + lineCount + " lines; break at position:");
  299. }
  300. }
  301. @Override
  302. public void updateData2(KnuthNode bestActiveNode,
  303. KnuthSequence par,
  304. int total) {
  305. // compute indent and adjustment ratio, according to
  306. // the value of text-align and text-align-last
  307. int startIndent;
  308. int endIndent;
  309. int difference = bestActiveNode.difference;
  310. int textAlign = (bestActiveNode.line < total) ? alignment : alignmentLast;
  311. switch ( textAlign ) {
  312. case Constants.EN_START:
  313. startIndent = 0;
  314. endIndent = difference > 0 ? difference : 0;
  315. break;
  316. case Constants.EN_END:
  317. startIndent = difference > 0 ? difference : 0;
  318. endIndent = 0;
  319. break;
  320. case Constants.EN_CENTER:
  321. startIndent = difference / 2;
  322. endIndent = startIndent;
  323. break;
  324. default:
  325. case Constants.EN_JUSTIFY:
  326. startIndent = 0;
  327. endIndent = 0;
  328. break;
  329. }
  330. /*
  331. startIndent += (textAlign == Constants.EN_CENTER)
  332. ? difference / 2 : (textAlign == Constants.EN_END) ? difference : 0;
  333. */
  334. startIndent += (bestActiveNode.line == 1 && indentFirstPart && isFirstInBlock)
  335. ? textIndent : 0;
  336. double ratio = (textAlign == Constants.EN_JUSTIFY
  337. || difference < 0 && -difference <= bestActiveNode.availableShrink)
  338. ? bestActiveNode.adjustRatio : 0;
  339. // add nodes at the beginning of the list, as they are found
  340. // backwards, from the last one to the first one
  341. // the first time this method is called, initialize activePossibility
  342. if (activePossibility == -1) {
  343. activePossibility = 0;
  344. addedPositions = 0;
  345. }
  346. if (addedPositions == lineLayouts.getLineCount(activePossibility)) {
  347. activePossibility++;
  348. addedPositions = 0;
  349. }
  350. if (log.isWarnEnabled()) {
  351. int lack = difference + bestActiveNode.availableShrink;
  352. // if this LLM is nested inside a BlockContainerLayoutManager that is constraining
  353. // the available width and thus responsible for the overflow then we do not issue
  354. // warning event here and instead let the BCLM handle that at a later stage
  355. if (lack < 0 && !handleOverflow(-lack)) {
  356. InlineLevelEventProducer eventProducer
  357. = InlineLevelEventProducer.Provider.get(
  358. getFObj().getUserAgent().getEventBroadcaster());
  359. eventProducer.lineOverflows(this, bestActiveNode.line,
  360. -lack, getFObj().getLocator());
  361. }
  362. }
  363. //log.debug("LLM> (" + (lineLayouts.getLineNumber(activePossibility) - addedPositions)
  364. // + ") difference = " + difference + " ratio = " + ratio);
  365. lineLayouts.addBreakPosition(makeLineBreakPosition(par,
  366. (bestActiveNode.line > 1 ? bestActiveNode.previous.position + 1 : 0),
  367. bestActiveNode.position,
  368. bestActiveNode.availableShrink - (addedPositions > 0
  369. ? 0 : ((Paragraph) par).lineFiller.getShrink()),
  370. bestActiveNode.availableStretch,
  371. difference, ratio, startIndent, endIndent), activePossibility);
  372. addedPositions++;
  373. }
  374. /* reset activePossibility, as if breakpoints have not yet been computed
  375. */
  376. public void resetAlgorithm() {
  377. activePossibility = -1;
  378. }
  379. private LineBreakPosition makeLineBreakPosition( // CSOK: ParameterNumber
  380. KnuthSequence par, int firstElementIndex, int lastElementIndex, int availableShrink,
  381. int availableStretch, int difference, double ratio, int startIndent,
  382. int endIndent) {
  383. // line height calculation - spaceBefore may differ from spaceAfter
  384. // by 1mpt due to rounding
  385. int spaceBefore = (lineHeight - lead - follow) / 2;
  386. int spaceAfter = lineHeight - lead - follow - spaceBefore;
  387. // height before the main baseline
  388. int lineLead = lead;
  389. // maximum follow
  390. int lineFollow = follow;
  391. // true if this line contains only zero-height, auxiliary boxes
  392. // and the actual line width is 0; in this case, the line "collapses"
  393. // i.e. the line area will have bpd = 0
  394. boolean isZeroHeightLine = (difference == ipd);
  395. // if line-stacking-strategy is "font-height", the line height
  396. // is not affected by its content
  397. if (fobj.getLineStackingStrategy() != EN_FONT_HEIGHT) {
  398. ListIterator inlineIterator
  399. = par.listIterator(firstElementIndex);
  400. AlignmentContext lastAC = null;
  401. int maxIgnoredHeight = 0; // See spec 7.13
  402. for (int j = firstElementIndex;
  403. j <= lastElementIndex;
  404. j++) {
  405. KnuthElement element = (KnuthElement) inlineIterator.next();
  406. if (element instanceof KnuthInlineBox ) {
  407. AlignmentContext ac = ((KnuthInlineBox) element).getAlignmentContext();
  408. if (ac != null && lastAC != ac) {
  409. if (!ac.usesInitialBaselineTable()
  410. || ac.getAlignmentBaselineIdentifier() != EN_BEFORE_EDGE
  411. && ac.getAlignmentBaselineIdentifier() != EN_AFTER_EDGE) {
  412. if (fobj.getLineHeightShiftAdjustment() == EN_CONSIDER_SHIFTS
  413. || ac.getBaselineShiftValue() == 0) {
  414. int alignmentOffset = ac.getTotalAlignmentBaselineOffset();
  415. if (alignmentOffset + ac.getAltitude() > lineLead) {
  416. lineLead = alignmentOffset + ac.getAltitude();
  417. }
  418. if (ac.getDepth() - alignmentOffset > lineFollow) {
  419. lineFollow = ac.getDepth() - alignmentOffset;
  420. }
  421. }
  422. } else {
  423. if (ac.getHeight() > maxIgnoredHeight) {
  424. maxIgnoredHeight = ac.getHeight();
  425. }
  426. }
  427. lastAC = ac;
  428. }
  429. if (isZeroHeightLine
  430. && (!element.isAuxiliary() || ac != null && ac.getHeight() > 0)) {
  431. isZeroHeightLine = false;
  432. }
  433. }
  434. }
  435. if (lineFollow < maxIgnoredHeight - lineLead) {
  436. lineFollow = maxIgnoredHeight - lineLead;
  437. }
  438. }
  439. constantLineHeight = lineLead + lineFollow;
  440. if (isZeroHeightLine) {
  441. return new LineBreakPosition(thisLLM,
  442. knuthParagraphs.indexOf(par),
  443. firstElementIndex, lastElementIndex,
  444. availableShrink, availableStretch,
  445. difference, ratio, 0, startIndent, endIndent,
  446. 0, ipd, 0, 0, 0);
  447. } else {
  448. return new LineBreakPosition(thisLLM,
  449. knuthParagraphs.indexOf(par),
  450. firstElementIndex, lastElementIndex,
  451. availableShrink, availableStretch,
  452. difference, ratio, 0, startIndent, endIndent,
  453. lineLead + lineFollow,
  454. ipd, spaceBefore, spaceAfter,
  455. lineLead);
  456. }
  457. }
  458. @Override
  459. protected int filterActiveNodes() {
  460. KnuthNode bestActiveNode = null;
  461. if (pageAlignment == EN_JUSTIFY) {
  462. // leave all active nodes and find the optimum line number
  463. //log.debug("LBA.filterActiveNodes> " + activeNodeCount + " layouts");
  464. for (int i = startLine; i < endLine; i++) {
  465. for (KnuthNode node = getNode(i); node != null; node = node.next) {
  466. //log.debug(" + lines = "
  467. //+ node.line + " demerits = " + node.totalDemerits);
  468. bestActiveNode = compareNodes(bestActiveNode, node);
  469. }
  470. }
  471. // scan the node set once again and remove some nodes
  472. //log.debug("LBA.filterActiveList> layout selection");
  473. for (int i = startLine; i < endLine; i++) {
  474. for (KnuthNode node = getNode(i); node != null; node = node.next) {
  475. //if (Math.abs(node.line - bestActiveNode.line) > maxDiff) {
  476. //if (false) {
  477. if (node.line != bestActiveNode.line
  478. && node.totalDemerits > MAX_DEMERITS) {
  479. //log.debug(" XXX lines = "
  480. //+ node.line + " demerits = " + node.totalDemerits);
  481. removeNode(i, node);
  482. } else {
  483. //log.debug(" ok lines = "
  484. //+ node.line + " demerits = " + node.totalDemerits);
  485. }
  486. }
  487. }
  488. } else {
  489. // leave only the active node with fewest total demerits
  490. for (int i = startLine; i < endLine; i++) {
  491. for (KnuthNode node = getNode(i); node != null; node = node.next) {
  492. bestActiveNode = compareNodes(bestActiveNode, node);
  493. if (node != bestActiveNode) {
  494. removeNode(i, node);
  495. }
  496. }
  497. }
  498. }
  499. return bestActiveNode.line;
  500. }
  501. }
  502. private int constantLineHeight = 12000;
  503. /**
  504. * Create a new Line Layout Manager.
  505. * This is used by the block layout manager to create
  506. * line managers for handling inline areas flowing into line areas.
  507. * @param block the block formatting object
  508. * @param lh the default line height
  509. * @param l the default lead, from top to baseline
  510. * @param f the default follow, from baseline to bottom
  511. */
  512. public LineLayoutManager(Block block, Length lh, int l, int f) {
  513. super(block);
  514. fobj = block;
  515. // the child FObj are owned by the parent BlockLM
  516. // this LM has all its childLMs preloaded
  517. fobjIter = null;
  518. lineHeight = lh;
  519. lead = l;
  520. follow = f;
  521. }
  522. /** {@inheritDoc} */
  523. @Override
  524. public void initialize() {
  525. bidiLevel = fobj.getBidiLevel();
  526. textAlignment = fobj.getTextAlign();
  527. textAlignmentLast = fobj.getTextAlignLast();
  528. textIndent = fobj.getTextIndent();
  529. lastLineEndIndent = fobj.getLastLineEndIndent();
  530. hyphenationProperties = fobj.getCommonHyphenation();
  531. hyphenationLadderCount = fobj.getHyphenationLadderCount();
  532. wrapOption = fobj.getWrapOption();
  533. whiteSpaceTreament = fobj.getWhitespaceTreatment();
  534. //
  535. effectiveAlignment = getEffectiveAlignment(textAlignment, textAlignmentLast);
  536. isFirstInBlock = (this == getParent().getChildLMs().get(0));
  537. }
  538. private int getEffectiveAlignment(int alignment, int alignmentLast) {
  539. if (textAlignment != EN_JUSTIFY && textAlignmentLast == EN_JUSTIFY) {
  540. return 0;
  541. } else {
  542. return textAlignment;
  543. }
  544. }
  545. /** {@inheritDoc} */
  546. @Override
  547. public List getNextKnuthElements(LayoutContext context, int alignment) {
  548. if (alignmentContext == null) {
  549. FontInfo fi = fobj.getFOEventHandler().getFontInfo();
  550. FontTriplet[] fontkeys = fobj.getCommonFont().getFontState(fi);
  551. Font fs = fi.getFontInstance(fontkeys[0], fobj.getCommonFont().fontSize.getValue(this));
  552. alignmentContext = new AlignmentContext(fs, lineHeight.getValue(this),
  553. context.getWritingMode());
  554. }
  555. context.setAlignmentContext(alignmentContext);
  556. ipd = context.getRefIPD();
  557. //PHASE 1: Create Knuth elements
  558. if (knuthParagraphs == null) {
  559. // it's the first time this method is called
  560. knuthParagraphs = new ArrayList<KnuthSequence>();
  561. // here starts Knuth's algorithm
  562. collectInlineKnuthElements(context);
  563. } else {
  564. // this method has been called before
  565. // all line breaks are already calculated
  566. }
  567. // return finished when there's no content
  568. if (knuthParagraphs.size() == 0) {
  569. setFinished(true);
  570. return null;
  571. }
  572. //PHASE 2: Create line breaks
  573. return createLineBreaks(context.getBPAlignment(), context);
  574. }
  575. /**
  576. * Get a sequence of KnuthElements representing the content
  577. * of the node assigned to the LM.
  578. * @param context the LayoutContext used to store layout information
  579. * @param alignment the desired text alignment
  580. * @param restartPosition position at restart
  581. * @return the list of KnuthElements
  582. * @see LayoutManager#getNextKnuthElements(LayoutContext,int)
  583. */
  584. public List getNextKnuthElements(LayoutContext context, int alignment,
  585. LeafPosition restartPosition) {
  586. log.trace("Restarting line breaking from index " + restartPosition.getIndex());
  587. int parIndex = restartPosition.getLeafPos();
  588. KnuthSequence paragraph = knuthParagraphs.get(parIndex);
  589. if (paragraph instanceof Paragraph) {
  590. ((Paragraph) paragraph).ignoreAtStart = 0;
  591. isFirstInBlock = false;
  592. }
  593. paragraph.subList(0, restartPosition.getIndex() + 1).clear();
  594. Iterator<KnuthElement> iter = paragraph.iterator();
  595. while (iter.hasNext() && !iter.next().isBox()) {
  596. iter.remove();
  597. }
  598. if (!iter.hasNext()) {
  599. knuthParagraphs.remove(parIndex);
  600. }
  601. // return finished when there's no content
  602. if (knuthParagraphs.size() == 0) {
  603. setFinished(true);
  604. return null;
  605. }
  606. ipd = context.getRefIPD();
  607. //PHASE 2: Create line breaks
  608. return createLineBreaks(context.getBPAlignment(), context);
  609. }
  610. /**
  611. * Phase 1 of Knuth algorithm: Collect all inline Knuth elements before determining line breaks.
  612. * @param context the LayoutContext
  613. */
  614. private void collectInlineKnuthElements(LayoutContext context) {
  615. LayoutContext inlineLC = LayoutContext.copyOf(context);
  616. // convert all the text in a sequence of paragraphs made
  617. // of KnuthBox, KnuthGlue and KnuthPenalty objects
  618. boolean previousIsBox = false;
  619. StringBuffer trace = new StringBuffer("LineLM:");
  620. Paragraph lastPar = null;
  621. InlineLevelLayoutManager curLM;
  622. while ((curLM = (InlineLevelLayoutManager) getChildLM()) != null) {
  623. List inlineElements = curLM.getNextKnuthElements(inlineLC, effectiveAlignment);
  624. if (inlineElements == null || inlineElements.size() == 0) {
  625. /* curLM.getNextKnuthElements() returned null or an empty list;
  626. * this can happen if there is nothing more to layout,
  627. * so just iterate once more to see if there are other children */
  628. continue;
  629. }
  630. if (lastPar != null) {
  631. KnuthSequence firstSeq = (KnuthSequence) inlineElements.get(0);
  632. // finish last paragraph before a new block sequence
  633. if (!firstSeq.isInlineSequence()) {
  634. lastPar.endParagraph();
  635. ElementListObserver.observe(lastPar, "line", null);
  636. lastPar = null;
  637. if (log.isTraceEnabled()) {
  638. trace.append(" ]");
  639. }
  640. previousIsBox = false;
  641. }
  642. // does the first element of the first paragraph add to an existing word?
  643. if (lastPar != null) {
  644. KnuthElement thisElement;
  645. thisElement = (KnuthElement) firstSeq.get(0);
  646. if (thisElement.isBox() && !thisElement.isAuxiliary()
  647. && previousIsBox) {
  648. lastPar.addALetterSpace();
  649. }
  650. }
  651. }
  652. // loop over the KnuthSequences (and single KnuthElements) in returnedList
  653. ListIterator iter = inlineElements.listIterator();
  654. while (iter.hasNext()) {
  655. KnuthSequence sequence = (KnuthSequence) iter.next();
  656. // the sequence contains inline Knuth elements
  657. if (sequence.isInlineSequence()) {
  658. // look at the last element
  659. ListElement lastElement = sequence.getLast();
  660. assert lastElement != null;
  661. previousIsBox = lastElement.isBox()
  662. && !((KnuthElement) lastElement).isAuxiliary()
  663. && ((KnuthElement) lastElement).getWidth() != 0;
  664. // if last paragraph is open, add the new elements to the paragraph
  665. // else this is the last paragraph
  666. if (lastPar == null) {
  667. lastPar = new Paragraph(this,
  668. textAlignment, textAlignmentLast,
  669. textIndent.getValue(this),
  670. lastLineEndIndent.getValue(this));
  671. lastPar.startSequence();
  672. if (log.isTraceEnabled()) {
  673. trace.append(" [");
  674. }
  675. } else {
  676. if (log.isTraceEnabled()) {
  677. trace.append(" +");
  678. }
  679. }
  680. lastPar.addAll(sequence);
  681. if (log.isTraceEnabled()) {
  682. trace.append(" I");
  683. }
  684. // finish last paragraph if it was closed with a linefeed
  685. if (lastElement.isPenalty()
  686. && ((KnuthPenalty) lastElement).getPenalty()
  687. == -KnuthPenalty.INFINITE) {
  688. // a penalty item whose value is -inf
  689. // represents a preserved linefeed,
  690. // which forces a line break
  691. lastPar.removeLast();
  692. if (!lastPar.containsBox()) {
  693. //only a forced linefeed on this line
  694. //-> compensate with an auxiliary glue
  695. lastPar.add(new KnuthGlue(ipd, 0, ipd, null, true));
  696. }
  697. lastPar.endParagraph();
  698. ElementListObserver.observe(lastPar, "line", null);
  699. lastPar = null;
  700. if (log.isTraceEnabled()) {
  701. trace.append(" ]");
  702. }
  703. previousIsBox = false;
  704. }
  705. } else { // the sequence is a block sequence
  706. // the positions will be wrapped with this LM in postProcessLineBreaks
  707. knuthParagraphs.add(sequence);
  708. if (log.isTraceEnabled()) {
  709. trace.append(" B");
  710. }
  711. }
  712. } // end of loop over returnedList
  713. }
  714. if (lastPar != null) {
  715. lastPar.endParagraph();
  716. ElementListObserver.observe(lastPar, "line", fobj.getId());
  717. if (log.isTraceEnabled()) {
  718. trace.append(" ]");
  719. }
  720. }
  721. log.trace(trace);
  722. }
  723. /**
  724. * Phase 2 of Knuth algorithm: find optimal break points.
  725. * @param alignment alignment in BP direction of the paragraph
  726. * @param context the layout context
  727. * @return a list of Knuth elements representing broken lines
  728. */
  729. private List<ListElement> createLineBreaks(int alignment, LayoutContext context) {
  730. // find the optimal line breaking points for each paragraph
  731. Iterator<KnuthSequence> paragraphsIterator = knuthParagraphs.iterator();
  732. lineLayoutsList = new LineLayoutPossibilities[knuthParagraphs.size()];
  733. LineLayoutPossibilities llPoss;
  734. for (int i = 0; paragraphsIterator.hasNext(); i++) {
  735. KnuthSequence seq = paragraphsIterator.next();
  736. if (!seq.isInlineSequence()) {
  737. // This set of line layout possibilities does not matter;
  738. // we only need an entry in lineLayoutsList.
  739. llPoss = new LineLayoutPossibilities();
  740. } else {
  741. llPoss = findOptimalBreakingPoints(alignment, (Paragraph) seq,
  742. !paragraphsIterator.hasNext());
  743. }
  744. lineLayoutsList[i] = llPoss;
  745. }
  746. setFinished(true);
  747. //Post-process the line breaks found
  748. return postProcessLineBreaks(alignment, context);
  749. }
  750. /**
  751. * Find the optimal linebreaks for a paragraph
  752. * @param alignment alignment of the paragraph
  753. * @param currPar the Paragraph for which the linebreaks are found
  754. * @param isLastPar flag indicating whether currPar is the last paragraph
  755. * @return the line layout possibilities for the paragraph
  756. */
  757. private LineLayoutPossibilities findOptimalBreakingPoints(int alignment, Paragraph currPar,
  758. boolean isLastPar) {
  759. // use the member lineLayouts, which is read by LineBreakingAlgorithm.updateData1 and 2
  760. lineLayouts = new LineLayoutPossibilities();
  761. double maxAdjustment = 1;
  762. LineBreakingAlgorithm alg = new LineBreakingAlgorithm(alignment,
  763. textAlignment, textAlignmentLast,
  764. textIndent.getValue(this), currPar.lineFiller.getOpt(),
  765. lineHeight.getValue(this), lead, follow,
  766. (knuthParagraphs.indexOf(currPar) == 0),
  767. hyphenationLadderCount.getEnum() == EN_NO_LIMIT
  768. ? 0 : hyphenationLadderCount.getValue(),
  769. this);
  770. alg.setConstantLineWidth(ipd);
  771. boolean canWrap = (wrapOption != EN_NO_WRAP);
  772. boolean canHyphenate = (canWrap && hyphenationProperties.hyphenate.getEnum() == EN_TRUE);
  773. // find hyphenation points, if allowed and not yet done
  774. if (canHyphenate && !hyphenationPerformed) {
  775. // make sure findHyphenationPoints() is bypassed if
  776. // the method is called twice (e.g. due to changing page-ipd)
  777. hyphenationPerformed = isLastPar;
  778. findHyphenationPoints(currPar);
  779. }
  780. // first try: do not consider hyphenation points as legal breaks
  781. int allowedBreaks = (canWrap ? BreakingAlgorithm.NO_FLAGGED_PENALTIES
  782. : BreakingAlgorithm.ONLY_FORCED_BREAKS);
  783. int breakingPoints = alg.findBreakingPoints(currPar, maxAdjustment, false, allowedBreaks);
  784. if (breakingPoints == 0 || alignment == EN_JUSTIFY) {
  785. // if the first try found a set of breaking points, save them
  786. if (breakingPoints > 0) {
  787. alg.resetAlgorithm();
  788. lineLayouts.savePossibilities(false);
  789. } else {
  790. // the first try failed
  791. log.debug("No set of breaking points found with maxAdjustment = " + maxAdjustment);
  792. }
  793. // now try something different
  794. log.debug("Hyphenation possible? " + canHyphenate);
  795. // Note: if allowedBreaks is guaranteed to be unchanged by alg.findBreakingPoints(),
  796. // the below check can be simplified to 'if (canHyphenate) ...'
  797. if (canHyphenate && allowedBreaks != BreakingAlgorithm.ONLY_FORCED_BREAKS) {
  798. // consider every hyphenation point as a legal break
  799. allowedBreaks = BreakingAlgorithm.ALL_BREAKS;
  800. } else {
  801. // try with a higher threshold
  802. maxAdjustment = 5;
  803. }
  804. breakingPoints = alg.findBreakingPoints(currPar, maxAdjustment, false, allowedBreaks);
  805. if (breakingPoints == 0) {
  806. // the second try failed too, try with a huge threshold
  807. // and force the algorithm to find a set of breaking points
  808. if (log.isDebugEnabled()) {
  809. log.debug("No set of breaking points found with maxAdjustment = "
  810. + maxAdjustment + (canHyphenate ? " and hyphenation" : ""));
  811. }
  812. maxAdjustment = 20;
  813. alg.findBreakingPoints(currPar, maxAdjustment, true, allowedBreaks);
  814. }
  815. // use non-hyphenated breaks, when possible
  816. lineLayouts.restorePossibilities();
  817. }
  818. return lineLayouts;
  819. }
  820. /**
  821. * Creates the element list in BP direction for the broken lines.
  822. * @param alignment the currently applicable vertical alignment
  823. * @param context the layout context
  824. * @return the newly built element list
  825. */
  826. private List<ListElement> postProcessLineBreaks(int alignment, LayoutContext context) {
  827. List<ListElement> returnList = new LinkedList<ListElement>();
  828. int endIndex = -1;
  829. for (int p = 0; p < knuthParagraphs.size(); p++) {
  830. // penalty between paragraphs
  831. if (p > 0) {
  832. Keep keep = getKeepTogether();
  833. returnList.add(new BreakElement(
  834. new Position(this),
  835. keep.getPenalty(),
  836. keep.getContext(),
  837. context));
  838. }
  839. LineLayoutPossibilities llPoss = lineLayoutsList[p];
  840. KnuthSequence seq = knuthParagraphs.get(p);
  841. if (!seq.isInlineSequence()) {
  842. List<ListElement> targetList = new LinkedList<ListElement>();
  843. ListIterator listIter = seq.listIterator();
  844. while (listIter.hasNext()) {
  845. ListElement tempElement;
  846. tempElement = (ListElement) listIter.next();
  847. if (tempElement.getLayoutManager() != this) {
  848. tempElement.setPosition(notifyPos(new NonLeafPosition(this,
  849. tempElement.getPosition())));
  850. }
  851. targetList.add(tempElement);
  852. }
  853. returnList.addAll(targetList);
  854. } else if (seq.isInlineSequence() && alignment == EN_JUSTIFY) {
  855. /* justified vertical alignment (not in the XSL FO recommendation):
  856. create a multi-layout sequence whose elements will contain
  857. a conventional Position */
  858. Position returnPosition = new LeafPosition(this, p);
  859. createElements(returnList, llPoss, returnPosition);
  860. } else {
  861. /* "normal" vertical alignment: create a sequence whose boxes
  862. represent effective lines, and contain LineBreakPositions */
  863. int startIndex = 0;
  864. for (int i = 0;
  865. i < llPoss.getChosenLineCount();
  866. i++) {
  867. if (returnList.size() > 0
  868. && i > 0 //if i==0 break generated above already
  869. && i >= fobj.getOrphans()
  870. && i <= llPoss.getChosenLineCount() - fobj.getWidows()) {
  871. // penalty allowing a page break between lines
  872. Keep keep = getKeepTogether();
  873. returnList.add(new BreakElement(
  874. new LeafPosition(this, p, endIndex),
  875. keep.getPenalty(),
  876. keep.getContext(),
  877. context));
  878. }
  879. endIndex = ((LineBreakPosition) llPoss.getChosenPosition(i)).getLeafPos();
  880. // create a list of the FootnoteBodyLM handling footnotes
  881. // whose citations are in this line
  882. List<LayoutManager> footnoteList = new LinkedList<LayoutManager>();
  883. ListIterator<KnuthElement> elementIterator = seq.listIterator(startIndex);
  884. while (elementIterator.nextIndex() <= endIndex) {
  885. KnuthElement element = elementIterator.next();
  886. if (element instanceof KnuthInlineBox
  887. && ((KnuthInlineBox) element).isAnchor()) {
  888. footnoteList.add(((KnuthInlineBox) element).getFootnoteBodyLM());
  889. } else if (element instanceof KnuthBlockBox) {
  890. footnoteList.addAll(((KnuthBlockBox) element).getFootnoteBodyLMs());
  891. }
  892. }
  893. startIndex = endIndex + 1;
  894. LineBreakPosition lbp = (LineBreakPosition) llPoss.getChosenPosition(i);
  895. returnList.add(new KnuthBlockBox
  896. (lbp.lineHeight + lbp.spaceBefore + lbp.spaceAfter,
  897. footnoteList, lbp, false));
  898. }
  899. }
  900. }
  901. return returnList;
  902. }
  903. private void createElements(List<ListElement> list, LineLayoutPossibilities llPoss,
  904. Position elementPosition) {
  905. /* number of normal, inner lines */
  906. int innerLines = 0;
  907. /* number of lines that can be used in order to fill more space */
  908. int optionalLines = 0;
  909. /* number of lines that can be used in order to fill more space
  910. only if the paragraph is not parted */
  911. int conditionalOptionalLines = 0;
  912. /* number of lines that can be omitted in order to fill less space */
  913. int eliminableLines = 0;
  914. /* number of lines that can be omitted in order to fill less space
  915. only if the paragraph is not parted */
  916. int conditionalEliminableLines = 0;
  917. /* number of the first unbreakable lines */
  918. int firstLines = fobj.getOrphans();
  919. /* number of the last unbreakable lines */
  920. int lastLines = fobj.getWidows();
  921. /* sub-sequence used to separate the elements representing different lines */
  922. List<KnuthElement> breaker = new LinkedList<KnuthElement>();
  923. /* comment out the next lines in order to test particular situations */
  924. if (fobj.getOrphans() + fobj.getWidows() <= llPoss.getMinLineCount()) {
  925. innerLines = llPoss.getMinLineCount() - (fobj.getOrphans() + fobj.getWidows());
  926. optionalLines = llPoss.getMaxLineCount() - llPoss.getOptLineCount();
  927. eliminableLines = llPoss.getOptLineCount() - llPoss.getMinLineCount();
  928. } else if (fobj.getOrphans() + fobj.getWidows() <= llPoss.getOptLineCount()) {
  929. optionalLines = llPoss.getMaxLineCount() - llPoss.getOptLineCount();
  930. eliminableLines = llPoss.getOptLineCount() - (fobj.getOrphans() + fobj.getWidows());
  931. conditionalEliminableLines
  932. = (fobj.getOrphans() + fobj.getWidows()) - llPoss.getMinLineCount();
  933. } else if (fobj.getOrphans() + fobj.getWidows() <= llPoss.getMaxLineCount()) {
  934. optionalLines = llPoss.getMaxLineCount() - (fobj.getOrphans() + fobj.getWidows());
  935. conditionalOptionalLines
  936. = (fobj.getOrphans() + fobj.getWidows()) - llPoss.getOptLineCount();
  937. conditionalEliminableLines = llPoss.getOptLineCount() - llPoss.getMinLineCount();
  938. firstLines -= conditionalOptionalLines;
  939. } else {
  940. conditionalOptionalLines = llPoss.getMaxLineCount() - llPoss.getOptLineCount();
  941. conditionalEliminableLines = llPoss.getOptLineCount() - llPoss.getMinLineCount();
  942. firstLines = llPoss.getOptLineCount();
  943. lastLines = 0;
  944. }
  945. /* comment out the previous lines in order to test particular situations */
  946. /* use these lines to test particular situations
  947. innerLines = 0;
  948. optionalLines = 1;
  949. conditionalOptionalLines = 2;
  950. eliminableLines = 0;
  951. conditionalEliminableLines = 0;
  952. firstLines = 1;
  953. lastLines = 3;
  954. */
  955. if (lastLines != 0
  956. && (conditionalOptionalLines > 0 || conditionalEliminableLines > 0)) {
  957. breaker.add(new KnuthPenalty(0, KnuthElement.INFINITE, false, elementPosition, false));
  958. breaker.add(new KnuthGlue(0, -conditionalOptionalLines * constantLineHeight,
  959. -conditionalEliminableLines * constantLineHeight,
  960. Adjustment.LINE_NUMBER_ADJUSTMENT, elementPosition, false));
  961. breaker.add(new KnuthPenalty(conditionalOptionalLines * constantLineHeight,
  962. 0, false, elementPosition, false));
  963. breaker.add(new KnuthGlue(0, conditionalOptionalLines * constantLineHeight,
  964. conditionalEliminableLines * constantLineHeight,
  965. Adjustment.LINE_NUMBER_ADJUSTMENT, elementPosition, false));
  966. } else if (lastLines != 0) {
  967. breaker.add(new KnuthPenalty(0, 0, false, elementPosition, false));
  968. }
  969. // creation of the elements:
  970. // first group of lines
  971. list.add(new KnuthBox(firstLines * constantLineHeight, elementPosition,
  972. (lastLines == 0
  973. && conditionalOptionalLines == 0
  974. && conditionalEliminableLines == 0)));
  975. if (conditionalOptionalLines > 0
  976. || conditionalEliminableLines > 0) {
  977. list.add(new KnuthPenalty(0, KnuthElement.INFINITE, false, elementPosition, false));
  978. list.add(new KnuthGlue(0, conditionalOptionalLines * constantLineHeight,
  979. conditionalEliminableLines * constantLineHeight,
  980. Adjustment.LINE_NUMBER_ADJUSTMENT, elementPosition, false));
  981. list.add(new KnuthBox(0, elementPosition, (lastLines == 0)));
  982. }
  983. // optional lines
  984. for (int i = 0; i < optionalLines; i++) {
  985. list.addAll(breaker);
  986. list.add(new KnuthBox(0, elementPosition, false));
  987. list.add(new KnuthPenalty(0, KnuthElement.INFINITE, false, elementPosition, false));
  988. list.add(new KnuthGlue(0, constantLineHeight, 0,
  989. Adjustment.LINE_NUMBER_ADJUSTMENT, elementPosition, false));
  990. list.add(new KnuthBox(0, elementPosition, false));
  991. }
  992. // eliminable lines
  993. for (int i = 0; i < eliminableLines; i++) {
  994. list.addAll(breaker);
  995. list.add(new KnuthBox(constantLineHeight, elementPosition, false));
  996. list.add(new KnuthPenalty(0, KnuthElement.INFINITE, false, elementPosition, false));
  997. list.add(new KnuthGlue(0, 0, constantLineHeight,
  998. Adjustment.LINE_NUMBER_ADJUSTMENT, elementPosition, false));
  999. list.add(new KnuthBox(0, elementPosition, false));
  1000. }
  1001. // inner lines
  1002. for (int i = 0; i < innerLines; i++) {
  1003. list.addAll(breaker);
  1004. list.add(new KnuthBox(constantLineHeight, elementPosition, false));
  1005. }
  1006. // last group of lines
  1007. if (lastLines > 0) {
  1008. list.addAll(breaker);
  1009. list.add(new KnuthBox(lastLines * constantLineHeight,
  1010. elementPosition, true));
  1011. }
  1012. }
  1013. /** {@inheritDoc} */
  1014. public boolean mustKeepTogether() {
  1015. return ((BlockLevelLayoutManager) getParent()).mustKeepTogether();
  1016. }
  1017. /** {@inheritDoc} */
  1018. public KeepProperty getKeepTogetherProperty() {
  1019. return ((BlockLevelLayoutManager) getParent()).getKeepTogetherProperty();
  1020. }
  1021. /** {@inheritDoc} */
  1022. public KeepProperty getKeepWithPreviousProperty() {
  1023. return ((BlockLevelLayoutManager) getParent()).getKeepWithPreviousProperty();
  1024. }
  1025. /** {@inheritDoc} */
  1026. public KeepProperty getKeepWithNextProperty() {
  1027. return ((BlockLevelLayoutManager) getParent()).getKeepWithNextProperty();
  1028. }
  1029. /** {@inheritDoc} */
  1030. public Keep getKeepTogether() {
  1031. return ((BlockLevelLayoutManager) getParent()).getKeepTogether();
  1032. }
  1033. /** {@inheritDoc} */
  1034. public boolean mustKeepWithPrevious() {
  1035. return !getKeepWithPrevious().isAuto();
  1036. }
  1037. /** {@inheritDoc} */
  1038. public boolean mustKeepWithNext() {
  1039. return !getKeepWithNext().isAuto();
  1040. }
  1041. /** {@inheritDoc} */
  1042. public Keep getKeepWithNext() {
  1043. return Keep.KEEP_AUTO;
  1044. }
  1045. /** {@inheritDoc} */
  1046. public Keep getKeepWithPrevious() {
  1047. return Keep.KEEP_AUTO;
  1048. }
  1049. /** {@inheritDoc} */
  1050. public int negotiateBPDAdjustment(int adj, KnuthElement lastElement) {
  1051. LeafPosition pos = (LeafPosition)lastElement.getPosition();
  1052. //if (lastElement.isPenalty()) {
  1053. // totalAdj += lastElement.getWidth();
  1054. //}
  1055. //int lineNumberDifference = (int)((double) totalAdj / constantLineHeight);
  1056. int lineNumberDifference = (int) Math.round((double) adj / constantLineHeight
  1057. + (adj > 0 ? -0.4 : 0.4));
  1058. //log.debug(" LLM> variazione calcolata = " + ((double) totalAdj / constantLineHeight)
  1059. //+ " variazione applicata = " + lineNumberDifference);
  1060. LineLayoutPossibilities llPoss;
  1061. llPoss = lineLayoutsList[pos.getLeafPos()];
  1062. lineNumberDifference = llPoss.applyLineCountAdjustment(lineNumberDifference);
  1063. return lineNumberDifference * constantLineHeight;
  1064. }
  1065. /** {@inheritDoc} */
  1066. public void discardSpace(KnuthGlue spaceGlue) {
  1067. }
  1068. /** {@inheritDoc} */
  1069. @Override
  1070. public List getChangedKnuthElements(List oldList, int alignment, int depth) {
  1071. return getChangedKnuthElements(oldList, alignment);
  1072. }
  1073. /** {@inheritDoc} */
  1074. @Override
  1075. public List getChangedKnuthElements(List oldList, int alignment) {
  1076. List<KnuthElement> returnList = new LinkedList<KnuthElement>();
  1077. for (int p = 0; p < knuthParagraphs.size(); p++) {
  1078. LineLayoutPossibilities llPoss = lineLayoutsList[p];
  1079. //log.debug("demerits of the chosen layout: " + llPoss.getChosenDemerits());
  1080. for (int i = 0; i < llPoss.getChosenLineCount(); i++) {
  1081. if (!((BlockLevelLayoutManager) parentLayoutManager).mustKeepTogether()
  1082. && i >= fobj.getOrphans()
  1083. && i <= llPoss.getChosenLineCount() - fobj.getWidows()) {
  1084. // null penalty allowing a page break between lines
  1085. returnList.add(new KnuthPenalty(0, 0, false, new Position(this), false));
  1086. }
  1087. LineBreakPosition lbp = (LineBreakPosition) llPoss.getChosenPosition(i);
  1088. //log.debug("LLM.getChangedKnuthElements> lineWidth= "
  1089. // + lbp.lineWidth + " difference= " + lbp.difference);
  1090. //log.debug(" shrink= "
  1091. // + lbp.availableShrink + " stretch= " + lbp.availableStretch);
  1092. //log.debug("linewidth= " + lbp.lineWidth + " difference= "
  1093. //+ lbp.difference + " indent= " + lbp.startIndent);
  1094. MinOptMax contentIPD;
  1095. if (alignment == EN_JUSTIFY) {
  1096. contentIPD = MinOptMax.getInstance(
  1097. lbp.lineWidth - lbp.difference - lbp.availableShrink,
  1098. lbp.lineWidth - lbp.difference,
  1099. lbp.lineWidth - lbp.difference + lbp.availableStretch);
  1100. } else if (alignment == EN_CENTER) {
  1101. contentIPD = MinOptMax.getInstance(lbp.lineWidth - 2 * lbp.startIndent);
  1102. } else if (alignment == EN_END) {
  1103. contentIPD = MinOptMax.getInstance(lbp.lineWidth - lbp.startIndent);
  1104. } else {
  1105. contentIPD
  1106. = MinOptMax.getInstance(lbp.lineWidth - lbp.difference + lbp.startIndent);
  1107. }
  1108. returnList.add(new KnuthBlockBox(lbp.lineHeight, contentIPD, (lbp.ipdAdjust != 0
  1109. ? lbp.lineWidth - lbp.difference : 0),
  1110. lbp, false));
  1111. }
  1112. }
  1113. return returnList;
  1114. }
  1115. /**
  1116. * Find hyphenation points for every word in the current paragraph.
  1117. *
  1118. * @param currPar the paragraph whose words will be hyphenated
  1119. */
  1120. private void findHyphenationPoints(Paragraph currPar) {
  1121. // hyphenate every word
  1122. ListIterator currParIterator = currPar.listIterator(currPar.ignoreAtStart);
  1123. // list of TLM involved in hyphenation
  1124. List updateList = new LinkedList();
  1125. KnuthElement firstElement;
  1126. KnuthElement nextElement;
  1127. // current InlineLevelLayoutManager
  1128. InlineLevelLayoutManager currLM = null;
  1129. // number of KnuthBox elements containing word fragments
  1130. int boxCount;
  1131. // number of auxiliary KnuthElements between KnuthBoxes
  1132. int auxCount;
  1133. StringBuffer sbChars;
  1134. // find all hyphenation points
  1135. while (currParIterator.hasNext()) {
  1136. firstElement = (KnuthElement) currParIterator.next();
  1137. //
  1138. if (firstElement.getLayoutManager() != currLM) {
  1139. currLM = (InlineLevelLayoutManager) firstElement.getLayoutManager();
  1140. if (currLM != null) {
  1141. updateList.add(new Update(currLM, currParIterator.previousIndex()));
  1142. } else {
  1143. break;
  1144. }
  1145. } else if (currLM == null) {
  1146. break;
  1147. }
  1148. // collect word fragments, ignoring auxiliary elements;
  1149. // each word fragment was created by a different TextLM
  1150. if (firstElement.isBox() && !firstElement.isAuxiliary()) {
  1151. boxCount = 1;
  1152. auxCount = 0;
  1153. sbChars = new StringBuffer();
  1154. sbChars.append(currLM.getWordChars(firstElement.getPosition()));
  1155. // look if next elements are boxes too
  1156. while (currParIterator.hasNext()) {
  1157. nextElement = (KnuthElement) currParIterator.next();
  1158. if (nextElement.isBox() && !nextElement.isAuxiliary()) {
  1159. // a non-auxiliary KnuthBox: append word chars
  1160. if (currLM != nextElement.getLayoutManager()) {
  1161. currLM = (InlineLevelLayoutManager) nextElement.getLayoutManager();
  1162. updateList.add(new Update(currLM, currParIterator.previousIndex()));
  1163. }
  1164. // append text to recreate the whole word
  1165. boxCount++;
  1166. sbChars.append(currLM.getWordChars(nextElement.getPosition()));
  1167. } else if (!nextElement.isAuxiliary()) {
  1168. // a non-auxiliary non-box KnuthElement: stop
  1169. // go back to the last box or auxiliary element
  1170. currParIterator.previous();
  1171. break;
  1172. } else {
  1173. if (currLM != nextElement.getLayoutManager()) {
  1174. currLM = (InlineLevelLayoutManager) nextElement.getLayoutManager();
  1175. updateList.add(new Update(currLM, currParIterator.previousIndex()));
  1176. }
  1177. // an auxiliary KnuthElement: simply ignore it
  1178. auxCount++;
  1179. }
  1180. }
  1181. if (log.isTraceEnabled()) {
  1182. log.trace(" Word to hyphenate: " + sbChars.toString());
  1183. }
  1184. // find hyphenation points
  1185. HyphContext hc = getHyphenContext(sbChars);
  1186. // ask each LM to hyphenate its word fragment
  1187. if (hc != null) {
  1188. KnuthElement element = null;
  1189. for (int i = 0; i < (boxCount + auxCount); i++) {
  1190. currParIterator.previous();
  1191. }
  1192. for (int i = 0; i < (boxCount + auxCount); i++) {
  1193. element = (KnuthElement) currParIterator.next();
  1194. if (element.isBox() && !element.isAuxiliary()) {
  1195. ((InlineLevelLayoutManager)
  1196. element.getLayoutManager()).hyphenate(element.getPosition(), hc);
  1197. } else {
  1198. // nothing to do, element is an auxiliary KnuthElement
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. processUpdates(currPar, updateList);
  1205. }
  1206. private void processUpdates(Paragraph par, List updateList) {
  1207. // create iterator for the updateList
  1208. ListIterator updateListIterator = updateList.listIterator();
  1209. Update currUpdate;
  1210. int elementsAdded = 0;
  1211. while (updateListIterator.hasNext()) {
  1212. // ask the LMs to apply the changes and return
  1213. // the new KnuthElements to replace the old ones
  1214. currUpdate = (Update) updateListIterator.next();
  1215. int fromIndex = currUpdate.firstIndex;
  1216. int toIndex;
  1217. if (updateListIterator.hasNext()) {
  1218. Update nextUpdate = (Update) updateListIterator.next();
  1219. toIndex = nextUpdate.firstIndex;
  1220. updateListIterator.previous();
  1221. } else {
  1222. // maybe this is not always correct!
  1223. toIndex = par.size() - par.ignoreAtEnd
  1224. - elementsAdded;
  1225. }
  1226. // applyChanges() returns true if the LM modifies its data,
  1227. // so it must return new KnuthElements to replace the old ones
  1228. if (currUpdate.inlineLM
  1229. .applyChanges(par.subList(fromIndex + elementsAdded,
  1230. toIndex + elementsAdded))) {
  1231. // insert the new KnuthElements
  1232. List newElements = currUpdate.inlineLM.getChangedKnuthElements
  1233. (par.subList(fromIndex + elementsAdded,
  1234. toIndex + elementsAdded),
  1235. /*flaggedPenalty,*/ effectiveAlignment);
  1236. // remove the old elements
  1237. par.subList(fromIndex + elementsAdded,
  1238. toIndex + elementsAdded).clear();
  1239. // insert the new elements
  1240. par.addAll(fromIndex + elementsAdded, newElements);
  1241. elementsAdded += newElements.size() - (toIndex - fromIndex);
  1242. }
  1243. }
  1244. updateList.clear();
  1245. }
  1246. /**
  1247. * Line area is always considered to act as a fence.
  1248. * @param isNotFirst ignored
  1249. * @return always true
  1250. */
  1251. @Override
  1252. protected boolean hasLeadingFence(boolean isNotFirst) {
  1253. return true;
  1254. }
  1255. /**
  1256. * Line area is always considered to act as a fence.
  1257. * @param isNotLast ignored
  1258. * @return always true
  1259. */
  1260. @Override
  1261. protected boolean hasTrailingFence(boolean isNotLast) {
  1262. return true;
  1263. }
  1264. private HyphContext getHyphenContext(StringBuffer sbChars) {
  1265. // Find all hyphenation points in this word
  1266. // (get in an array of offsets)
  1267. // hyphenationProperties are from the block level?.
  1268. // Note that according to the spec,
  1269. // they also "apply to" fo:character.
  1270. // I don't know what that means, since
  1271. // if we change language in the middle of a "word",
  1272. // the effect would seem quite strange!
  1273. // Or perhaps in that case, we say that it's several words.
  1274. // We probably should bring the hyphenation props up from the actual
  1275. // TextLM which generate the hyphenation buffer,
  1276. // since these properties inherit and could be specified
  1277. // on an inline or wrapper below the block level.
  1278. Hyphenation hyph = Hyphenator.hyphenate(hyphenationProperties.language.getString(),
  1279. hyphenationProperties.country.getString(),
  1280. getFObj().getUserAgent().getResourceResolver(),
  1281. getFObj().getUserAgent().getHyphenationPatternNames(),
  1282. sbChars.toString(),
  1283. hyphenationProperties.hyphenationRemainCharacterCount.getValue(),
  1284. hyphenationProperties.hyphenationPushCharacterCount.getValue());
  1285. // They hyph structure contains the information we need
  1286. // Now start from prev: reset to that position, ask that LM to get
  1287. // a Position for the first hyphenation offset. If the offset isn't in
  1288. // its characters, it returns null,
  1289. // but must tell how many chars it had.
  1290. // Keep looking at currentBP using next hyphenation point until the
  1291. // returned size is greater than the available size
  1292. // or no more hyphenation points remain. Choose the best break.
  1293. if (hyph != null) {
  1294. return new HyphContext(hyph.getHyphenationPoints());
  1295. } else {
  1296. return null;
  1297. }
  1298. }
  1299. /**
  1300. * Add the areas with the break points.
  1301. *
  1302. * @param parentIter the iterator of break positions
  1303. * @param context the context for adding areas
  1304. */
  1305. @Override
  1306. public void addAreas(PositionIterator parentIter,
  1307. LayoutContext context) {
  1308. while (parentIter.hasNext()) {
  1309. Position pos = parentIter.next();
  1310. boolean isLastPosition = !parentIter.hasNext();
  1311. if (pos instanceof LineBreakPosition) {
  1312. addInlineArea(context, (LineBreakPosition) pos, isLastPosition);
  1313. } else if ((pos instanceof NonLeafPosition) && pos.generatesAreas()) {
  1314. addBlockArea(context, pos, isLastPosition);
  1315. } else {
  1316. /*
  1317. * pos was the Position inside a penalty item, nothing to do;
  1318. * or Pos does not generate an area,
  1319. * i.e. it stand for spaces, borders and padding.
  1320. */
  1321. }
  1322. }
  1323. setCurrentArea(null); // ?? necessary
  1324. }
  1325. /**
  1326. * Add a line with inline content
  1327. * @param context the context for adding areas
  1328. * @param lbp the position for which the line is generated
  1329. * @param isLastPosition true if this is the last position of this LM
  1330. */
  1331. private void addInlineArea(LayoutContext context, LineBreakPosition lbp,
  1332. boolean isLastPosition) {
  1333. KnuthSequence seq = knuthParagraphs.get(lbp.parIndex);
  1334. int startElementIndex = lbp.startIndex;
  1335. int endElementIndex = lbp.getLeafPos();
  1336. LineArea lineArea = new LineArea(
  1337. (lbp.getLeafPos() < seq.size() - 1 ? textAlignment : textAlignmentLast),
  1338. lbp.difference, lbp.availableStretch, lbp.availableShrink);
  1339. if (lbp.startIndent != 0) {
  1340. lineArea.addTrait(Trait.START_INDENT, lbp.startIndent);
  1341. }
  1342. if (lbp.endIndent != 0) {
  1343. lineArea.addTrait(Trait.END_INDENT, new Integer(lbp.endIndent));
  1344. }
  1345. lineArea.setBPD(lbp.lineHeight);
  1346. lineArea.setIPD(lbp.lineWidth);
  1347. lineArea.setBidiLevel(bidiLevel);
  1348. lineArea.addTrait(Trait.SPACE_BEFORE, lbp.spaceBefore);
  1349. lineArea.addTrait(Trait.SPACE_AFTER, lbp.spaceAfter);
  1350. alignmentContext.resizeLine(lbp.lineHeight, lbp.baseline);
  1351. if (seq instanceof Paragraph) {
  1352. Paragraph currPar = (Paragraph) seq;
  1353. // ignore the first elements added by the LineLayoutManager
  1354. startElementIndex += (startElementIndex == 0) ? currPar.ignoreAtStart : 0;
  1355. // if this is the last line area that for this paragraph,
  1356. // ignore the last elements added by the LineLayoutManager and
  1357. // subtract the last-line-end-indent from the area ipd
  1358. if (endElementIndex == (currPar.size() - 1)) {
  1359. endElementIndex -= currPar.ignoreAtEnd;
  1360. lineArea.setIPD(lineArea.getIPD() - lastLineEndIndent.getValue(this));
  1361. }
  1362. }
  1363. // ignore the last element in the line if it is a KnuthGlue object
  1364. ListIterator seqIterator = seq.listIterator(endElementIndex);
  1365. KnuthElement lastElement = (KnuthElement) seqIterator.next();
  1366. // the TLM which created the last KnuthElement in this line
  1367. LayoutManager lastLM = lastElement.getLayoutManager();
  1368. if (lastElement.isGlue()) {
  1369. // Remove trailing spaces if allowed so
  1370. if (whiteSpaceTreament == EN_IGNORE_IF_SURROUNDING_LINEFEED
  1371. || whiteSpaceTreament == EN_IGNORE
  1372. || whiteSpaceTreament == EN_IGNORE_IF_BEFORE_LINEFEED) {
  1373. endElementIndex--;
  1374. // this returns the same KnuthElement
  1375. seqIterator.previous();
  1376. if (seqIterator.hasPrevious()) {
  1377. lastLM = ((KnuthElement) seqIterator.previous()).getLayoutManager();
  1378. }
  1379. }
  1380. }
  1381. // Remove leading spaces if allowed so
  1382. if (whiteSpaceTreament == EN_IGNORE_IF_SURROUNDING_LINEFEED
  1383. || whiteSpaceTreament == EN_IGNORE
  1384. || whiteSpaceTreament == EN_IGNORE_IF_AFTER_LINEFEED) {
  1385. // ignore KnuthGlue and KnuthPenalty objects
  1386. // at the beginning of the line
  1387. seqIterator = seq.listIterator(startElementIndex);
  1388. while (seqIterator.hasNext() && !((KnuthElement) seqIterator.next()).isBox()) {
  1389. startElementIndex++;
  1390. }
  1391. }
  1392. // Add the inline areas to lineArea
  1393. PositionIterator inlinePosIter = new KnuthPossPosIter(seq, startElementIndex,
  1394. endElementIndex + 1);
  1395. LayoutContext lc = LayoutContext.offspringOf(context);
  1396. lc.setAlignmentContext(alignmentContext);
  1397. lc.setSpaceAdjust(lbp.dAdjust);
  1398. lc.setIPDAdjust(lbp.ipdAdjust);
  1399. lc.setLeadingSpace(new SpaceSpecifier(true));
  1400. lc.setTrailingSpace(new SpaceSpecifier(false));
  1401. lc.setFlags(LayoutContext.RESOLVE_LEADING_SPACE, true);
  1402. setCurrentArea(lineArea);
  1403. setChildContext(lc);
  1404. LayoutManager childLM;
  1405. while ((childLM = inlinePosIter.getNextChildLM()) != null) {
  1406. lc.setFlags(LayoutContext.LAST_AREA, (childLM == lastLM));
  1407. childLM.addAreas(inlinePosIter, lc);
  1408. lc.setLeadingSpace(lc.getTrailingSpace());
  1409. lc.setTrailingSpace(new SpaceSpecifier(false));
  1410. }
  1411. // if display-align is distribute, add space after
  1412. if (context.getSpaceAfter() > 0
  1413. && (!context.isLastArea() || !isLastPosition)) {
  1414. lineArea.setBPD(lineArea.getBPD() + context.getSpaceAfter());
  1415. }
  1416. lineArea.finish();
  1417. if ( lineArea.getBidiLevel() >= 0 ) {
  1418. BidiResolver.reorder ( lineArea );
  1419. }
  1420. parentLayoutManager.addChildArea(lineArea);
  1421. }
  1422. /**
  1423. * Add a line with block content
  1424. * @param context the context for adding areas
  1425. * @param pos the position for which the line is generated
  1426. * @param isLastPosition true if this is the last position of this LM
  1427. */
  1428. private void addBlockArea(LayoutContext context, Position pos, boolean isLastPosition) {
  1429. /* Nested block-level content;
  1430. * go down the LM stack again;
  1431. * "unwrap" the positions and put the child positions in a new list.
  1432. * The positionList must contain one area-generating position,
  1433. * which creates one line area.
  1434. */
  1435. List positionList = new ArrayList(1);
  1436. Position innerPosition = pos.getPosition();
  1437. positionList.add(innerPosition);
  1438. // do we have the last LM?
  1439. LayoutManager lastLM = null;
  1440. if (isLastPosition) {
  1441. lastLM = innerPosition.getLM();
  1442. }
  1443. LineArea lineArea = new LineArea();
  1444. setCurrentArea(lineArea);
  1445. LayoutContext lc = LayoutContext.newInstance();
  1446. lc.setAlignmentContext(alignmentContext);
  1447. setChildContext(lc);
  1448. PositionIterator childPosIter = new PositionIterator(positionList.listIterator());
  1449. LayoutContext blocklc = LayoutContext.offspringOf(context);
  1450. blocklc.setLeadingSpace(new SpaceSpecifier(true));
  1451. blocklc.setTrailingSpace(new SpaceSpecifier(false));
  1452. blocklc.setFlags(LayoutContext.RESOLVE_LEADING_SPACE, true);
  1453. LayoutManager childLM;
  1454. while ((childLM = childPosIter.getNextChildLM()) != null) {
  1455. // set last area flag
  1456. blocklc.setFlags(LayoutContext.LAST_AREA,
  1457. (context.isLastArea() && childLM == lastLM));
  1458. blocklc.setStackLimitBP(context.getStackLimitBP());
  1459. // Add the line areas to Area
  1460. childLM.addAreas(childPosIter, blocklc);
  1461. blocklc.setLeadingSpace(blocklc.getTrailingSpace());
  1462. blocklc.setTrailingSpace(new SpaceSpecifier(false));
  1463. }
  1464. lineArea.updateExtentsFromChildren();
  1465. if ( lineArea.getBidiLevel() >= 0 ) {
  1466. BidiResolver.reorder ( lineArea );
  1467. }
  1468. parentLayoutManager.addChildArea(lineArea);
  1469. }
  1470. /** {@inheritDoc} */
  1471. @Override
  1472. public void addChildArea(Area childArea) {
  1473. // Make sure childArea is inline area
  1474. if (childArea instanceof InlineArea) {
  1475. Area parent = getCurrentArea();
  1476. if (getContext().resolveLeadingSpace()) {
  1477. addSpace(parent, getContext().getLeadingSpace().resolve(false),
  1478. getContext().getSpaceAdjust());
  1479. }
  1480. parent.addChildArea(childArea);
  1481. }
  1482. }
  1483. // --------- Property Resolution related functions --------- //
  1484. /** {@inheritDoc} */
  1485. @Override
  1486. public boolean getGeneratesBlockArea() {
  1487. return true;
  1488. }
  1489. /** {@inheritDoc} */
  1490. @Override
  1491. public boolean getGeneratesLineArea() {
  1492. return true;
  1493. }
  1494. /** {@inheritDoc} */
  1495. @Override
  1496. public boolean isRestartable() {
  1497. return true;
  1498. }
  1499. /**
  1500. * Whether this LM can handle horizontal overflow error messages (only a BlockContainerLayoutManager can).
  1501. * @param milliPoints horizontal overflow
  1502. * @return true if handled by a BlockContainerLayoutManager
  1503. */
  1504. public boolean handleOverflow(int milliPoints) {
  1505. if (getParent() instanceof BlockLayoutManager) {
  1506. return ((BlockLayoutManager) getParent()).handleOverflow(milliPoints);
  1507. }
  1508. return false;
  1509. }
  1510. }