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.

TraitSetter.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.layoutmgr;
  52. import org.apache.fop.layout.BorderAndPadding;
  53. import org.apache.fop.traits.BorderProps;
  54. import org.apache.fop.area.Area;
  55. import org.apache.fop.area.Trait;
  56. import org.apache.fop.layout.BackgroundProps;
  57. /**
  58. * This is a helper class used for setting common traits on areas.
  59. */
  60. public class TraitSetter {
  61. /**
  62. * Sets border and padding traits on areas.
  63. * @param area area to set the traits on
  64. * @param bpProps border and padding properties
  65. */
  66. public static void setBorderPaddingTraits(Area area,
  67. BorderAndPadding bpProps, boolean bNotFirst, boolean bNotLast) {
  68. int iBP;
  69. iBP = bpProps.getPadding(BorderAndPadding.START, bNotFirst);
  70. if (iBP > 0) {
  71. //area.addTrait(new Trait(Trait.PADDING_START, new Integer(iBP)));
  72. area.addTrait(Trait.PADDING_START, new Integer(iBP));
  73. }
  74. iBP = bpProps.getPadding(BorderAndPadding.END, bNotLast);
  75. if (iBP > 0) {
  76. //area.addTrait(new Trait(Trait.PADDING_END, new Integer(iBP)));
  77. area.addTrait(Trait.PADDING_END, new Integer(iBP));
  78. }
  79. iBP = bpProps.getPadding(BorderAndPadding.BEFORE, false);
  80. if (iBP > 0) {
  81. // area.addTrait(new Trait(Trait.PADDING_BEFORE, new Integer(iBP)));
  82. area.addTrait(Trait.PADDING_BEFORE, new Integer(iBP));
  83. }
  84. iBP = bpProps.getPadding(BorderAndPadding.AFTER, false);
  85. if (iBP > 0) {
  86. //area.addTrait(new Trait(Trait.PADDING_AFTER, new Integer(iBP)));
  87. area.addTrait(Trait.PADDING_AFTER, new Integer(iBP));
  88. }
  89. addBorderTrait(area, bpProps, bNotFirst,
  90. BorderAndPadding.START, Trait.BORDER_START);
  91. addBorderTrait(area, bpProps, bNotLast, BorderAndPadding.END,
  92. Trait.BORDER_END);
  93. addBorderTrait(area, bpProps, false, BorderAndPadding.BEFORE,
  94. Trait.BORDER_BEFORE);
  95. addBorderTrait(area, bpProps, false, BorderAndPadding.AFTER,
  96. Trait.BORDER_AFTER);
  97. }
  98. /**
  99. * Sets border traits on an area.
  100. * @param area area to set the traits on
  101. * @param bpProps border and padding properties
  102. */
  103. private static void addBorderTrait(Area area,
  104. BorderAndPadding bpProps,
  105. boolean bDiscard, int iSide,
  106. Object oTrait) {
  107. int iBP = bpProps.getBorderWidth(iSide, bDiscard);
  108. if (iBP > 0) {
  109. // area.addTrait(new Trait(oTrait,
  110. // new BorderProps(bpProps.getBorderStyle(iSide),
  111. // iBP,
  112. // bpProps.getBorderColor(iSide))));
  113. area.addTrait(oTrait,
  114. new BorderProps(bpProps.getBorderStyle(iSide),
  115. iBP, bpProps.getBorderColor(iSide)));
  116. }
  117. }
  118. /**
  119. * Add borders to an area.
  120. * Layout managers that create areas with borders can use this to
  121. * add the borders to the area.
  122. * @param curBlock area to set the traits on
  123. * @param bordProps border properties
  124. */
  125. public static void addBorders(Area curBlock, BorderAndPadding bordProps) {
  126. BorderProps bps = getBorderProps(bordProps, BorderAndPadding.TOP);
  127. if (bps.width != 0) {
  128. curBlock.addTrait(Trait.BORDER_BEFORE, bps);
  129. }
  130. bps = getBorderProps(bordProps, BorderAndPadding.BOTTOM);
  131. if (bps.width != 0) {
  132. curBlock.addTrait(Trait.BORDER_AFTER, bps);
  133. }
  134. bps = getBorderProps(bordProps, BorderAndPadding.LEFT);
  135. if (bps.width != 0) {
  136. curBlock.addTrait(Trait.BORDER_START, bps);
  137. }
  138. bps = getBorderProps(bordProps, BorderAndPadding.RIGHT);
  139. if (bps.width != 0) {
  140. curBlock.addTrait(Trait.BORDER_END, bps);
  141. }
  142. }
  143. private static BorderProps getBorderProps(BorderAndPadding bordProps, int side) {
  144. BorderProps bps;
  145. bps = new BorderProps(bordProps.getBorderStyle(side),
  146. bordProps.getBorderWidth(side, false),
  147. bordProps.getBorderColor(side));
  148. return bps;
  149. }
  150. /**
  151. * Add background to an area.
  152. * Layout managers that create areas with a background can use this to
  153. * add the background to the area.
  154. * @param curBlock the current block
  155. * @param backProps the background properties
  156. */
  157. public static void addBackground(Area curBlock, BackgroundProps backProps) {
  158. Trait.Background back = new Trait.Background();
  159. back.setColor(backProps.backColor);
  160. if (backProps.backImage != null) {
  161. back.setURL(backProps.backImage);
  162. back.setRepeat(backProps.backRepeat);
  163. if (backProps.backPosHorizontal != null) {
  164. back.setHoriz(backProps.backPosHorizontal.getValue());
  165. }
  166. if (backProps.backPosVertical != null) {
  167. back.setVertical(backProps.backPosVertical.getValue());
  168. }
  169. }
  170. if (back.getColor() != null || back.getURL() != null) {
  171. curBlock.addTrait(Trait.BACKGROUND, back);
  172. }
  173. }
  174. }