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.

Area.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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.area;
  19. import java.io.Serializable;
  20. import java.util.Map;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.traits.BorderProps;
  24. // If the area appears more than once in the output
  25. // or if the area has external data it is cached
  26. // to keep track of it and to minimize rendered output
  27. // renderers can render the output once and display it
  28. // for every occurence
  29. // this should also extend to all outputs (including PDFGraphics2D)
  30. // and all types of renderers
  31. /**
  32. * Base object for all areas.
  33. */
  34. public class Area extends AreaTreeObject implements Serializable {
  35. // stacking directions
  36. /**
  37. * Stacking left to right
  38. */
  39. public static final int LR = 0;
  40. /**
  41. * Stacking right to left
  42. */
  43. public static final int RL = 1;
  44. /**
  45. * Stacking top to bottom
  46. */
  47. public static final int TB = 2;
  48. /**
  49. * Stacking bottom to top
  50. */
  51. public static final int BT = 3;
  52. // orientations for reference areas
  53. /**
  54. * Normal orientation
  55. */
  56. public static final int ORIENT_0 = 0;
  57. /**
  58. * Rotated 90 degrees clockwise
  59. */
  60. public static final int ORIENT_90 = 1;
  61. /**
  62. * Rotate 180 degrees
  63. */
  64. public static final int ORIENT_180 = 2;
  65. /**
  66. * Rotated 270 degrees clockwise
  67. */
  68. public static final int ORIENT_270 = 3;
  69. // area class values
  70. /**
  71. * Normal class
  72. */
  73. public static final int CLASS_NORMAL = 0;
  74. /**
  75. * Fixed position class
  76. */
  77. public static final int CLASS_FIXED = 1;
  78. /**
  79. * Absolute position class
  80. */
  81. public static final int CLASS_ABSOLUTE = 2;
  82. /**
  83. * Before float class
  84. */
  85. public static final int CLASS_BEFORE_FLOAT = 3;
  86. /**
  87. * Footnote class
  88. */
  89. public static final int CLASS_FOOTNOTE = 4;
  90. /**
  91. * Side float class
  92. */
  93. public static final int CLASS_SIDE_FLOAT = 5;
  94. // IMPORTANT: make sure this is the maximum + 1
  95. /**
  96. * Maximum class count
  97. */
  98. public static final int CLASS_MAX = CLASS_SIDE_FLOAT + 1;
  99. private int areaClass = CLASS_NORMAL;
  100. /** the area's inline-progression-dimension */
  101. protected int ipd;
  102. /** the area's block-progression-dimension */
  103. protected int bpd;
  104. /**
  105. * Traits for this area stored in a HashMap
  106. */
  107. protected Map props = null;
  108. /**
  109. * logging instance
  110. */
  111. protected static Log log = LogFactory.getLog(Area.class);
  112. /**
  113. * Get the area class of this area.
  114. *
  115. * @return the area class
  116. */
  117. public int getAreaClass() {
  118. return areaClass;
  119. }
  120. /**
  121. * Set the area class of this area.
  122. *
  123. * @param areaClass the area class
  124. */
  125. public void setAreaClass(int areaClass) {
  126. this.areaClass = areaClass;
  127. }
  128. /**
  129. * Set the inline progression dimension of content rectangle
  130. * for this area.
  131. *
  132. * @param i the new inline progression dimension
  133. * @see <a href="http://www.w3.org/TR/xsl/slice4.html#area-common">ipd</a>
  134. */
  135. public void setIPD(int i) {
  136. ipd = i;
  137. }
  138. /**
  139. * Get the inline progression dimension of the content rectangle
  140. * for this area.
  141. *
  142. * @return the inline progression dimension
  143. * @see <a href="http://www.w3.org/TR/xsl/slice4.html#area-common">ipd</a>
  144. */
  145. public int getIPD() {
  146. return ipd;
  147. }
  148. /**
  149. * Set the block progression dimension of the content rectangle
  150. * for this area.
  151. *
  152. * @param b the new block progression dimension
  153. * @see <a href="http://www.w3.org/TR/xsl/slice4.html#area-common">bpd</a>
  154. */
  155. public void setBPD(int b) {
  156. bpd = b;
  157. }
  158. /**
  159. * Get the block progression dimension of the content rectangle
  160. * for this area.
  161. *
  162. * @return the block progression dimension
  163. * @see <a href="http://www.w3.org/TR/xsl/slice4.html#area-common">bpd</a>
  164. */
  165. public int getBPD() {
  166. return bpd;
  167. }
  168. /**
  169. * Get the allocation inline progression dimension of this area.
  170. * This adds the content, borders and the padding to find the
  171. * total allocated IPD.
  172. *
  173. * @return the total IPD allocation for this area
  174. */
  175. public int getAllocIPD() {
  176. return getBorderAndPaddingWidthStart() + getIPD() + getBorderAndPaddingWidthEnd();
  177. }
  178. /**
  179. * Get the allocation block progression dimension of this area.
  180. * This adds the content, borders, padding and spaces to find the
  181. * total allocated BPD.
  182. *
  183. * @return the total BPD allocation for this area
  184. */
  185. public int getAllocBPD() {
  186. return getSpaceBefore() + getBorderAndPaddingWidthBefore() + getBPD()
  187. + getBorderAndPaddingWidthAfter() + getSpaceAfter();
  188. }
  189. /**
  190. * Return the sum of region border- and padding-before
  191. *
  192. * @return width in millipoints
  193. */
  194. public int getBorderAndPaddingWidthBefore() {
  195. int margin = 0;
  196. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_BEFORE);
  197. if (bps != null) {
  198. margin = bps.width;
  199. }
  200. Integer padWidth = (Integer) getTrait(Trait.PADDING_BEFORE);
  201. if (padWidth != null) {
  202. margin += padWidth.intValue();
  203. }
  204. return margin;
  205. }
  206. /**
  207. * Return the sum of region border- and padding-after
  208. *
  209. * @return width in millipoints
  210. */
  211. public int getBorderAndPaddingWidthAfter() {
  212. int margin = 0;
  213. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_AFTER);
  214. if (bps != null) {
  215. margin = bps.width;
  216. }
  217. Integer padWidth = (Integer) getTrait(Trait.PADDING_AFTER);
  218. if (padWidth != null) {
  219. margin += padWidth.intValue();
  220. }
  221. return margin;
  222. }
  223. /**
  224. * Return the sum of region border- and padding-start
  225. *
  226. * @return width in millipoints
  227. */
  228. public int getBorderAndPaddingWidthStart() {
  229. int margin = 0;
  230. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_START);
  231. if (bps != null) {
  232. margin = bps.width;
  233. }
  234. Integer padWidth = (Integer) getTrait(Trait.PADDING_START);
  235. if (padWidth != null) {
  236. margin += padWidth.intValue();
  237. }
  238. return margin;
  239. }
  240. /**
  241. * Return the sum of region border- and padding-end
  242. *
  243. * @return width in millipoints
  244. */
  245. public int getBorderAndPaddingWidthEnd() {
  246. int margin = 0;
  247. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_END);
  248. if (bps != null) {
  249. margin = bps.width;
  250. }
  251. Integer padWidth = (Integer) getTrait(Trait.PADDING_END);
  252. if (padWidth != null) {
  253. margin += padWidth.intValue();
  254. }
  255. return margin;
  256. }
  257. /**
  258. * Returns the space before
  259. *
  260. * @return width in millipoints
  261. */
  262. public int getSpaceBefore() {
  263. int margin = 0;
  264. Integer space = (Integer) getTrait(Trait.SPACE_BEFORE);
  265. if (space != null) {
  266. margin = space.intValue();
  267. }
  268. return margin;
  269. }
  270. /**
  271. * Returns the space after
  272. *
  273. * @return width in millipoints
  274. */
  275. public int getSpaceAfter() {
  276. int margin = 0;
  277. Integer space = (Integer) getTrait(Trait.SPACE_AFTER);
  278. if (space != null) {
  279. margin = space.intValue();
  280. }
  281. return margin;
  282. }
  283. /**
  284. * Returns the space start
  285. *
  286. * @return width in millipoints
  287. */
  288. public int getSpaceStart() {
  289. int margin = 0;
  290. Integer space = (Integer) getTrait(Trait.SPACE_START);
  291. if (space != null) {
  292. margin = space.intValue();
  293. }
  294. return margin;
  295. }
  296. /**
  297. * Returns the space end
  298. *
  299. * @return width in millipoints
  300. */
  301. public int getSpaceEnd() {
  302. int margin = 0;
  303. Integer space = (Integer) getTrait(Trait.SPACE_END);
  304. if (space != null) {
  305. margin = space.intValue();
  306. }
  307. return margin;
  308. }
  309. /**
  310. * Add a child to this area.
  311. * The default is to do nothing. Subclasses must override
  312. * to do something if they can have child areas.
  313. *
  314. * @param child the child area to add
  315. */
  316. public void addChildArea(Area child) {
  317. }
  318. /**
  319. * Add a trait to this area.
  320. *
  321. * @param traitCode the trait key
  322. * @param prop the value of the trait
  323. */
  324. public void addTrait(Object traitCode, Object prop) {
  325. if (props == null) {
  326. props = new java.util.HashMap(20);
  327. }
  328. props.put(traitCode, prop);
  329. }
  330. /**
  331. * Get the map of all traits on this area.
  332. *
  333. * @return the map of traits
  334. */
  335. public Map getTraits() {
  336. return this.props;
  337. }
  338. /** @return true if the area has traits */
  339. public boolean hasTraits() {
  340. return (this.props != null);
  341. }
  342. /**
  343. * Get a trait from this area.
  344. *
  345. * @param oTraitCode the trait key
  346. * @return the trait value
  347. */
  348. public Object getTrait(Object oTraitCode) {
  349. return (props != null ? props.get(oTraitCode) : null);
  350. }
  351. /**
  352. * Checks whether a certain trait is set on this area.
  353. * @param oTraitCode the trait key
  354. * @return true if the trait is set
  355. */
  356. public boolean hasTrait(Object oTraitCode) {
  357. return (getTrait(oTraitCode) != null);
  358. }
  359. /**
  360. * Get a boolean trait from this area.
  361. * @param oTraitCode the trait key
  362. * @return the trait value
  363. */
  364. public boolean getTraitAsBoolean(Object oTraitCode) {
  365. return Boolean.TRUE.equals(getTrait(oTraitCode));
  366. }
  367. /**
  368. * Get a trait from this area as an integer.
  369. *
  370. * @param oTraitCode the trait key
  371. * @return the trait value
  372. */
  373. public int getTraitAsInteger(Object oTraitCode) {
  374. final Object obj = getTrait(oTraitCode);
  375. if (obj instanceof Integer) {
  376. return ((Integer)obj).intValue();
  377. } else {
  378. throw new IllegalArgumentException("Trait "
  379. + oTraitCode.getClass().getName()
  380. + " could not be converted to an integer");
  381. }
  382. }
  383. /**
  384. * {@inheritDoc}
  385. * @return ipd and bpd of area
  386. * */
  387. public String toString() {
  388. StringBuffer sb = new StringBuffer(super.toString());
  389. sb.append(" {ipd=").append(Integer.toString(getIPD()));
  390. sb.append(", bpd=").append(Integer.toString(getBPD()));
  391. sb.append("}");
  392. return sb.toString();
  393. }
  394. }