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.

BorderAndPadding.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.layout;
  52. import org.apache.fop.datatypes.ColorType;
  53. import org.apache.fop.datatypes.CondLength;
  54. import org.apache.fop.fo.properties.Constants;
  55. public class BorderAndPadding implements Cloneable {
  56. public static final int BEFORE = 0;
  57. public static final int AFTER = 1;
  58. public static final int START = 2;
  59. public static final int END = 3;
  60. public static final int TOP = BEFORE;
  61. public static final int BOTTOM = AFTER;
  62. public static final int LEFT = START;
  63. public static final int RIGHT = END;
  64. private static class ResolvedCondLength implements Cloneable {
  65. private int iLength; // Resolved length value
  66. private boolean bDiscard;
  67. public ResolvedCondLength(CondLength length) {
  68. bDiscard = length.isDiscard();
  69. iLength = length.getLengthValue();
  70. }
  71. public Object clone() throws CloneNotSupportedException {
  72. return super.clone();
  73. }
  74. }
  75. /**
  76. * Return a full copy of the BorderAndPadding information. This clones all
  77. * padding and border information.
  78. * @return The copy.
  79. */
  80. public Object clone() throws CloneNotSupportedException {
  81. BorderAndPadding bp = (BorderAndPadding) super.clone();
  82. bp.padding = (ResolvedCondLength[]) padding.clone();
  83. bp.borderInfo = (BorderInfo[]) borderInfo.clone();
  84. for (int i = 0; i < padding.length; i++) {
  85. if (padding[i] != null) {
  86. bp.padding[i] = (ResolvedCondLength) padding[i].clone();
  87. }
  88. if (borderInfo[i] != null) {
  89. bp.borderInfo[i] = (BorderInfo) borderInfo[i].clone();
  90. }
  91. }
  92. return bp;
  93. }
  94. public static class BorderInfo implements Cloneable {
  95. private int mStyle; // Enum for border style
  96. private ColorType mColor; // Border color
  97. private ResolvedCondLength mWidth;
  98. BorderInfo(int style, CondLength width, ColorType color) {
  99. mStyle = style;
  100. mWidth = new ResolvedCondLength(width);
  101. mColor = color;
  102. }
  103. public Object clone() throws CloneNotSupportedException {
  104. BorderInfo bi = (BorderInfo) super.clone();
  105. bi.mWidth = (ResolvedCondLength) mWidth.clone();
  106. // do we need to clone the Color too???
  107. return bi;
  108. }
  109. }
  110. private BorderInfo[] borderInfo = new BorderInfo[4];
  111. private ResolvedCondLength[] padding = new ResolvedCondLength[4];
  112. public BorderAndPadding() {
  113. }
  114. public void setBorder(int side, int style, CondLength width,
  115. ColorType color) {
  116. borderInfo[side] = new BorderInfo(style, width, color);
  117. }
  118. public void setPadding(int side, CondLength width) {
  119. padding[side] = new ResolvedCondLength(width);
  120. }
  121. public void setPaddingLength(int side, int iLength) {
  122. padding[side].iLength = iLength;
  123. }
  124. public void setBorderLength(int side, int iLength) {
  125. borderInfo[side].mWidth.iLength = iLength;
  126. }
  127. public int getBorderLeftWidth(boolean bDiscard) {
  128. return getBorderWidth(LEFT, bDiscard);
  129. }
  130. public int getBorderRightWidth(boolean bDiscard) {
  131. return getBorderWidth(RIGHT, bDiscard);
  132. }
  133. public int getBorderTopWidth(boolean bDiscard) {
  134. return getBorderWidth(TOP, bDiscard);
  135. }
  136. public int getBorderBottomWidth(boolean bDiscard) {
  137. return getBorderWidth(BOTTOM, bDiscard);
  138. }
  139. public int getPaddingLeft(boolean bDiscard) {
  140. return getPadding(LEFT, bDiscard);
  141. }
  142. public int getPaddingRight(boolean bDiscard) {
  143. return getPadding(RIGHT, bDiscard);
  144. }
  145. public int getPaddingBottom(boolean bDiscard) {
  146. return getPadding(BOTTOM, bDiscard);
  147. }
  148. public int getPaddingTop(boolean bDiscard) {
  149. return getPadding(TOP, bDiscard);
  150. }
  151. public int getBorderWidth(int side, boolean bDiscard) {
  152. if ((borderInfo[side] == null)
  153. || (borderInfo[side].mStyle == Constants.NONE)
  154. || (bDiscard && borderInfo[side].mWidth.bDiscard)) {
  155. return 0;
  156. } else {
  157. return borderInfo[side].mWidth.iLength;
  158. }
  159. }
  160. public ColorType getBorderColor(int side) {
  161. if (borderInfo[side] != null) {
  162. return borderInfo[side].mColor;
  163. } else {
  164. return null;
  165. }
  166. }
  167. public int getBorderStyle(int side) {
  168. if (borderInfo[side] != null) {
  169. return borderInfo[side].mStyle;
  170. } else {
  171. return 0;
  172. }
  173. }
  174. public int getPadding(int side, boolean bDiscard) {
  175. if ((padding[side] == null) || (bDiscard && padding[side].bDiscard)) {
  176. return 0;
  177. } else {
  178. return padding[side].iLength;
  179. }
  180. }
  181. }