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 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 leading fo:character
  44. * objects with suppress-at-line-break="suppress" should not generate
  45. * areas. This is the case at the beginning of each new LineArea
  46. * except the first.
  47. */
  48. public static final int SUPPRESS_LEADING_SPACE = 0x10;
  49. public static final int FIRST_AREA = 0x20;
  50. public static final int TRY_HYPHENATE = 0x40;
  51. public static final int LAST_AREA = 0x80;
  52. public static final int RESOLVE_LEADING_SPACE = 0x100;
  53. /**
  54. * This flag indicates that there's a keep-with-next that hasn't
  55. * been processed, yet.
  56. */
  57. public static final int KEEP_WITH_NEXT_PENDING = 0x200;
  58. /**
  59. * This flag indicates that there's a keep-with-previous that hasn't
  60. * been processed, yet.
  61. */
  62. public static final int KEEP_WITH_PREVIOUS_PENDING = 0x400;
  63. private int flags; // Contains some set of flags defined above
  64. /**
  65. * Total available stacking dimension for a "galley-level" layout
  66. * manager in block-progression-direction. It is passed by the
  67. * parent LM.
  68. * These LM <b>may</b> wish to pass this information down to lower
  69. * level LM to allow them to optimize returned break possibilities.
  70. */
  71. private MinOptMax stackLimitBP;
  72. /**
  73. * Total available stacking dimension for a "galley-level" layout
  74. * manager in inline-progression-direction. It is passed by the
  75. * parent LM. For LineLM, the block LM determines this based on
  76. * indent properties.
  77. * These LM <b>may</b> wish to pass this information down to lower
  78. * level LM to allow them to optimize returned break possibilities.
  79. */
  80. private MinOptMax stackLimitIP;
  81. /** True if current element list is spanning in multi-column layout. */
  82. private int nextSpan = Constants.NOT_SET;
  83. /** inline-progression-dimension of nearest ancestor reference area */
  84. private int refIPD;
  85. //TODO After the split of stackLimit into stackLimitBP and stackLimitIP there's now some
  86. //overlap with refIPD. Need to investigate how best to refactor that.
  87. /** the writing mode established by the nearest ancestor reference area */
  88. private int writingMode = Constants.EN_LR_TB;
  89. /** Current pending space-after or space-end from preceding area */
  90. private SpaceSpecifier trailingSpace;
  91. /** Current pending space-before or space-start from ancestor areas */
  92. private SpaceSpecifier leadingSpace;
  93. /**
  94. * A list of pending marks (border and padding) on the after edge when a page break occurs.
  95. * May be null.
  96. */
  97. private List pendingAfterMarks;
  98. /**
  99. * A list of pending marks (border and padding) on the before edge when a page break occurs.
  100. * May be null.
  101. */
  102. private List pendingBeforeMarks;
  103. /** Current hyphenation context. May be null. */
  104. private HyphContext hyphContext = null;
  105. /** Alignment in BP direction */
  106. private int bpAlignment = Constants.EN_START;
  107. /** Stretch or shrink value when making areas. */
  108. private double ipdAdjust = 0.0;
  109. /** Stretch or shrink value when adding spaces. */
  110. private double dSpaceAdjust = 0.0;
  111. private AlignmentContext alignmentContext = null;
  112. /** Amount of space before / start */
  113. private int spaceBefore = 0;
  114. /** Amount of space after / end */
  115. private int spaceAfter = 0;
  116. /** Amount of space to reserve at the beginning of each line */
  117. private int lineStartBorderAndPaddingWidth = 0;
  118. /** Amount of space to reserve at the end of each line */
  119. private int lineEndBorderAndPaddingWidth = 0;
  120. private int breakBefore;
  121. private int breakAfter;
  122. /**
  123. * Copy constructor for creating child layout contexts.
  124. * @param parentLC the parent layout context to copy from
  125. */
  126. public LayoutContext(LayoutContext parentLC) {
  127. this.flags = parentLC.flags;
  128. this.refIPD = parentLC.refIPD;
  129. this.writingMode = parentLC.writingMode;
  130. setStackLimitsFrom(parentLC);
  131. this.leadingSpace = parentLC.leadingSpace; //???
  132. this.trailingSpace = parentLC.trailingSpace; //???
  133. this.hyphContext = parentLC.hyphContext;
  134. this.bpAlignment = parentLC.bpAlignment;
  135. this.dSpaceAdjust = parentLC.dSpaceAdjust;
  136. this.ipdAdjust = parentLC.ipdAdjust;
  137. this.alignmentContext = parentLC.alignmentContext;
  138. this.lineStartBorderAndPaddingWidth = parentLC.lineStartBorderAndPaddingWidth;
  139. this.lineEndBorderAndPaddingWidth = parentLC.lineEndBorderAndPaddingWidth;
  140. copyPendingMarksFrom(parentLC);
  141. // Copy other fields as necessary.
  142. }
  143. /**
  144. * Main constructor.
  145. * @param flags the initial flags
  146. */
  147. public LayoutContext(int flags) {
  148. this.flags = flags;
  149. this.refIPD = 0;
  150. stackLimitBP = new MinOptMax(0);
  151. stackLimitIP = new MinOptMax(0);
  152. leadingSpace = null;
  153. trailingSpace = null;
  154. }
  155. public void copyPendingMarksFrom(LayoutContext source) {
  156. if (source.pendingAfterMarks != null) {
  157. this.pendingAfterMarks = new java.util.ArrayList(source.pendingAfterMarks);
  158. }
  159. if (source.pendingBeforeMarks != null) {
  160. this.pendingBeforeMarks = new java.util.ArrayList(source.pendingBeforeMarks);
  161. }
  162. }
  163. public void setFlags(int flags) {
  164. setFlags(flags, true);
  165. }
  166. public void setFlags(int flags, boolean bSet) {
  167. if (bSet) {
  168. this.flags |= flags;
  169. } else {
  170. this.flags &= ~flags;
  171. }
  172. }
  173. public void unsetFlags(int flags) {
  174. setFlags(flags, false);
  175. }
  176. public boolean isStart() {
  177. return ((this.flags & NEW_AREA) != 0);
  178. }
  179. public boolean startsNewArea() {
  180. return ((this.flags & NEW_AREA) != 0 && leadingSpace != null);
  181. }
  182. public boolean isFirstArea() {
  183. return ((this.flags & FIRST_AREA) != 0);
  184. }
  185. public boolean isLastArea() {
  186. return ((this.flags & LAST_AREA) != 0);
  187. }
  188. public boolean suppressLeadingSpace() {
  189. return ((this.flags & SUPPRESS_LEADING_SPACE) != 0);
  190. }
  191. public boolean isKeepWithNextPending() {
  192. return ((this.flags & KEEP_WITH_NEXT_PENDING) != 0);
  193. }
  194. public boolean isKeepWithPreviousPending() {
  195. return ((this.flags & KEEP_WITH_PREVIOUS_PENDING) != 0);
  196. }
  197. public void setLeadingSpace(SpaceSpecifier space) {
  198. leadingSpace = space;
  199. }
  200. public SpaceSpecifier getLeadingSpace() {
  201. return leadingSpace;
  202. }
  203. public boolean resolveLeadingSpace() {
  204. return ((this.flags & RESOLVE_LEADING_SPACE) != 0);
  205. }
  206. public void setTrailingSpace(SpaceSpecifier space) {
  207. trailingSpace = space;
  208. }
  209. public SpaceSpecifier getTrailingSpace() {
  210. return trailingSpace;
  211. }
  212. /**
  213. * Adds a border or padding element to the pending list which will be used to generate
  214. * the right element list for break possibilities. Conditionality resolution will be done
  215. * elsewhere.
  216. * @param element the border, padding or space element
  217. */
  218. public void addPendingAfterMark(UnresolvedListElementWithLength element) {
  219. if (this.pendingAfterMarks == null) {
  220. this.pendingAfterMarks = new java.util.ArrayList();
  221. }
  222. this.pendingAfterMarks.add(element);
  223. }
  224. /**
  225. * @return the pending border and padding elements at the after edge
  226. * @see #addPendingAfterMark(UnresolvedListElementWithLength)
  227. */
  228. public List getPendingAfterMarks() {
  229. if (this.pendingAfterMarks != null) {
  230. return Collections.unmodifiableList(this.pendingAfterMarks);
  231. } else {
  232. return null;
  233. }
  234. }
  235. /**
  236. * Clears all pending marks on the LayoutContext.
  237. */
  238. public void clearPendingMarks() {
  239. this.pendingBeforeMarks = null;
  240. this.pendingAfterMarks = null;
  241. }
  242. /**
  243. * Adds a border or padding element to the pending list which will be used to generate
  244. * the right element list for break possibilities. Conditionality resolution will be done
  245. * elsewhere.
  246. * @param element the border, padding or space element
  247. */
  248. public void addPendingBeforeMark(UnresolvedListElementWithLength element) {
  249. if (this.pendingBeforeMarks == null) {
  250. this.pendingBeforeMarks = new java.util.ArrayList();
  251. }
  252. this.pendingBeforeMarks.add(element);
  253. }
  254. /**
  255. * @return the pending border and padding elements at the before edge
  256. * @see #addPendingBeforeMark(UnresolvedListElementWithLength)
  257. */
  258. public List getPendingBeforeMarks() {
  259. if (this.pendingBeforeMarks != null) {
  260. return Collections.unmodifiableList(this.pendingBeforeMarks);
  261. } else {
  262. return null;
  263. }
  264. }
  265. /**
  266. * Sets the stack limit in block-progression-dimension.
  267. * @param limit the stack limit
  268. */
  269. public void setStackLimitBP(MinOptMax limit) {
  270. stackLimitBP = limit;
  271. }
  272. /**
  273. * Returns the stack limit in block-progression-dimension.
  274. * @return the stack limit
  275. */
  276. public MinOptMax getStackLimitBP() {
  277. return stackLimitBP;
  278. }
  279. /**
  280. * Sets the stack limit in inline-progression-dimension.
  281. * @param limit the stack limit
  282. */
  283. public void setStackLimitIP(MinOptMax limit) {
  284. stackLimitIP = limit;
  285. }
  286. /**
  287. * Returns the stack limit in inline-progression-dimension.
  288. * @return the stack limit
  289. */
  290. public MinOptMax getStackLimitIP() {
  291. return stackLimitIP;
  292. }
  293. /**
  294. * Sets (Copies) the stack limits in both directions from another layout context.
  295. * @param context the layout context to taje the values from
  296. */
  297. public void setStackLimitsFrom(LayoutContext context) {
  298. setStackLimitBP(context.getStackLimitBP());
  299. setStackLimitIP(context.getStackLimitIP());
  300. }
  301. /**
  302. * Sets the inline-progression-dimension of the nearest ancestor reference area.
  303. */
  304. public void setRefIPD(int ipd) {
  305. refIPD = ipd;
  306. }
  307. /**
  308. * Returns the inline-progression-dimension of the nearest ancestor reference area.
  309. *
  310. * @return the inline-progression-dimension of the nearest ancestor reference area
  311. */
  312. public int getRefIPD() {
  313. return refIPD;
  314. }
  315. public void setHyphContext(HyphContext hyph) {
  316. hyphContext = hyph;
  317. }
  318. public HyphContext getHyphContext() {
  319. return hyphContext;
  320. }
  321. public boolean tryHyphenate() {
  322. return ((this.flags & TRY_HYPHENATE) != 0);
  323. }
  324. /**
  325. * Sets the currently applicable alignment in BP direction.
  326. * @param alignment one of EN_START, EN_JUSTIFY etc.
  327. */
  328. public void setBPAlignment(int alignment) {
  329. this.bpAlignment = alignment;
  330. }
  331. /** @return the currently applicable alignment in BP direction (EN_START, EN_JUSTIFY...) */
  332. public int getBPAlignment() {
  333. return this.bpAlignment;
  334. }
  335. public void setSpaceAdjust(double adjust) {
  336. dSpaceAdjust = adjust;
  337. }
  338. public double getSpaceAdjust() {
  339. return dSpaceAdjust;
  340. }
  341. public void setIPDAdjust(double ipdA) {
  342. ipdAdjust = ipdA;
  343. }
  344. public double getIPDAdjust() {
  345. return ipdAdjust;
  346. }
  347. public void setAlignmentContext(AlignmentContext alignmentContext) {
  348. this.alignmentContext = alignmentContext;
  349. }
  350. public AlignmentContext getAlignmentContext() {
  351. return this.alignmentContext;
  352. }
  353. public void resetAlignmentContext() {
  354. if (this.alignmentContext != null) {
  355. this.alignmentContext = this.alignmentContext.getParentAlignmentContext();
  356. }
  357. }
  358. /**
  359. * Get the width to be reserved for border and padding at the start of the line.
  360. * @return the width to be reserved
  361. */
  362. public int getLineStartBorderAndPaddingWidth() {
  363. return lineStartBorderAndPaddingWidth;
  364. }
  365. /**
  366. * Set the width to be reserved for border and padding at the start of the line.
  367. * @param lineStartBorderAndPaddingWidth the width to be reserved
  368. */
  369. public void setLineStartBorderAndPaddingWidth(int lineStartBorderAndPaddingWidth) {
  370. this.lineStartBorderAndPaddingWidth = lineStartBorderAndPaddingWidth;
  371. }
  372. /**
  373. * Get the width to be reserved for border and padding at the end of the line.
  374. * @return the width to be reserved
  375. */
  376. public int getLineEndBorderAndPaddingWidth() {
  377. return lineEndBorderAndPaddingWidth;
  378. }
  379. /**
  380. * Set the width to be reserved for border and padding at the end of the line.
  381. * @param lineEndBorderAndPaddingWidth the width to be reserved
  382. */
  383. public void setLineEndBorderAndPaddingWidth(int lineEndBorderAndPaddingWidth) {
  384. this.lineEndBorderAndPaddingWidth = lineEndBorderAndPaddingWidth;
  385. }
  386. /**
  387. * @return true if the current element list ends early because of a span change
  388. * in multi-column layout.
  389. */
  390. public int getNextSpan() {
  391. return nextSpan;
  392. }
  393. /**
  394. * Used to signal the PSLM that the element list ends early because of a span change in
  395. * multi-column layout.
  396. * @param span the new span value (legal values: NOT_SET, EN_NONE, EN_ALL)
  397. */
  398. public void signalSpanChange(int span) {
  399. if (span == Constants.NOT_SET || span == Constants.EN_NONE || span == Constants.EN_ALL) {
  400. this.nextSpan = span;
  401. } else {
  402. throw new IllegalArgumentException("Illegal value on signalSpanChange() for span: "
  403. + span);
  404. }
  405. }
  406. /**
  407. * Get the writing mode of the relevant reference area.
  408. * @return the applicable writing mode
  409. */
  410. public int getWritingMode() {
  411. return writingMode;
  412. }
  413. /**
  414. * Set the writing mode.
  415. * @param writingMode the writing mode
  416. */
  417. public void setWritingMode(int writingMode) {
  418. this.writingMode = writingMode;
  419. }
  420. /**
  421. * Get the current amount of space before / start
  422. * @return the space before / start amount
  423. */
  424. public int getSpaceBefore() {
  425. return spaceBefore;
  426. }
  427. /**
  428. * Set the amount of space before / start
  429. * @param spaceBefore the amount of space before / start
  430. */
  431. public void setSpaceBefore(int spaceBefore) {
  432. this.spaceBefore = spaceBefore;
  433. }
  434. /**
  435. * Get the current amount of space after / end
  436. * @return the space after / end amount
  437. */
  438. public int getSpaceAfter() {
  439. return spaceAfter;
  440. }
  441. /**
  442. * Set the amount of space after / end
  443. * @param spaceAfter the amount of space after / end
  444. */
  445. public void setSpaceAfter(int spaceAfter) {
  446. this.spaceAfter = spaceAfter;
  447. }
  448. /**
  449. * Returns the value of the break before the element whose
  450. * {@link LayoutManager#getNextKnuthElements(LayoutContext, int)} method has just been
  451. * called.
  452. *
  453. * @return one of {@link Constants#EN_AUTO}, {@link Constants#EN_COLUMN},
  454. * {@link Constants#EN_PAGE}, {@link Constants#EN_EVEN_PAGE}, or
  455. * {@link Constants#EN_ODD_PAGE}
  456. */
  457. public int getBreakBefore() {
  458. return breakBefore;
  459. }
  460. /**
  461. * Sets the value of the break before the current element.
  462. *
  463. * @param breakBefore the value of the break-before
  464. * @see #getBreakBefore()
  465. */
  466. public void setBreakBefore(int breakBefore) {
  467. this.breakBefore = breakBefore;
  468. }
  469. /**
  470. * Returns the value of the break after the element whose
  471. * {@link LayoutManager#getNextKnuthElements(LayoutContext, int)} method has just been
  472. * called.
  473. *
  474. * @return one of {@link Constants#EN_AUTO}, {@link Constants#EN_COLUMN},
  475. * {@link Constants#EN_PAGE}, {@link Constants#EN_EVEN_PAGE}, or
  476. * {@link Constants#EN_ODD_PAGE}
  477. */
  478. public int getBreakAfter() {
  479. return breakAfter;
  480. }
  481. /**
  482. * Sets the value of the break after the current element.
  483. *
  484. * @param breakAfter the value of the break-after
  485. * @see #getBreakAfter()
  486. */
  487. public void setBreakAfter(int breakAfter) {
  488. this.breakAfter = breakAfter;
  489. }
  490. /** {@inheritDoc} */
  491. public String toString() {
  492. return "Layout Context:"
  493. + "\nStack Limit BPD: \t"
  494. + (getStackLimitBP() == null ? "null" : getStackLimitBP().toString())
  495. + "\nStack Limit IPD: \t"
  496. + (getStackLimitIP() == null ? "null" : getStackLimitIP().toString())
  497. + "\nTrailing Space: \t"
  498. + (getTrailingSpace() == null ? "null" : getTrailingSpace().toString())
  499. + "\nLeading Space: \t"
  500. + (getLeadingSpace() == null ? "null" : getLeadingSpace().toString())
  501. + "\nReference IPD: \t" + getRefIPD()
  502. + "\nSpace Adjust: \t" + getSpaceAdjust()
  503. + "\nIPD Adjust: \t" + getIPDAdjust()
  504. + "\nResolve Leading Space: \t" + resolveLeadingSpace()
  505. + "\nSuppress Leading Space: \t" + suppressLeadingSpace()
  506. + "\nIs First Area: \t" + isFirstArea()
  507. + "\nStarts New Area: \t" + startsNewArea()
  508. + "\nIs Last Area: \t" + isLastArea()
  509. + "\nTry Hyphenate: \t" + tryHyphenate()
  510. + "\nKeeps: \t[" + (isKeepWithNextPending() ? "keep-with-next" : "") + "]["
  511. + (isKeepWithPreviousPending() ? "keep-with-previous" : "") + "] pending"
  512. + "\nBreaks: \tforced [" + (breakBefore != Constants.EN_AUTO ? "break-before" : "") + "]["
  513. + (breakAfter != Constants.EN_AUTO ? "break-after" : "") + "]";
  514. }
  515. }