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.

Block.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.fo.flow;
  8. // FOP
  9. import org.apache.fop.fo.*;
  10. import org.apache.fop.fo.properties.*;
  11. import org.apache.fop.layout.*;
  12. import org.apache.fop.datatypes.*;
  13. import org.apache.fop.apps.FOPException;
  14. import org.apache.fop.layoutmgr.LayoutManager;
  15. import org.apache.fop.layoutmgr.BlockLayoutManager;
  16. import org.apache.fop.util.CharUtilities;
  17. import org.apache.fop.apps.StreamRenderer;
  18. import org.xml.sax.Attributes;
  19. import java.util.List;
  20. /*
  21. Modified by Mark Lillywhite mark-fop@inomial.com. The changes
  22. here are based on memory profiling and do not change functionality.
  23. Essentially, the Block object had a pointer to a BlockArea object
  24. that it created. The BlockArea was not referenced after the Block
  25. was finished except to determine the size of the BlockArea, however
  26. a reference to the BlockArea was maintained and this caused a lot of
  27. GC problems, and was a major reason for FOP memory leaks. So,
  28. the reference to BlockArea was made local, the required information
  29. is now stored (instead of a reference to the complex BlockArea object)
  30. and it appears that there are a lot of changes in this file, in fact
  31. there are only a few sematic changes; mostly I just got rid of
  32. "this." from blockArea since BlockArea is now local.
  33. */
  34. public class Block extends FObjMixed {
  35. int align;
  36. int alignLast;
  37. int breakAfter;
  38. int lineHeight;
  39. int startIndent;
  40. int endIndent;
  41. int spaceBefore;
  42. int spaceAfter;
  43. int textIndent;
  44. int keepWithNext;
  45. ColorType backgroundColor;
  46. int blockWidows;
  47. int blockOrphans;
  48. int areaHeight = 0;
  49. int contentWidth = 0;
  50. String id;
  51. int span;
  52. private int wsTreatment; //ENUMERATION
  53. private int lfTreatment; //ENUMERATION
  54. private boolean bWScollapse; //true if white-space-collapse=true
  55. // this may be helpful on other FOs too
  56. boolean anythingLaidOut = false;
  57. /**
  58. * Index of first inline-type FO seen in a sequence.
  59. * Used during FO tree building to do white-space handling.
  60. */
  61. private FONode firstInlineChild = null;
  62. public Block(FONode parent) {
  63. super(parent);
  64. }
  65. public void handleAttrs(Attributes attlist) throws FOPException {
  66. super.handleAttrs(attlist);
  67. this.span = this.properties.get("span").getEnum();
  68. this.wsTreatment =
  69. this.properties.get("white-space-treatment").getEnum();
  70. this.bWScollapse =
  71. (this.properties.get("white-space-collapse").getEnum()
  72. == Constants.TRUE);
  73. this.lfTreatment =
  74. this.properties.get("linefeed-treatment").getEnum();
  75. }
  76. public Status layout(Area area) throws FOPException {
  77. BlockArea blockArea;
  78. // log.error(" b:LAY[" + marker + "] ");
  79. if (this.marker == BREAK_AFTER) {
  80. return new Status(Status.OK);
  81. }
  82. if (this.marker == START) {
  83. // Common Accessibility Properties
  84. AccessibilityProps mAccProps = propMgr.getAccessibilityProps();
  85. // Common Aural Properties
  86. AuralProps mAurProps = propMgr.getAuralProps();
  87. // Common Border, Padding, and Background Properties
  88. BorderAndPadding bap = propMgr.getBorderAndPadding();
  89. BackgroundProps bProps = propMgr.getBackgroundProps();
  90. // Common Font Properties
  91. //this.fontState = propMgr.getFontState(area.getFontInfo());
  92. // Common Hyphenation Properties
  93. HyphenationProps mHyphProps = propMgr.getHyphenationProps();
  94. // Common Margin Properties-Block
  95. MarginProps mProps = propMgr.getMarginProps();
  96. // Common Relative Position Properties
  97. RelativePositionProps mRelProps =
  98. propMgr.getRelativePositionProps();
  99. // this.properties.get("break-after");
  100. // this.properties.get("break-before");
  101. // this.properties.get("color");
  102. // this.properties.get("text-depth");
  103. // this.properties.get("text-altitude");
  104. // this.properties.get("hyphenation-keep");
  105. // this.properties.get("hyphenation-ladder-count");
  106. // this.properties.get("id");
  107. // this.properties.get("keep-together");
  108. // this.properties.get("keep-with-next");
  109. // this.properties.get("keep-with-previous");
  110. // this.properties.get("last-line-end-indent");
  111. // this.properties.get("linefeed-treatment");
  112. // this.properties.get("line-height");
  113. // this.properties.get("line-height-shift-adjustment");
  114. // this.properties.get("line-stacking-strategy");
  115. // this.properties.get("orphans");
  116. // this.properties.get("white-space-treatment");
  117. // this.properties.get("span");
  118. // this.properties.get("text-align");
  119. // this.properties.get("text-align-last");
  120. // this.properties.get("text-indent");
  121. // this.properties.get("visibility");
  122. // this.properties.get("white-space-collapse");
  123. // this.properties.get("widows");
  124. // this.properties.get("wrap-option");
  125. // this.properties.get("z-index");
  126. this.align = this.properties.get("text-align").getEnum();
  127. this.alignLast =
  128. this.properties.get("text-align-last").getEnum();
  129. this.breakAfter = this.properties.get("break-after").getEnum();
  130. this.lineHeight = this.properties.get(
  131. "line-height").getLength().mvalue();
  132. this.startIndent = this.properties.get(
  133. "start-indent").getLength().mvalue();
  134. this.endIndent = this.properties.get(
  135. "end-indent").getLength().mvalue();
  136. this.spaceBefore = this.properties.get(
  137. "space-before.optimum").getLength().mvalue();
  138. this.spaceAfter = this.properties.get(
  139. "space-after.optimum").getLength().mvalue();
  140. this.textIndent = this.properties.get(
  141. "text-indent").getLength().mvalue();
  142. this.keepWithNext =
  143. this.properties.get("keep-with-next").getEnum();
  144. this.backgroundColor = this.properties.get(
  145. "background-color").getColorType();
  146. this.blockWidows =
  147. this.properties.get("widows").getNumber().intValue();
  148. this.blockOrphans =
  149. this.properties.get("orphans").getNumber().intValue();
  150. this.id = this.properties.get("id").getString();
  151. if (area instanceof BlockArea) {
  152. area.end();
  153. }
  154. if (area.getIDReferences() != null)
  155. area.getIDReferences().createID(id);
  156. this.marker = 0;
  157. // no break if first in area tree, or leading in context
  158. // area
  159. int breakBeforeStatus = propMgr.checkBreakBefore(area);
  160. if (breakBeforeStatus != Status.OK) {
  161. return new Status(breakBeforeStatus);
  162. }
  163. int numChildren = this.children.size();
  164. for (int i = 0; i < numChildren; i++) {
  165. FONode fo = (FONode) children.get(i);
  166. if (fo instanceof FOText) {
  167. if (((FOText) fo).willCreateArea()) {
  168. //fo.setWidows(blockWidows);
  169. break;
  170. } else {
  171. children.remove(i);
  172. numChildren = this.children.size();
  173. i--;
  174. }
  175. } else {
  176. //fo.setWidows(blockWidows);
  177. break;
  178. }
  179. }
  180. for (int i = numChildren - 1; i >= 0; i--) {
  181. FONode fo = (FONode) children.get(i);
  182. if (fo instanceof FOText) {
  183. if (((FOText) fo).willCreateArea()) {
  184. //fo.setOrphans(blockOrphans);
  185. break;
  186. }
  187. } else {
  188. //fo.setOrphans(blockOrphans);
  189. break;
  190. }
  191. }
  192. }
  193. if ((spaceBefore != 0) && (this.marker == 0)) {
  194. area.addDisplaySpace(spaceBefore);
  195. }
  196. if (anythingLaidOut) {
  197. this.textIndent = 0;
  198. }
  199. if (marker == 0 && area.getIDReferences() != null) {
  200. area.getIDReferences().configureID(id, area);
  201. }
  202. int spaceLeft = area.spaceLeft();
  203. blockArea = new BlockArea( propMgr.getFontState(area.getFontInfo()),
  204. area.getAllocationWidth(), area.spaceLeft(),
  205. startIndent, endIndent, textIndent, align, alignLast,
  206. lineHeight);
  207. blockArea.setGeneratedBy(this);
  208. this.areasGenerated++;
  209. if (this.areasGenerated == 1)
  210. blockArea.isFirst(true);
  211. // for normal areas this should be the only pair
  212. blockArea.addLineagePair(this, this.areasGenerated);
  213. // markers
  214. //if (this.hasMarkers())
  215. //blockArea.addMarkers(this.getMarkers());
  216. blockArea.setParent(area); // BasicLink needs it
  217. blockArea.setPage(area.getPage());
  218. blockArea.setBackgroundColor(backgroundColor);
  219. blockArea.setBorderAndPadding(propMgr.getBorderAndPadding());
  220. blockArea.setHyphenation(propMgr.getHyphenationProps());
  221. blockArea.start();
  222. blockArea.setAbsoluteHeight(area.getAbsoluteHeight());
  223. blockArea.setIDReferences(area.getIDReferences());
  224. blockArea.setTableCellXOffset(area.getTableCellXOffset());
  225. int numChildren = this.children.size();
  226. for (int i = this.marker; i < numChildren; i++) {
  227. FONode fo = (FONode) children.get(i);
  228. Status status;
  229. if ((status = fo.layout(blockArea)).isIncomplete()) {
  230. this.marker = i;
  231. // this block was modified by
  232. // Hani Elabed 11/27/2000
  233. // if ((i != 0) && (status.getCode() == Status.AREA_FULL_NONE))
  234. // {
  235. // status = new Status(Status.AREA_FULL_SOME);
  236. // }
  237. // new block to replace the one above
  238. // Hani Elabed 11/27/2000
  239. if (status.getCode() == Status.AREA_FULL_NONE) {
  240. // something has already been laid out
  241. if ((i != 0)) {
  242. status = new Status(Status.AREA_FULL_SOME);
  243. area.addChild(blockArea);
  244. area.setMaxHeight(area.getMaxHeight() -
  245. spaceLeft + blockArea.getMaxHeight());
  246. area.increaseHeight(blockArea.getHeight());
  247. area.setAbsoluteHeight(
  248. blockArea.getAbsoluteHeight());
  249. anythingLaidOut = true;
  250. return status;
  251. } else // i == 0 nothing was laid out..
  252. {
  253. anythingLaidOut = false;
  254. return status;
  255. }
  256. }
  257. // blockArea.end();
  258. area.addChild(blockArea);
  259. area.setMaxHeight(area.getMaxHeight() - spaceLeft +
  260. blockArea.getMaxHeight());
  261. area.increaseHeight(blockArea.getHeight());
  262. area.setAbsoluteHeight(blockArea.getAbsoluteHeight());
  263. anythingLaidOut = true;
  264. return status;
  265. }
  266. anythingLaidOut = true;
  267. }
  268. blockArea.end();
  269. area.setMaxHeight(area.getMaxHeight() - spaceLeft +
  270. blockArea.getMaxHeight());
  271. area.addChild(blockArea);
  272. /* should this be combined into above? */
  273. area.increaseHeight(blockArea.getHeight());
  274. area.setAbsoluteHeight(blockArea.getAbsoluteHeight());
  275. if (spaceAfter != 0) {
  276. area.addDisplaySpace(spaceAfter);
  277. }
  278. if (area instanceof BlockArea) {
  279. area.start();
  280. }
  281. // This is not needed any more and it consumes a LOT
  282. // of memory. So we release it for the GC.
  283. areaHeight = blockArea.getHeight();
  284. contentWidth = blockArea.getContentWidth();
  285. // no break if last in area tree, or trailing in context
  286. // area
  287. int breakAfterStatus = propMgr.checkBreakAfter(area);
  288. if (breakAfterStatus != Status.OK) {
  289. this.marker = BREAK_AFTER;
  290. blockArea = null; //Faster GC - BlockArea is big
  291. return new Status(breakAfterStatus);
  292. }
  293. if (keepWithNext != 0) {
  294. blockArea = null; // Faster GC - BlockArea is big
  295. return new Status(Status.KEEP_WITH_NEXT);
  296. }
  297. // log.error(" b:OK" + marker + " ");
  298. blockArea.isLast(true);
  299. blockArea = null; // Faster GC - BlockArea is big
  300. return new Status(Status.OK);
  301. }
  302. public int getAreaHeight() {
  303. return areaHeight;
  304. }
  305. /**
  306. * Return the content width of the boxes generated by this FO.
  307. */
  308. public int getContentWidth() {
  309. return contentWidth; // getAllocationWidth()??
  310. }
  311. public int getSpan() {
  312. return this.span;
  313. }
  314. public void addLayoutManager(List list) {
  315. BlockLayoutManager blm = new BlockLayoutManager(this);
  316. TextInfo ti = propMgr.getTextLayoutProps(fontInfo);
  317. blm.setBlockTextInfo(ti);
  318. list.add(blm);
  319. }
  320. public boolean generatesInlineAreas() {
  321. return false;
  322. }
  323. public void addChild(FONode child) {
  324. // Handle whitespace based on values of properties
  325. // Handle a sequence of inline-producing children in
  326. // one pass
  327. if (((FObj) child).generatesInlineAreas()) {
  328. if (firstInlineChild == null) {
  329. firstInlineChild = child;
  330. }
  331. // lastInlineChild = children.size();
  332. } else {
  333. // Handle whitespace in preceeding inline areas if any
  334. handleWhiteSpace();
  335. }
  336. super.addChild(child);
  337. }
  338. public void end() {
  339. handleWhiteSpace();
  340. }
  341. private void handleWhiteSpace() {
  342. log.debug("fo:block: handleWhiteSpace");
  343. if (firstInlineChild != null) {
  344. boolean bInWS = false;
  345. boolean bPrevWasLF = false;
  346. RecursiveCharIterator charIter =
  347. new RecursiveCharIterator(this, firstInlineChild);
  348. LFchecker lfCheck = new LFchecker(charIter);
  349. while (charIter.hasNext()) {
  350. switch (CharUtilities.classOf(charIter.nextChar())) {
  351. case CharUtilities.XMLWHITESPACE:
  352. /* Some kind of whitespace character, except linefeed. */
  353. boolean bIgnore = false;
  354. switch (wsTreatment) {
  355. case Constants.IGNORE:
  356. bIgnore = true;
  357. break;
  358. case Constants.IGNORE_IF_BEFORE_LINEFEED:
  359. bIgnore = lfCheck.nextIsLF();
  360. break;
  361. case Constants.IGNORE_IF_SURROUNDING_LINEFEED:
  362. bIgnore = (bPrevWasLF ||
  363. lfCheck.nextIsLF());
  364. break;
  365. case Constants.IGNORE_IF_AFTER_LINEFEED:
  366. bIgnore = bPrevWasLF;
  367. break;
  368. }
  369. // Handle ignore
  370. if (bIgnore) {
  371. charIter.remove();
  372. } else if (bWScollapse) {
  373. if (bInWS || (lfTreatment ==
  374. Constants.PRESERVE &&
  375. (bPrevWasLF || lfCheck.nextIsLF()))) {
  376. charIter.remove();
  377. } else {
  378. bInWS = true;
  379. }
  380. }
  381. break;
  382. case CharUtilities.LINEFEED:
  383. /* A linefeed */
  384. lfCheck.reset();
  385. bPrevWasLF = true; // for following whitespace
  386. switch (lfTreatment) {
  387. case Constants.IGNORE:
  388. charIter.remove();
  389. break;
  390. case Constants.TREAT_AS_SPACE:
  391. if (bInWS) {
  392. // only if bWScollapse=true
  393. charIter.remove();
  394. } else {
  395. if (bWScollapse)
  396. bInWS = true;
  397. charIter.replaceChar('\u0020');
  398. }
  399. break;
  400. case Constants.TREAT_AS_ZERO_WIDTH_SPACE:
  401. charIter.replaceChar('\u200b');
  402. // Fall through: this isn't XML whitespace
  403. case Constants.PRESERVE:
  404. bInWS = false;
  405. break;
  406. }
  407. break;
  408. case CharUtilities.EOT:
  409. // A "boundary" objects such as non-character inline
  410. // or nested block object was encountered.
  411. // If any whitespace run in progress, finish it.
  412. // FALL THROUGH
  413. case CharUtilities.UCWHITESPACE: // Non XML-whitespace
  414. case CharUtilities.NONWHITESPACE:
  415. /* Any other character */
  416. bInWS = bPrevWasLF = false;
  417. lfCheck.reset();
  418. break;
  419. }
  420. }
  421. firstInlineChild = null;
  422. }
  423. }
  424. private static class LFchecker {
  425. private boolean bNextIsLF = false;
  426. private RecursiveCharIterator charIter;
  427. LFchecker(RecursiveCharIterator charIter) {
  428. this.charIter = charIter;
  429. }
  430. boolean nextIsLF() {
  431. if (bNextIsLF == false) {
  432. CharIterator lfIter = charIter.mark();
  433. while (lfIter.hasNext()) {
  434. char c = lfIter.nextChar();
  435. if (c == '\n') {
  436. bNextIsLF = true;
  437. break;
  438. } else if (CharUtilities.classOf(c) !=
  439. CharUtilities.XMLWHITESPACE) {
  440. break;
  441. }
  442. }
  443. }
  444. return bNextIsLF;
  445. }
  446. void reset() {
  447. bNextIsLF = false;
  448. }
  449. }
  450. }