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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * Copyright 1999-2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.area;
  18. import java.io.Serializable;
  19. import java.util.Collections;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.datatypes.ColorType;
  25. import org.apache.fop.traits.BorderProps;
  26. import org.apache.fop.util.QName;
  27. // If the area appears more than once in the output
  28. // or if the area has external data it is cached
  29. // to keep track of it and to minimize rendered output
  30. // renderers can render the output once and display it
  31. // for every occurence
  32. // this should also extend to all outputs (including PDFGraphics2D)
  33. // and all types of renderers
  34. /**
  35. * Base object for all areas.
  36. */
  37. public class Area implements Serializable {
  38. // stacking directions
  39. /**
  40. * Stacking left to right
  41. */
  42. public static final int LR = 0;
  43. /**
  44. * Stacking right to left
  45. */
  46. public static final int RL = 1;
  47. /**
  48. * Stacking top to bottom
  49. */
  50. public static final int TB = 2;
  51. /**
  52. * Stacking bottom to top
  53. */
  54. public static final int BT = 3;
  55. // orientations for reference areas
  56. /**
  57. * Normal orientation
  58. */
  59. public static final int ORIENT_0 = 0;
  60. /**
  61. * Rotated 90 degrees clockwise
  62. */
  63. public static final int ORIENT_90 = 1;
  64. /**
  65. * Rotate 180 degrees
  66. */
  67. public static final int ORIENT_180 = 2;
  68. /**
  69. * Rotated 270 degrees clockwise
  70. */
  71. public static final int ORIENT_270 = 3;
  72. // area class values
  73. /**
  74. * Normal class
  75. */
  76. public static final int CLASS_NORMAL = 0;
  77. /**
  78. * Fixed position class
  79. */
  80. public static final int CLASS_FIXED = 1;
  81. /**
  82. * Absolute position class
  83. */
  84. public static final int CLASS_ABSOLUTE = 2;
  85. /**
  86. * Before float class
  87. */
  88. public static final int CLASS_BEFORE_FLOAT = 3;
  89. /**
  90. * Footnote class
  91. */
  92. public static final int CLASS_FOOTNOTE = 4;
  93. /**
  94. * Side float class
  95. */
  96. public static final int CLASS_SIDE_FLOAT = 5;
  97. // IMPORTANT: make sure this is the maximum + 1
  98. /**
  99. * Maximum class count
  100. */
  101. public static final int CLASS_MAX = CLASS_SIDE_FLOAT + 1;
  102. private int areaClass = CLASS_NORMAL;
  103. /** the area's inline-progression-dimension */
  104. protected int ipd;
  105. /** the area's block-progression-dimension */
  106. protected int bpd;
  107. /**
  108. * Traits for this area stored in a HashMap
  109. */
  110. protected Map props = null;
  111. /** Foreign attributes */
  112. protected Map foreignAttributes = null;
  113. /**
  114. * logging instance
  115. */
  116. protected static Log log = LogFactory.getLog(Area.class);
  117. /**
  118. * Get the area class of this area.
  119. *
  120. * @return the area class
  121. */
  122. public int getAreaClass() {
  123. return areaClass;
  124. }
  125. /**
  126. * Set the area class of this area.
  127. *
  128. * @param areaClass the area class
  129. */
  130. public void setAreaClass(int areaClass) {
  131. this.areaClass = areaClass;
  132. }
  133. /**
  134. * Set the inline progression dimension of content rectangle
  135. * for this area.
  136. *
  137. * @param i the new inline progression dimension
  138. * @see <a href="http://www.w3.org/TR/xsl/slice4.html#area-common">ipd</a>
  139. */
  140. public void setIPD(int i) {
  141. ipd = i;
  142. }
  143. /**
  144. * Get the inline progression dimension of the content rectangle
  145. * for this area.
  146. *
  147. * @return the inline progression dimension
  148. * @see <a href="http://www.w3.org/TR/xsl/slice4.html#area-common">ipd</a>
  149. */
  150. public int getIPD() {
  151. return ipd;
  152. }
  153. /**
  154. * Set the block progression dimension of the content rectangle
  155. * for this area.
  156. *
  157. * @param b the new block progression dimension
  158. * @see <a href="http://www.w3.org/TR/xsl/slice4.html#area-common">bpd</a>
  159. */
  160. public void setBPD(int b) {
  161. bpd = b;
  162. }
  163. /**
  164. * Get the block progression dimension of the content rectangle
  165. * for this area.
  166. *
  167. * @return the block progression dimension
  168. * @see <a href="http://www.w3.org/TR/xsl/slice4.html#area-common">bpd</a>
  169. */
  170. public int getBPD() {
  171. return bpd;
  172. }
  173. /**
  174. * Get the allocation inline progression dimension of this area.
  175. * This adds the content, borders and the padding to find the
  176. * total allocated IPD.
  177. *
  178. * @return the total IPD allocation for this area
  179. */
  180. public int getAllocIPD() {
  181. return getBorderAndPaddingWidthStart() + getIPD() + getBorderAndPaddingWidthEnd();
  182. }
  183. /**
  184. * Get the allocation block progression dimension of this area.
  185. * This adds the content, borders, padding and spaces to find the
  186. * total allocated BPD.
  187. *
  188. * @return the total BPD allocation for this area
  189. */
  190. public int getAllocBPD() {
  191. return getSpaceBefore() + getBorderAndPaddingWidthBefore() + getBPD()
  192. + getBorderAndPaddingWidthAfter() + getSpaceAfter();
  193. }
  194. /**
  195. * Return the sum of region border- and padding-before
  196. *
  197. * @return width in millipoints
  198. */
  199. public int getBorderAndPaddingWidthBefore() {
  200. int margin = 0;
  201. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_BEFORE);
  202. if (bps != null) {
  203. margin = bps.width;
  204. }
  205. Integer padWidth = (Integer) getTrait(Trait.PADDING_BEFORE);
  206. if (padWidth != null) {
  207. margin += padWidth.intValue();
  208. }
  209. return margin;
  210. }
  211. /**
  212. * Return the sum of region border- and padding-after
  213. *
  214. * @return width in millipoints
  215. */
  216. public int getBorderAndPaddingWidthAfter() {
  217. int margin = 0;
  218. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_AFTER);
  219. if (bps != null) {
  220. margin = bps.width;
  221. }
  222. Integer padWidth = (Integer) getTrait(Trait.PADDING_AFTER);
  223. if (padWidth != null) {
  224. margin += padWidth.intValue();
  225. }
  226. return margin;
  227. }
  228. /**
  229. * Return the sum of region border- and padding-start
  230. *
  231. * @return width in millipoints
  232. */
  233. public int getBorderAndPaddingWidthStart() {
  234. int margin = 0;
  235. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_START);
  236. if (bps != null) {
  237. margin = bps.width;
  238. }
  239. Integer padWidth = (Integer) getTrait(Trait.PADDING_START);
  240. if (padWidth != null) {
  241. margin += padWidth.intValue();
  242. }
  243. return margin;
  244. }
  245. /**
  246. * Return the sum of region border- and padding-end
  247. *
  248. * @return width in millipoints
  249. */
  250. public int getBorderAndPaddingWidthEnd() {
  251. int margin = 0;
  252. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_END);
  253. if (bps != null) {
  254. margin = bps.width;
  255. }
  256. Integer padWidth = (Integer) getTrait(Trait.PADDING_END);
  257. if (padWidth != null) {
  258. margin += padWidth.intValue();
  259. }
  260. return margin;
  261. }
  262. /**
  263. * Returns the space before
  264. *
  265. * @return width in millipoints
  266. */
  267. public int getSpaceBefore() {
  268. int margin = 0;
  269. Integer space = (Integer) getTrait(Trait.SPACE_BEFORE);
  270. if (space != null) {
  271. margin = space.intValue();
  272. }
  273. return margin;
  274. }
  275. /**
  276. * Returns the space after
  277. *
  278. * @return width in millipoints
  279. */
  280. public int getSpaceAfter() {
  281. int margin = 0;
  282. Integer space = (Integer) getTrait(Trait.SPACE_AFTER);
  283. if (space != null) {
  284. margin = space.intValue();
  285. }
  286. return margin;
  287. }
  288. /**
  289. * Returns the space start
  290. *
  291. * @return width in millipoints
  292. */
  293. public int getSpaceStart() {
  294. int margin = 0;
  295. Integer space = (Integer) getTrait(Trait.SPACE_START);
  296. if (space != null) {
  297. margin = space.intValue();
  298. }
  299. return margin;
  300. }
  301. /**
  302. * Returns the space end
  303. *
  304. * @return width in millipoints
  305. */
  306. public int getSpaceEnd() {
  307. int margin = 0;
  308. Integer space = (Integer) getTrait(Trait.SPACE_END);
  309. if (space != null) {
  310. margin = space.intValue();
  311. }
  312. return margin;
  313. }
  314. /**
  315. * Add a child to this area.
  316. * The default is to do nothing. Subclasses must override
  317. * to do something if they can have child areas.
  318. *
  319. * @param child the child area to add
  320. */
  321. public void addChildArea(Area child) {
  322. }
  323. /**
  324. * Add a trait property to this area.
  325. *
  326. * @param prop the Trait to add
  327. */
  328. public void addTrait(Trait prop) {
  329. if (props == null) {
  330. props = new java.util.HashMap(20);
  331. }
  332. props.put(prop.getPropType(), prop.getData());
  333. }
  334. /**
  335. * Add a trait to this area.
  336. *
  337. * @param traitCode the trait key
  338. * @param prop the value of the trait
  339. */
  340. public void addTrait(Object traitCode, Object prop) {
  341. if (props == null) {
  342. props = new java.util.HashMap(20);
  343. }
  344. if (prop instanceof ColorType) {
  345. props.put(traitCode, Trait.Color.makeSerializable((ColorType)prop));
  346. } else {
  347. props.put(traitCode, prop);
  348. }
  349. }
  350. /**
  351. * Get the map of all traits on this area.
  352. *
  353. * @return the map of traits
  354. */
  355. public Map getTraits() {
  356. return this.props;
  357. }
  358. /** @return true if the area has traits */
  359. public boolean hasTraits() {
  360. return (this.props != null);
  361. }
  362. /**
  363. * Get a trait from this area.
  364. *
  365. * @param oTraitCode the trait key
  366. * @return the trait value
  367. */
  368. public Object getTrait(Object oTraitCode) {
  369. return (props != null ? props.get(oTraitCode) : null);
  370. }
  371. /**
  372. * Checks whether a certain trait is set on this area.
  373. * @param oTraitCode the trait key
  374. * @return true if the trait is set
  375. */
  376. public boolean hasTrait(Object oTraitCode) {
  377. return (getTrait(oTraitCode) != null);
  378. }
  379. /**
  380. * Get a boolean trait from this area.
  381. * @param oTraitCode the trait key
  382. * @return the trait value
  383. */
  384. public boolean getBooleanTrait(Object oTraitCode) {
  385. final Object obj = getTrait(oTraitCode);
  386. if (obj instanceof Boolean) {
  387. return ((Boolean)obj).booleanValue();
  388. } else {
  389. return false;
  390. }
  391. }
  392. /**
  393. * Get a trait from this area as an integer.
  394. *
  395. * @param oTraitCode the trait key
  396. * @return the trait value
  397. */
  398. public int getTraitAsInteger(Object oTraitCode) {
  399. final Object obj = getTrait(oTraitCode);
  400. if (obj instanceof Integer) {
  401. return ((Integer)obj).intValue();
  402. } else {
  403. throw new IllegalArgumentException("Trait "
  404. + oTraitCode.getClass().getName()
  405. + " could not be converted to an integer");
  406. }
  407. }
  408. /**
  409. * Sets a foreign attribute.
  410. * @param name the qualified name of the attribute
  411. * @param value the attribute value
  412. */
  413. public void setForeignAttribute(QName name, String value) {
  414. if (this.foreignAttributes == null) {
  415. this.foreignAttributes = new java.util.HashMap();
  416. }
  417. this.foreignAttributes.put(name, value);
  418. }
  419. /**
  420. * Set foreign attributes from a Map.
  421. * @param atts a Map with attributes (keys: QName, values: String)
  422. */
  423. public void setForeignAttributes(Map atts) {
  424. if (atts.size() == 0) {
  425. return;
  426. }
  427. Iterator iter = atts.keySet().iterator();
  428. while (iter.hasNext()) {
  429. QName qName = (QName)iter.next();
  430. String value = (String)atts.get(qName);
  431. //The casting is only to ensure type safety (too bad we can't use generics, yet)
  432. setForeignAttribute(qName, value);
  433. }
  434. }
  435. /**
  436. * Returns the value of a foreign attribute on the area.
  437. * @param name the qualified name of the attribute
  438. * @return the attribute value or null if it isn't set
  439. */
  440. public String getForeignAttributeValue(QName name) {
  441. if (this.foreignAttributes != null) {
  442. return (String)this.foreignAttributes.get(name);
  443. } else {
  444. return null;
  445. }
  446. }
  447. /** @return the foreign attributes associated with this area */
  448. public Map getForeignAttributes() {
  449. if (this.foreignAttributes != null) {
  450. return Collections.unmodifiableMap(this.foreignAttributes);
  451. } else {
  452. return Collections.EMPTY_MAP;
  453. }
  454. }
  455. /** @see java.lang.Object#toString() */
  456. public String toString() {
  457. StringBuffer sb = new StringBuffer(super.toString());
  458. sb.append(" {ipd=").append(Integer.toString(getIPD()));
  459. sb.append(", bpd=").append(Integer.toString(getBPD()));
  460. sb.append("}");
  461. return sb.toString();
  462. }
  463. }