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.

BorderProps.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.traits;
  18. import org.apache.fop.area.Trait;
  19. import org.apache.fop.datatypes.ColorType;
  20. import org.apache.fop.fo.Constants;
  21. import org.apache.fop.fonts.FontTriplet;
  22. import java.io.Serializable;
  23. import java.util.StringTokenizer;
  24. /**
  25. * Border properties.
  26. * Class to store border trait propties for the area tree.
  27. */
  28. public class BorderProps implements Serializable {
  29. /** Separate border model */
  30. public static final int SEPARATE = 0;
  31. /** Collapsing border model, for borders inside a table */
  32. public static final int COLLAPSE_INNER = 1;
  33. /** Collapsing border model, for borders at the table's outer border */
  34. public static final int COLLAPSE_OUTER = 2;
  35. /** Border style (one of EN_*) */
  36. public int style; // Enum for border style
  37. /** Border color */
  38. public ColorType color;
  39. /** Border width */
  40. public int width;
  41. /** Border mode (one of SEPARATE, COLLAPSE_INNER and COLLAPSE_OUTER) */
  42. public int mode;
  43. /**
  44. * Constructs a new BorderProps instance.
  45. * @param style border style (one of EN_*)
  46. * @param width border width
  47. * @param color border color
  48. * @param mode border mode ((one of SEPARATE, COLLAPSE_INNER and COLLAPSE_OUTER)
  49. */
  50. public BorderProps(int style, int width, ColorType color, int mode) {
  51. this.style = style;
  52. this.width = width;
  53. this.color = Trait.Color.makeSerializable(color);
  54. this.mode = mode;
  55. }
  56. /**
  57. * Constructs a new BorderProps instance.
  58. * @param style border style (one of the XSL enum values for border style)
  59. * @param width border width
  60. * @param color border color
  61. * @param mode border mode ((one of SEPARATE, COLLAPSE_INNER and COLLAPSE_OUTER)
  62. */
  63. public BorderProps(String style, int width, ColorType color, int mode) {
  64. this(getConstantForStyle(style), width, color, mode);
  65. }
  66. /**
  67. * @param bp the border properties or null
  68. * @return the effective width of the clipped part of the border
  69. */
  70. public static int getClippedWidth(BorderProps bp) {
  71. if ((bp != null) && (bp.mode != SEPARATE)) {
  72. return bp.width / 2;
  73. } else {
  74. return 0;
  75. }
  76. }
  77. private String getStyleString() {
  78. switch (style) {
  79. case Constants.EN_NONE: return "none";
  80. case Constants.EN_HIDDEN: return "hidden";
  81. case Constants.EN_DOTTED: return "dotted";
  82. case Constants.EN_DASHED: return "dashed";
  83. case Constants.EN_SOLID: return "solid";
  84. case Constants.EN_DOUBLE: return "double";
  85. case Constants.EN_GROOVE: return "groove";
  86. case Constants.EN_RIDGE: return "ridge";
  87. case Constants.EN_INSET: return "inset";
  88. case Constants.EN_OUTSET: return "outset";
  89. default: throw new IllegalStateException("Illegal border style: " + style);
  90. }
  91. }
  92. private static int getConstantForStyle(String style) {
  93. if ("none".equalsIgnoreCase(style)) {
  94. return Constants.EN_NONE;
  95. } else if ("hidden".equalsIgnoreCase(style)) {
  96. return Constants.EN_HIDDEN;
  97. } else if ("dotted".equalsIgnoreCase(style)) {
  98. return Constants.EN_DOTTED;
  99. } else if ("dashed".equalsIgnoreCase(style)) {
  100. return Constants.EN_DASHED;
  101. } else if ("solid".equalsIgnoreCase(style)) {
  102. return Constants.EN_SOLID;
  103. } else if ("double".equalsIgnoreCase(style)) {
  104. return Constants.EN_DOUBLE;
  105. } else if ("groove".equalsIgnoreCase(style)) {
  106. return Constants.EN_GROOVE;
  107. } else if ("ridge".equalsIgnoreCase(style)) {
  108. return Constants.EN_RIDGE;
  109. } else if ("inset".equalsIgnoreCase(style)) {
  110. return Constants.EN_INSET;
  111. } else if ("outset".equalsIgnoreCase(style)) {
  112. return Constants.EN_OUTSET;
  113. } else {
  114. throw new IllegalStateException("Illegal border style: " + style);
  115. }
  116. }
  117. /** @see java.lang.Object#hashCode() */
  118. public int hashCode() {
  119. return toString().hashCode();
  120. }
  121. /** @see java.lang.Object#equals(java.lang.Object) */
  122. public boolean equals(Object obj) {
  123. if (obj == null) {
  124. return false;
  125. } else if (obj == this) {
  126. return true;
  127. } else {
  128. if (obj instanceof BorderProps) {
  129. BorderProps other = (BorderProps)obj;
  130. return (style == other.style)
  131. && color.equals(other.color)
  132. && width == other.width
  133. && mode == other.mode;
  134. }
  135. }
  136. return false;
  137. }
  138. /**
  139. * Returns a BorderProps represtation of a string of the format as written by
  140. * BorderProps.toString().
  141. * @param s the string
  142. * @return a BorderProps instance
  143. */
  144. public static BorderProps valueOf(String s) {
  145. if (s.startsWith("(") && s.endsWith(")")) {
  146. s = s.substring(1, s.length() - 1);
  147. StringTokenizer st = new StringTokenizer(s, ",");
  148. String style = st.nextToken();
  149. String color = st.nextToken();
  150. int width = Integer.parseInt(st.nextToken());
  151. int mode = SEPARATE;
  152. if (st.hasMoreTokens()) {
  153. String ms = st.nextToken();
  154. if ("collapse-inner".equalsIgnoreCase(ms)) {
  155. mode = COLLAPSE_INNER;
  156. } else if ("collapse-outer".equalsIgnoreCase(ms)) {
  157. mode = COLLAPSE_OUTER;
  158. }
  159. }
  160. return new BorderProps(style, width, Trait.Color.valueOf(color), mode);
  161. } else {
  162. throw new IllegalArgumentException("BorderProps must be surrounded by parentheses");
  163. }
  164. }
  165. /** @see java.lang.Object#toString() */
  166. public String toString() {
  167. StringBuffer sbuf = new StringBuffer();
  168. sbuf.append('(');
  169. sbuf.append(getStyleString());
  170. sbuf.append(',');
  171. sbuf.append(color);
  172. sbuf.append(',');
  173. sbuf.append(width);
  174. if (mode != SEPARATE) {
  175. sbuf.append(',');
  176. if (mode == COLLAPSE_INNER) {
  177. sbuf.append("collapse-inner");
  178. } else {
  179. sbuf.append("collapse-outer");
  180. }
  181. }
  182. sbuf.append(')');
  183. return sbuf.toString();
  184. }
  185. }