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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001-2002 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.xml.sax.Attributes;
  18. import java.util.List;
  19. /*
  20. Modified by Mark Lillywhite mark-fop@inomial.com. The changes
  21. here are based on memory profiling and do not change functionality.
  22. Essentially, the Block object had a pointer to a BlockArea object
  23. that it created. The BlockArea was not referenced after the Block
  24. was finished except to determine the size of the BlockArea, however
  25. a reference to the BlockArea was maintained and this caused a lot of
  26. GC problems, and was a major reason for FOP memory leaks. So,
  27. the reference to BlockArea was made local, the required information
  28. is now stored (instead of a reference to the complex BlockArea object)
  29. and it appears that there are a lot of changes in this file, in fact
  30. there are only a few sematic changes; mostly I just got rid of
  31. "this." from blockArea since BlockArea is now local.
  32. */
  33. public class Block extends FObjMixed {
  34. int align;
  35. int alignLast;
  36. int breakAfter;
  37. int lineHeight;
  38. int startIndent;
  39. int endIndent;
  40. int spaceBefore;
  41. int spaceAfter;
  42. int textIndent;
  43. int keepWithNext;
  44. ColorType backgroundColor;
  45. int blockWidows;
  46. int blockOrphans;
  47. int areaHeight = 0;
  48. int contentWidth = 0;
  49. String id;
  50. int span;
  51. private int wsTreatment; //ENUMERATION
  52. private int lfTreatment; //ENUMERATION
  53. private boolean bWScollapse; //true if white-space-collapse=true
  54. // this may be helpful on other FOs too
  55. boolean anythingLaidOut = false;
  56. /**
  57. * Index of first inline-type FO seen in a sequence.
  58. * Used during FO tree building to do white-space handling.
  59. */
  60. private FONode firstInlineChild = null;
  61. public Block(FONode parent) {
  62. super(parent);
  63. }
  64. public void handleAttrs(Attributes attlist) throws FOPException {
  65. super.handleAttrs(attlist);
  66. this.span = this.properties.get("span").getEnum();
  67. this.wsTreatment =
  68. this.properties.get("white-space-treatment").getEnum();
  69. this.bWScollapse =
  70. (this.properties.get("white-space-collapse").getEnum()
  71. == Constants.TRUE);
  72. this.lfTreatment =
  73. this.properties.get("linefeed-treatment").getEnum();
  74. setupID();
  75. structHandler.startBlock(this);
  76. }
  77. public void setup() {
  78. // Common Accessibility Properties
  79. AccessibilityProps mAccProps = propMgr.getAccessibilityProps();
  80. // Common Aural Properties
  81. AuralProps mAurProps = propMgr.getAuralProps();
  82. // Common Border, Padding, and Background Properties
  83. BorderAndPadding bap = propMgr.getBorderAndPadding();
  84. BackgroundProps bProps = propMgr.getBackgroundProps();
  85. // Common Font Properties
  86. //this.fontState = propMgr.getFontState(area.getFontInfo());
  87. // Common Hyphenation Properties
  88. HyphenationProps mHyphProps = propMgr.getHyphenationProps();
  89. // Common Margin Properties-Block
  90. MarginProps mProps = propMgr.getMarginProps();
  91. // Common Relative Position Properties
  92. RelativePositionProps mRelProps =
  93. propMgr.getRelativePositionProps();
  94. // this.properties.get("break-after");
  95. // this.properties.get("break-before");
  96. // this.properties.get("color");
  97. // this.properties.get("text-depth");
  98. // this.properties.get("text-altitude");
  99. // this.properties.get("hyphenation-keep");
  100. // this.properties.get("hyphenation-ladder-count");
  101. setupID();
  102. // this.properties.get("keep-together");
  103. // this.properties.get("keep-with-next");
  104. // this.properties.get("keep-with-previous");
  105. // this.properties.get("last-line-end-indent");
  106. // this.properties.get("linefeed-treatment");
  107. // this.properties.get("line-height");
  108. // this.properties.get("line-height-shift-adjustment");
  109. // this.properties.get("line-stacking-strategy");
  110. // this.properties.get("orphans");
  111. // this.properties.get("white-space-treatment");
  112. // this.properties.get("span");
  113. // this.properties.get("text-align");
  114. // this.properties.get("text-align-last");
  115. // this.properties.get("text-indent");
  116. // this.properties.get("visibility");
  117. // this.properties.get("white-space-collapse");
  118. // this.properties.get("widows");
  119. // this.properties.get("wrap-option");
  120. // this.properties.get("z-index");
  121. this.align = this.properties.get("text-align").getEnum();
  122. this.alignLast =
  123. this.properties.get("text-align-last").getEnum();
  124. this.breakAfter = this.properties.get("break-after").getEnum();
  125. this.lineHeight = this.properties.get(
  126. "line-height").getLength().mvalue();
  127. this.startIndent = this.properties.get(
  128. "start-indent").getLength().mvalue();
  129. this.endIndent = this.properties.get(
  130. "end-indent").getLength().mvalue();
  131. this.spaceBefore = this.properties.get(
  132. "space-before.optimum").getLength().mvalue();
  133. this.spaceAfter = this.properties.get(
  134. "space-after.optimum").getLength().mvalue();
  135. this.textIndent = this.properties.get(
  136. "text-indent").getLength().mvalue();
  137. this.keepWithNext =
  138. this.properties.get("keep-with-next").getEnum();
  139. this.backgroundColor = this.properties.get(
  140. "background-color").getColorType();
  141. this.blockWidows =
  142. this.properties.get("widows").getNumber().intValue();
  143. this.blockOrphans =
  144. this.properties.get("orphans").getNumber().intValue();
  145. }
  146. public int getAreaHeight() {
  147. return areaHeight;
  148. }
  149. /**
  150. * Return the content width of the boxes generated by this FO.
  151. */
  152. public int getContentWidth() {
  153. return contentWidth; // getAllocationWidth()??
  154. }
  155. public int getSpan() {
  156. return this.span;
  157. }
  158. public void addLayoutManager(List list) {
  159. BlockLayoutManager blm = new BlockLayoutManager(this);
  160. TextInfo ti = propMgr.getTextLayoutProps(fontInfo);
  161. blm.setBlockTextInfo(ti);
  162. list.add(blm);
  163. }
  164. public boolean generatesInlineAreas() {
  165. return false;
  166. }
  167. public void addChild(FONode child) {
  168. // Handle whitespace based on values of properties
  169. // Handle a sequence of inline-producing children in
  170. // one pass
  171. if (child instanceof FObj && ((FObj) child).generatesInlineAreas()) {
  172. if (firstInlineChild == null) {
  173. firstInlineChild = child;
  174. }
  175. // lastInlineChild = children.size();
  176. } else {
  177. // Handle whitespace in preceeding inline areas if any
  178. handleWhiteSpace();
  179. }
  180. super.addChild(child);
  181. }
  182. public void end() {
  183. handleWhiteSpace();
  184. structHandler.endBlock(this);
  185. }
  186. private void handleWhiteSpace() {
  187. //getLogger().debug("fo:block: handleWhiteSpace");
  188. if (firstInlineChild != null) {
  189. boolean bInWS = false;
  190. boolean bPrevWasLF = false;
  191. RecursiveCharIterator charIter =
  192. new RecursiveCharIterator(this, firstInlineChild);
  193. LFchecker lfCheck = new LFchecker(charIter);
  194. while (charIter.hasNext()) {
  195. switch (CharUtilities.classOf(charIter.nextChar())) {
  196. case CharUtilities.XMLWHITESPACE:
  197. /* Some kind of whitespace character, except linefeed. */
  198. boolean bIgnore = false;
  199. switch (wsTreatment) {
  200. case Constants.IGNORE:
  201. bIgnore = true;
  202. break;
  203. case Constants.IGNORE_IF_BEFORE_LINEFEED:
  204. bIgnore = lfCheck.nextIsLF();
  205. break;
  206. case Constants.IGNORE_IF_SURROUNDING_LINEFEED:
  207. bIgnore = (bPrevWasLF ||
  208. lfCheck.nextIsLF());
  209. break;
  210. case Constants.IGNORE_IF_AFTER_LINEFEED:
  211. bIgnore = bPrevWasLF;
  212. break;
  213. }
  214. // Handle ignore
  215. if (bIgnore) {
  216. charIter.remove();
  217. } else if (bWScollapse) {
  218. if (bInWS || (lfTreatment ==
  219. Constants.PRESERVE &&
  220. (bPrevWasLF || lfCheck.nextIsLF()))) {
  221. charIter.remove();
  222. } else {
  223. bInWS = true;
  224. }
  225. }
  226. break;
  227. case CharUtilities.LINEFEED:
  228. /* A linefeed */
  229. lfCheck.reset();
  230. bPrevWasLF = true; // for following whitespace
  231. switch (lfTreatment) {
  232. case Constants.IGNORE:
  233. charIter.remove();
  234. break;
  235. case Constants.TREAT_AS_SPACE:
  236. if (bInWS) {
  237. // only if bWScollapse=true
  238. charIter.remove();
  239. } else {
  240. if (bWScollapse) {
  241. bInWS = true;
  242. }
  243. charIter.replaceChar('\u0020');
  244. }
  245. break;
  246. case Constants.TREAT_AS_ZERO_WIDTH_SPACE:
  247. charIter.replaceChar('\u200b');
  248. // Fall through: this isn't XML whitespace
  249. case Constants.PRESERVE:
  250. bInWS = false;
  251. break;
  252. }
  253. break;
  254. case CharUtilities.EOT:
  255. // A "boundary" objects such as non-character inline
  256. // or nested block object was encountered.
  257. // If any whitespace run in progress, finish it.
  258. // FALL THROUGH
  259. case CharUtilities.UCWHITESPACE: // Non XML-whitespace
  260. case CharUtilities.NONWHITESPACE:
  261. /* Any other character */
  262. bInWS = bPrevWasLF = false;
  263. lfCheck.reset();
  264. break;
  265. }
  266. }
  267. firstInlineChild = null;
  268. }
  269. }
  270. private static class LFchecker {
  271. private boolean bNextIsLF = false;
  272. private RecursiveCharIterator charIter;
  273. LFchecker(RecursiveCharIterator charIter) {
  274. this.charIter = charIter;
  275. }
  276. boolean nextIsLF() {
  277. if (bNextIsLF == false) {
  278. CharIterator lfIter = charIter.mark();
  279. while (lfIter.hasNext()) {
  280. char c = lfIter.nextChar();
  281. if (c == '\n') {
  282. bNextIsLF = true;
  283. break;
  284. } else if (CharUtilities.classOf(c) !=
  285. CharUtilities.XMLWHITESPACE) {
  286. break;
  287. }
  288. }
  289. }
  290. return bNextIsLF;
  291. }
  292. void reset() {
  293. bNextIsLF = false;
  294. }
  295. }
  296. }