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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.fo.flow;
  52. // FOP
  53. import org.apache.fop.apps.FOPException;
  54. import org.apache.fop.datatypes.ColorType;
  55. import org.apache.fop.fo.CharIterator;
  56. import org.apache.fop.fo.FONode;
  57. import org.apache.fop.fo.FObj;
  58. import org.apache.fop.fo.FObjMixed;
  59. import org.apache.fop.fo.RecursiveCharIterator;
  60. import org.apache.fop.fo.TextInfo;
  61. import org.apache.fop.fo.properties.Constants;
  62. import org.apache.fop.layout.AccessibilityProps;
  63. import org.apache.fop.layout.AuralProps;
  64. import org.apache.fop.layout.BackgroundProps;
  65. import org.apache.fop.layout.BorderAndPadding;
  66. import org.apache.fop.layout.HyphenationProps;
  67. import org.apache.fop.layout.MarginProps;
  68. import org.apache.fop.layout.RelativePositionProps;
  69. import org.apache.fop.layoutmgr.BlockLayoutManager;
  70. import org.apache.fop.util.CharUtilities;
  71. import org.xml.sax.Attributes;
  72. import java.util.List;
  73. /*
  74. Modified by Mark Lillywhite mark-fop@inomial.com. The changes
  75. here are based on memory profiling and do not change functionality.
  76. Essentially, the Block object had a pointer to a BlockArea object
  77. that it created. The BlockArea was not referenced after the Block
  78. was finished except to determine the size of the BlockArea, however
  79. a reference to the BlockArea was maintained and this caused a lot of
  80. GC problems, and was a major reason for FOP memory leaks. So,
  81. the reference to BlockArea was made local, the required information
  82. is now stored (instead of a reference to the complex BlockArea object)
  83. and it appears that there are a lot of changes in this file, in fact
  84. there are only a few sematic changes; mostly I just got rid of
  85. "this." from blockArea since BlockArea is now local.
  86. */
  87. public class Block extends FObjMixed {
  88. int align;
  89. int alignLast;
  90. int breakAfter;
  91. int lineHeight;
  92. int startIndent;
  93. int endIndent;
  94. int spaceBefore;
  95. int spaceAfter;
  96. int textIndent;
  97. int keepWithNext;
  98. ColorType backgroundColor;
  99. int blockWidows;
  100. int blockOrphans;
  101. String id;
  102. int span;
  103. private int wsTreatment; //ENUMERATION
  104. private int lfTreatment; //ENUMERATION
  105. private boolean bWScollapse; //true if white-space-collapse=true
  106. // this may be helpful on other FOs too
  107. boolean anythingLaidOut = false;
  108. /**
  109. * Index of first inline-type FO seen in a sequence.
  110. * Used during FO tree building to do white-space handling.
  111. */
  112. private FONode firstInlineChild = null;
  113. public Block(FONode parent) {
  114. super(parent);
  115. }
  116. public void handleAttrs(Attributes attlist) throws FOPException {
  117. super.handleAttrs(attlist);
  118. this.span = this.properties.get("span").getEnum();
  119. this.wsTreatment =
  120. this.properties.get("white-space-treatment").getEnum();
  121. this.bWScollapse =
  122. (this.properties.get("white-space-collapse").getEnum()
  123. == Constants.TRUE);
  124. this.lfTreatment =
  125. this.properties.get("linefeed-treatment").getEnum();
  126. setupID();
  127. structHandler.startBlock(this);
  128. }
  129. public void setup() {
  130. // Common Accessibility Properties
  131. AccessibilityProps mAccProps = propMgr.getAccessibilityProps();
  132. // Common Aural Properties
  133. AuralProps mAurProps = propMgr.getAuralProps();
  134. // Common Border, Padding, and Background Properties
  135. BorderAndPadding bap = propMgr.getBorderAndPadding();
  136. BackgroundProps bProps = propMgr.getBackgroundProps();
  137. // Common Font Properties
  138. //this.fontState = propMgr.getFontState(area.getFontInfo());
  139. // Common Hyphenation Properties
  140. HyphenationProps mHyphProps = propMgr.getHyphenationProps();
  141. // Common Margin Properties-Block
  142. MarginProps mProps = propMgr.getMarginProps();
  143. // Common Relative Position Properties
  144. RelativePositionProps mRelProps =
  145. propMgr.getRelativePositionProps();
  146. // this.properties.get("break-after");
  147. // this.properties.get("break-before");
  148. // this.properties.get("color");
  149. // this.properties.get("text-depth");
  150. // this.properties.get("text-altitude");
  151. // this.properties.get("hyphenation-keep");
  152. // this.properties.get("hyphenation-ladder-count");
  153. setupID();
  154. // this.properties.get("keep-together");
  155. // this.properties.get("keep-with-next");
  156. // this.properties.get("keep-with-previous");
  157. // this.properties.get("last-line-end-indent");
  158. // this.properties.get("linefeed-treatment");
  159. // this.properties.get("line-height");
  160. // this.properties.get("line-height-shift-adjustment");
  161. // this.properties.get("line-stacking-strategy");
  162. // this.properties.get("orphans");
  163. // this.properties.get("white-space-treatment");
  164. // this.properties.get("span");
  165. // this.properties.get("text-align");
  166. // this.properties.get("text-align-last");
  167. // this.properties.get("text-indent");
  168. // this.properties.get("visibility");
  169. // this.properties.get("white-space-collapse");
  170. // this.properties.get("widows");
  171. // this.properties.get("wrap-option");
  172. // this.properties.get("z-index");
  173. this.align = this.properties.get("text-align").getEnum();
  174. this.alignLast =
  175. this.properties.get("text-align-last").getEnum();
  176. this.breakAfter = this.properties.get("break-after").getEnum();
  177. this.lineHeight = this.properties.get(
  178. "line-height").getLength().getValue();
  179. this.startIndent = this.properties.get(
  180. "start-indent").getLength().getValue();
  181. this.endIndent = this.properties.get(
  182. "end-indent").getLength().getValue();
  183. this.spaceBefore = this.properties.get(
  184. "space-before.optimum").getLength().getValue();
  185. this.spaceAfter = this.properties.get(
  186. "space-after.optimum").getLength().getValue();
  187. this.textIndent = this.properties.get(
  188. "text-indent").getLength().getValue();
  189. this.keepWithNext =
  190. this.properties.get("keep-with-next").getEnum();
  191. this.blockWidows =
  192. this.properties.get("widows").getNumber().intValue();
  193. this.blockOrphans =
  194. this.properties.get("orphans").getNumber().intValue();
  195. }
  196. protected boolean containsMarkers() {
  197. return true;
  198. }
  199. public int getSpan() {
  200. return this.span;
  201. }
  202. public void addLayoutManager(List list) {
  203. BlockLayoutManager blm = new BlockLayoutManager();
  204. blm.setUserAgent(getUserAgent());
  205. blm.setFObj(this);
  206. TextInfo ti = propMgr.getTextLayoutProps(fontInfo);
  207. blm.setBlockTextInfo(ti);
  208. list.add(blm);
  209. }
  210. public boolean generatesInlineAreas() {
  211. return false;
  212. }
  213. public void addChild(FONode child) {
  214. // Handle whitespace based on values of properties
  215. // Handle a sequence of inline-producing children in
  216. // one pass
  217. if (child instanceof FObj && ((FObj) child).generatesInlineAreas()) {
  218. if (firstInlineChild == null) {
  219. firstInlineChild = child;
  220. }
  221. // lastInlineChild = children.size();
  222. } else {
  223. // Handle whitespace in preceeding inline areas if any
  224. handleWhiteSpace();
  225. }
  226. super.addChild(child);
  227. }
  228. public void end() {
  229. handleWhiteSpace();
  230. structHandler.endBlock(this);
  231. }
  232. private void handleWhiteSpace() {
  233. //getLogger().debug("fo:block: handleWhiteSpace");
  234. if (firstInlineChild != null) {
  235. boolean bInWS = false;
  236. boolean bPrevWasLF = false;
  237. RecursiveCharIterator charIter =
  238. new RecursiveCharIterator(this, firstInlineChild);
  239. LFchecker lfCheck = new LFchecker(charIter);
  240. while (charIter.hasNext()) {
  241. switch (CharUtilities.classOf(charIter.nextChar())) {
  242. case CharUtilities.XMLWHITESPACE:
  243. /* Some kind of whitespace character, except linefeed. */
  244. boolean bIgnore = false;
  245. switch (wsTreatment) {
  246. case Constants.IGNORE:
  247. bIgnore = true;
  248. break;
  249. case Constants.IGNORE_IF_BEFORE_LINEFEED:
  250. bIgnore = lfCheck.nextIsLF();
  251. break;
  252. case Constants.IGNORE_IF_SURROUNDING_LINEFEED:
  253. bIgnore = (bPrevWasLF
  254. || lfCheck.nextIsLF());
  255. break;
  256. case Constants.IGNORE_IF_AFTER_LINEFEED:
  257. bIgnore = bPrevWasLF;
  258. break;
  259. }
  260. // Handle ignore
  261. if (bIgnore) {
  262. charIter.remove();
  263. } else if (bWScollapse) {
  264. if (bInWS || (lfTreatment == Constants.PRESERVE
  265. && (bPrevWasLF || lfCheck.nextIsLF()))) {
  266. charIter.remove();
  267. } else {
  268. bInWS = true;
  269. }
  270. }
  271. break;
  272. case CharUtilities.LINEFEED:
  273. /* A linefeed */
  274. lfCheck.reset();
  275. bPrevWasLF = true; // for following whitespace
  276. switch (lfTreatment) {
  277. case Constants.IGNORE:
  278. charIter.remove();
  279. break;
  280. case Constants.TREAT_AS_SPACE:
  281. if (bInWS) {
  282. // only if bWScollapse=true
  283. charIter.remove();
  284. } else {
  285. if (bWScollapse) {
  286. bInWS = true;
  287. }
  288. charIter.replaceChar('\u0020');
  289. }
  290. break;
  291. case Constants.TREAT_AS_ZERO_WIDTH_SPACE:
  292. charIter.replaceChar('\u200b');
  293. // Fall through: this isn't XML whitespace
  294. case Constants.PRESERVE:
  295. bInWS = false;
  296. break;
  297. }
  298. break;
  299. case CharUtilities.EOT:
  300. // A "boundary" objects such as non-character inline
  301. // or nested block object was encountered.
  302. // If any whitespace run in progress, finish it.
  303. // FALL THROUGH
  304. case CharUtilities.UCWHITESPACE: // Non XML-whitespace
  305. case CharUtilities.NONWHITESPACE:
  306. /* Any other character */
  307. bInWS = bPrevWasLF = false;
  308. lfCheck.reset();
  309. break;
  310. }
  311. }
  312. firstInlineChild = null;
  313. }
  314. }
  315. private static class LFchecker {
  316. private boolean bNextIsLF = false;
  317. private RecursiveCharIterator charIter;
  318. LFchecker(RecursiveCharIterator charIter) {
  319. this.charIter = charIter;
  320. }
  321. boolean nextIsLF() {
  322. if (bNextIsLF == false) {
  323. CharIterator lfIter = charIter.mark();
  324. while (lfIter.hasNext()) {
  325. char c = lfIter.nextChar();
  326. if (c == '\n') {
  327. bNextIsLF = true;
  328. break;
  329. } else if (CharUtilities.classOf(c)
  330. != CharUtilities.XMLWHITESPACE) {
  331. break;
  332. }
  333. }
  334. }
  335. return bNextIsLF;
  336. }
  337. void reset() {
  338. bNextIsLF = false;
  339. }
  340. }
  341. }