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.

LayoutContext.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.layoutmgr;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import org.apache.fop.fo.Constants;
  22. import org.apache.fop.layoutmgr.inline.AlignmentContext;
  23. import org.apache.fop.layoutmgr.inline.HyphContext;
  24. import org.apache.fop.traits.MinOptMax;
  25. /**
  26. * This class is used to pass information to the getNextKnuthElements()
  27. * method. It is set up by higher level LM and used by lower level LM.
  28. */
  29. public class LayoutContext {
  30. /**
  31. * Values for flags.
  32. */
  33. public static final int LINEBREAK_AT_LF_ONLY = 0x01;
  34. /** Generated break possibility is first in a new area */
  35. public static final int NEW_AREA = 0x02;
  36. public static final int IPD_UNKNOWN = 0x04;
  37. /** Signal to a Line LM that a higher level LM may provoke a change
  38. * in the reference area, thus ref area IPD. The LineLM should return
  39. * without looking for a line break.
  40. */
  41. public static final int CHECK_REF_AREA = 0x08;
  42. /**
  43. * If this flag is set, it indicates that any break-before values other than "auto" should
  44. * not cause a mandatory break as this break was already handled by a parent layout manager.
  45. */
  46. public static final int SUPPRESS_BREAK_BEFORE = 0x10;
  47. public static final int FIRST_AREA = 0x20;
  48. public static final int TRY_HYPHENATE = 0x40;
  49. public static final int LAST_AREA = 0x80;
  50. public static final int RESOLVE_LEADING_SPACE = 0x100;
  51. /**
  52. * This flag indicates that there's a keep-with-next that hasn't
  53. * been processed, yet.
  54. */
  55. //public static final int KEEP_WITH_NEXT_PENDING = 0x200;
  56. /**
  57. * This flag indicates that there's a keep-with-previous that hasn't
  58. * been processed, yet.
  59. */
  60. //public static final int KEEP_WITH_PREVIOUS_PENDING = 0x400;
  61. private int flags; // Contains some set of flags defined above
  62. /**
  63. * Total available stacking dimension for a "galley-level" layout
  64. * manager in block-progression-direction. It is passed by the
  65. * parent LM.
  66. * These LM <b>may</b> wish to pass this information down to lower
  67. * level LM to allow them to optimize returned break possibilities.
  68. */
  69. private MinOptMax stackLimitBP;
  70. /** to keep track of spanning in multi-column layout */
  71. private int currentSpan = Constants.NOT_SET;
  72. private int nextSpan = Constants.NOT_SET;
  73. /** inline-progression-dimension of nearest ancestor reference area */
  74. private int refIPD;
  75. //TODO After the split of stackLimit into stackLimitBP and stackLimitIP there's now some
  76. //overlap with refIPD. Need to investigate how best to refactor that.
  77. /** the writing mode established by the nearest ancestor reference area */
  78. private int writingMode = Constants.EN_LR_TB;
  79. /** Current pending space-after or space-end from preceding area */
  80. private SpaceSpecifier trailingSpace;
  81. /** Current pending space-before or space-start from ancestor areas */
  82. private SpaceSpecifier leadingSpace;
  83. /**
  84. * A list of pending marks (border and padding) on the after edge when a page break occurs.
  85. * May be null.
  86. */
  87. private List pendingAfterMarks;
  88. /**
  89. * A list of pending marks (border and padding) on the before edge when a page break occurs.
  90. * May be null.
  91. */
  92. private List pendingBeforeMarks;
  93. /** Current hyphenation context. May be null. */
  94. private HyphContext hyphContext = null;
  95. /** Alignment in BP direction */
  96. private int bpAlignment = Constants.EN_START;
  97. /** Stretch or shrink value when making areas. */
  98. private double ipdAdjust = 0.0;
  99. /** Stretch or shrink value when adding spaces. */
  100. private double dSpaceAdjust = 0.0;
  101. private AlignmentContext alignmentContext = null;
  102. /** Amount of space before / start */
  103. private int spaceBefore = 0;
  104. /** Amount of space after / end */
  105. private int spaceAfter = 0;
  106. /** Amount of space to reserve at the beginning of each line */
  107. private int lineStartBorderAndPaddingWidth = 0;
  108. /** Amount of space to reserve at the end of each line */
  109. private int lineEndBorderAndPaddingWidth = 0;
  110. private int breakBefore;
  111. private int breakAfter;
  112. private Keep pendingKeepWithNext = Keep.KEEP_AUTO;
  113. private Keep pendingKeepWithPrevious = Keep.KEEP_AUTO;
  114. private int disableColumnBalancing;
  115. /**
  116. * Copy constructor for creating child layout contexts.
  117. * @param parentLC the parent layout context to copy from
  118. */
  119. public LayoutContext(LayoutContext parentLC) {
  120. this.flags = parentLC.flags;
  121. this.refIPD = parentLC.refIPD;
  122. this.writingMode = parentLC.writingMode;
  123. setStackLimitBP(parentLC.getStackLimitBP());
  124. this.leadingSpace = parentLC.leadingSpace; //???
  125. this.trailingSpace = parentLC.trailingSpace; //???
  126. this.hyphContext = parentLC.hyphContext;
  127. this.bpAlignment = parentLC.bpAlignment;
  128. this.dSpaceAdjust = parentLC.dSpaceAdjust;
  129. this.ipdAdjust = parentLC.ipdAdjust;
  130. this.alignmentContext = parentLC.alignmentContext;
  131. this.lineStartBorderAndPaddingWidth = parentLC.lineStartBorderAndPaddingWidth;
  132. this.lineEndBorderAndPaddingWidth = parentLC.lineEndBorderAndPaddingWidth;
  133. copyPendingMarksFrom(parentLC);
  134. this.pendingKeepWithNext = parentLC.pendingKeepWithNext;
  135. this.pendingKeepWithPrevious = parentLC.pendingKeepWithPrevious;
  136. // Copy other fields as necessary.
  137. this.disableColumnBalancing = parentLC.disableColumnBalancing;
  138. }
  139. /**
  140. * Main constructor.
  141. * @param flags the initial flags
  142. */
  143. public LayoutContext(int flags) {
  144. this.flags = flags;
  145. this.refIPD = 0;
  146. stackLimitBP = new MinOptMax(0);
  147. leadingSpace = null;
  148. trailingSpace = null;
  149. }
  150. public void copyPendingMarksFrom(LayoutContext source) {
  151. if (source.pendingAfterMarks != null) {
  152. this.pendingAfterMarks = new java.util.ArrayList(source.pendingAfterMarks);
  153. }
  154. if (source.pendingBeforeMarks != null) {
  155. this.pendingBeforeMarks = new java.util.ArrayList(source.pendingBeforeMarks);
  156. }
  157. }
  158. public void setFlags(int flags) {
  159. setFlags(flags, true);
  160. }
  161. public void setFlags(int flags, boolean bSet) {
  162. if (bSet) {
  163. this.flags |= flags;
  164. } else {
  165. this.flags &= ~flags;
  166. }
  167. }
  168. public void unsetFlags(int flags) {
  169. setFlags(flags, false);
  170. }
  171. public boolean isStart() {
  172. return ((this.flags & NEW_AREA) != 0);
  173. }
  174. public boolean startsNewArea() {
  175. return ((this.flags & NEW_AREA) != 0 && leadingSpace != null);
  176. }
  177. public boolean isFirstArea() {
  178. return ((this.flags & FIRST_AREA) != 0);
  179. }
  180. public boolean isLastArea() {
  181. return ((this.flags & LAST_AREA) != 0);
  182. }
  183. public boolean suppressBreakBefore() {
  184. return ((this.flags & SUPPRESS_BREAK_BEFORE) != 0);
  185. }
  186. /**
  187. * Returns the strength of a keep-with-next currently pending.
  188. * @return the keep-with-next strength
  189. */
  190. public Keep getKeepWithNextPending() {
  191. return this.pendingKeepWithNext;
  192. }
  193. /**
  194. * Returns the strength of a keep-with-previous currently pending.
  195. * @return the keep-with-previous strength
  196. */
  197. public Keep getKeepWithPreviousPending() {
  198. return this.pendingKeepWithPrevious;
  199. }
  200. /**
  201. * Clears any pending keep-with-next strength.
  202. */
  203. public void clearKeepWithNextPending() {
  204. this.pendingKeepWithNext = Keep.KEEP_AUTO;
  205. }
  206. /**
  207. * Clears any pending keep-with-previous strength.
  208. */
  209. public void clearKeepWithPreviousPending() {
  210. this.pendingKeepWithPrevious = Keep.KEEP_AUTO;
  211. }
  212. /**
  213. * Clears both keep-with-previous and keep-with-next strengths.
  214. */
  215. public void clearKeepsPending() {
  216. clearKeepWithPreviousPending();
  217. clearKeepWithNextPending();
  218. }
  219. /**
  220. * Updates the currently pending keep-with-next strength.
  221. * @param keep the new strength to consider
  222. */
  223. public void updateKeepWithNextPending(Keep keep) {
  224. this.pendingKeepWithNext = this.pendingKeepWithNext.compare(keep);
  225. }
  226. /**
  227. * Updates the currently pending keep-with-previous strength.
  228. * @param keep the new strength to consider
  229. */
  230. public void updateKeepWithPreviousPending(Keep keep) {
  231. this.pendingKeepWithPrevious = this.pendingKeepWithPrevious.compare(keep);
  232. }
  233. /**
  234. * Indicates whether a keep-with-next constraint is pending.
  235. * @return true if a keep-with-next constraint is pending
  236. */
  237. public boolean isKeepWithNextPending() {
  238. return !getKeepWithNextPending().isAuto();
  239. }
  240. /**
  241. * Indicates whether a keep-with-previous constraint is pending.
  242. * @return true if a keep-with-previous constraint is pending
  243. */
  244. public boolean isKeepWithPreviousPending() {
  245. return !getKeepWithPreviousPending().isAuto();
  246. }
  247. public void setLeadingSpace(SpaceSpecifier space) {
  248. leadingSpace = space;
  249. }
  250. public SpaceSpecifier getLeadingSpace() {
  251. return leadingSpace;
  252. }
  253. public boolean resolveLeadingSpace() {
  254. return ((this.flags & RESOLVE_LEADING_SPACE) != 0);
  255. }
  256. public void setTrailingSpace(SpaceSpecifier space) {
  257. trailingSpace = space;
  258. }
  259. public SpaceSpecifier getTrailingSpace() {
  260. return trailingSpace;
  261. }
  262. /**
  263. * Adds a border or padding element to the pending list which will be used to generate
  264. * the right element list for break possibilities. Conditionality resolution will be done
  265. * elsewhere.
  266. * @param element the border, padding or space element
  267. */
  268. public void addPendingAfterMark(UnresolvedListElementWithLength element) {
  269. if (this.pendingAfterMarks == null) {
  270. this.pendingAfterMarks = new java.util.ArrayList();
  271. }
  272. this.pendingAfterMarks.add(element);
  273. }
  274. /**
  275. * @return the pending border and padding elements at the after edge
  276. * @see #addPendingAfterMark(UnresolvedListElementWithLength)
  277. */
  278. public List getPendingAfterMarks() {
  279. if (this.pendingAfterMarks != null) {
  280. return Collections.unmodifiableList(this.pendingAfterMarks);
  281. } else {
  282. return null;
  283. }
  284. }
  285. /**
  286. * Clears all pending marks on the LayoutContext.
  287. */
  288. public void clearPendingMarks() {
  289. this.pendingBeforeMarks = null;
  290. this.pendingAfterMarks = null;
  291. }
  292. /**
  293. * Adds a border or padding element to the pending list which will be used to generate
  294. * the right element list for break possibilities. Conditionality resolution will be done
  295. * elsewhere.
  296. * @param element the border, padding or space element
  297. */
  298. public void addPendingBeforeMark(UnresolvedListElementWithLength element) {
  299. if (this.pendingBeforeMarks == null) {
  300. this.pendingBeforeMarks = new java.util.ArrayList();
  301. }
  302. this.pendingBeforeMarks.add(element);
  303. }
  304. /**
  305. * @return the pending border and padding elements at the before edge
  306. * @see #addPendingBeforeMark(UnresolvedListElementWithLength)
  307. */
  308. public List getPendingBeforeMarks() {
  309. if (this.pendingBeforeMarks != null) {
  310. return Collections.unmodifiableList(this.pendingBeforeMarks);
  311. } else {
  312. return null;
  313. }
  314. }
  315. /**
  316. * Sets the stack limit in block-progression-dimension.
  317. * @param limit the stack limit
  318. */
  319. public void setStackLimitBP(MinOptMax limit) {
  320. stackLimitBP = limit;
  321. }
  322. /**
  323. * Returns the stack limit in block-progression-dimension.
  324. * @return the stack limit
  325. */
  326. public MinOptMax getStackLimitBP() {
  327. return stackLimitBP;
  328. }
  329. /**
  330. * Sets the inline-progression-dimension of the nearest ancestor reference area.
  331. */
  332. public void setRefIPD(int ipd) {
  333. refIPD = ipd;
  334. }
  335. /**
  336. * Returns the inline-progression-dimension of the nearest ancestor reference area.
  337. *
  338. * @return the inline-progression-dimension of the nearest ancestor reference area
  339. */
  340. public int getRefIPD() {
  341. return refIPD;
  342. }
  343. public void setHyphContext(HyphContext hyph) {
  344. hyphContext = hyph;
  345. }
  346. public HyphContext getHyphContext() {
  347. return hyphContext;
  348. }
  349. public boolean tryHyphenate() {
  350. return ((this.flags & TRY_HYPHENATE) != 0);
  351. }
  352. /**
  353. * Sets the currently applicable alignment in BP direction.
  354. * @param alignment one of EN_START, EN_JUSTIFY etc.
  355. */
  356. public void setBPAlignment(int alignment) {
  357. this.bpAlignment = alignment;
  358. }
  359. /** @return the currently applicable alignment in BP direction (EN_START, EN_JUSTIFY...) */
  360. public int getBPAlignment() {
  361. return this.bpAlignment;
  362. }
  363. public void setSpaceAdjust(double adjust) {
  364. dSpaceAdjust = adjust;
  365. }
  366. public double getSpaceAdjust() {
  367. return dSpaceAdjust;
  368. }
  369. public void setIPDAdjust(double ipdA) {
  370. ipdAdjust = ipdA;
  371. }
  372. public double getIPDAdjust() {
  373. return ipdAdjust;
  374. }
  375. public void setAlignmentContext(AlignmentContext alignmentContext) {
  376. this.alignmentContext = alignmentContext;
  377. }
  378. public AlignmentContext getAlignmentContext() {
  379. return this.alignmentContext;
  380. }
  381. public void resetAlignmentContext() {
  382. if (this.alignmentContext != null) {
  383. this.alignmentContext = this.alignmentContext.getParentAlignmentContext();
  384. }
  385. }
  386. /**
  387. * Get the width to be reserved for border and padding at the start of the line.
  388. * @return the width to be reserved
  389. */
  390. public int getLineStartBorderAndPaddingWidth() {
  391. return lineStartBorderAndPaddingWidth;
  392. }
  393. /**
  394. * Set the width to be reserved for border and padding at the start of the line.
  395. * @param lineStartBorderAndPaddingWidth the width to be reserved
  396. */
  397. public void setLineStartBorderAndPaddingWidth(int lineStartBorderAndPaddingWidth) {
  398. this.lineStartBorderAndPaddingWidth = lineStartBorderAndPaddingWidth;
  399. }
  400. /**
  401. * Get the width to be reserved for border and padding at the end of the line.
  402. * @return the width to be reserved
  403. */
  404. public int getLineEndBorderAndPaddingWidth() {
  405. return lineEndBorderAndPaddingWidth;
  406. }
  407. /**
  408. * Set the width to be reserved for border and padding at the end of the line.
  409. * @param lineEndBorderAndPaddingWidth the width to be reserved
  410. */
  411. public void setLineEndBorderAndPaddingWidth(int lineEndBorderAndPaddingWidth) {
  412. this.lineEndBorderAndPaddingWidth = lineEndBorderAndPaddingWidth;
  413. }
  414. /**
  415. * @return one of: {@link Constants#NOT_SET}, {@link Constants#EN_NONE}
  416. * {@link Constants#EN_ALL}
  417. */
  418. public int getNextSpan() {
  419. return nextSpan;
  420. }
  421. /**
  422. * @return one of: {@link Constants#NOT_SET}, {@link Constants#EN_NONE}
  423. * {@link Constants#EN_ALL}
  424. */
  425. public int getCurrentSpan() {
  426. return (currentSpan == Constants.NOT_SET)
  427. ? Constants.EN_NONE : currentSpan;
  428. }
  429. /**
  430. * Used to signal the PSLM that the element list ends early because of a span change in
  431. * multi-column layout.
  432. * @param span the new span value (legal values: NOT_SET, EN_NONE, EN_ALL)
  433. */
  434. public void signalSpanChange(int span) {
  435. switch (span) {
  436. case Constants.NOT_SET:
  437. case Constants.EN_NONE:
  438. case Constants.EN_ALL:
  439. this.currentSpan = this.nextSpan;
  440. this.nextSpan = span;
  441. break;
  442. default:
  443. assert false;
  444. throw new IllegalArgumentException("Illegal value on signalSpanChange() for span: "
  445. + span);
  446. }
  447. }
  448. /**
  449. * Get the writing mode of the relevant reference area.
  450. * @return the applicable writing mode
  451. */
  452. public int getWritingMode() {
  453. return writingMode;
  454. }
  455. /**
  456. * Set the writing mode.
  457. * @param writingMode the writing mode
  458. */
  459. public void setWritingMode(int writingMode) {
  460. this.writingMode = writingMode;
  461. }
  462. /**
  463. * Get the current amount of space before / start
  464. * @return the space before / start amount
  465. */
  466. public int getSpaceBefore() {
  467. return spaceBefore;
  468. }
  469. /**
  470. * Set the amount of space before / start
  471. * @param spaceBefore the amount of space before / start
  472. */
  473. public void setSpaceBefore(int spaceBefore) {
  474. this.spaceBefore = spaceBefore;
  475. }
  476. /**
  477. * Get the current amount of space after / end
  478. * @return the space after / end amount
  479. */
  480. public int getSpaceAfter() {
  481. return spaceAfter;
  482. }
  483. /**
  484. * Set the amount of space after / end
  485. * @param spaceAfter the amount of space after / end
  486. */
  487. public void setSpaceAfter(int spaceAfter) {
  488. this.spaceAfter = spaceAfter;
  489. }
  490. /**
  491. * Returns the value of the break before the element whose
  492. * {@link LayoutManager#getNextKnuthElements(LayoutContext, int)} method has just been
  493. * called.
  494. *
  495. * @return one of {@link Constants#EN_AUTO}, {@link Constants#EN_COLUMN},
  496. * {@link Constants#EN_PAGE}, {@link Constants#EN_EVEN_PAGE}, or
  497. * {@link Constants#EN_ODD_PAGE}
  498. */
  499. public int getBreakBefore() {
  500. return breakBefore;
  501. }
  502. /**
  503. * Sets the value of the break before the current element.
  504. *
  505. * @param breakBefore the value of the break-before
  506. * @see #getBreakBefore()
  507. */
  508. public void setBreakBefore(int breakBefore) {
  509. this.breakBefore = breakBefore;
  510. }
  511. /**
  512. * Returns the value of the break after the element whose
  513. * {@link LayoutManager#getNextKnuthElements(LayoutContext, int)} method has just been
  514. * called.
  515. *
  516. * @return one of {@link Constants#EN_AUTO}, {@link Constants#EN_COLUMN},
  517. * {@link Constants#EN_PAGE}, {@link Constants#EN_EVEN_PAGE}, or
  518. * {@link Constants#EN_ODD_PAGE}
  519. */
  520. public int getBreakAfter() {
  521. return breakAfter;
  522. }
  523. /**
  524. * Sets the value of the break after the current element.
  525. *
  526. * @param breakAfter the value of the break-after
  527. * @see #getBreakAfter()
  528. */
  529. public void setBreakAfter(int breakAfter) {
  530. this.breakAfter = breakAfter;
  531. }
  532. /** {@inheritDoc} */
  533. public String toString() {
  534. return "Layout Context:"
  535. + "\nStack Limit BPD: \t"
  536. + (getStackLimitBP() == null ? "null" : getStackLimitBP().toString())
  537. + "\nTrailing Space: \t"
  538. + (getTrailingSpace() == null ? "null" : getTrailingSpace().toString())
  539. + "\nLeading Space: \t"
  540. + (getLeadingSpace() == null ? "null" : getLeadingSpace().toString())
  541. + "\nReference IPD: \t" + getRefIPD()
  542. + "\nSpace Adjust: \t" + getSpaceAdjust()
  543. + "\nIPD Adjust: \t" + getIPDAdjust()
  544. + "\nResolve Leading Space: \t" + resolveLeadingSpace()
  545. + "\nSuppress Break Before: \t" + suppressBreakBefore()
  546. + "\nIs First Area: \t" + isFirstArea()
  547. + "\nStarts New Area: \t" + startsNewArea()
  548. + "\nIs Last Area: \t" + isLastArea()
  549. + "\nTry Hyphenate: \t" + tryHyphenate()
  550. + "\nKeeps: \t[keep-with-next=" + getKeepWithNextPending()
  551. + "][keep-with-previous=" + getKeepWithPreviousPending() + "] pending"
  552. + "\nBreaks: \tforced [" + (breakBefore != Constants.EN_AUTO ? "break-before" : "") + "]["
  553. + (breakAfter != Constants.EN_AUTO ? "break-after" : "") + "]";
  554. }
  555. /**
  556. * Returns whether the column balancer should be disabled before a spanning block
  557. *
  558. * @return one of {@link Constants#EN_TRUE}, {@link Constants#EN_FALSE}
  559. */
  560. public int getDisableColumnBalancing() {
  561. return disableColumnBalancing;
  562. }
  563. /**
  564. * Sets whether the column balancer should be disabled before a spanning block
  565. *
  566. * @param disableColumnBalancing the value of the fox:disable-column-balancing property
  567. * @see #getDisableColumnBalancing()
  568. */
  569. public void setDisableColumnBalancing(int disableColumnBalancing) {
  570. this.disableColumnBalancing = disableColumnBalancing;
  571. }
  572. }