Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

BorderProps.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.traits;
  19. import java.awt.Color;
  20. import java.io.Serializable;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23. import org.apache.fop.apps.FOUserAgent;
  24. import org.apache.fop.fo.expr.PropertyException;
  25. import org.apache.fop.util.ColorUtil;
  26. /**
  27. * Border properties.
  28. * Class to store border trait properties for the area tree.
  29. */
  30. public class BorderProps implements Serializable {
  31. private static final long serialVersionUID = 8022237892391068187L;
  32. public enum Mode {
  33. SEPARATE("separate") {
  34. @Override
  35. int getClippedWidth(BorderProps bp) {
  36. return 0;
  37. }
  38. },
  39. COLLAPSE_INNER("collapse-inner"), // for borders inside a table
  40. COLLAPSE_OUTER("collapse-outer"); // for borders at the table's outer border
  41. private final String value;
  42. Mode(String value) {
  43. this.value = value;
  44. }
  45. int getClippedWidth(BorderProps bp) {
  46. return bp.width / 2;
  47. };
  48. }
  49. /** Border style (one of EN_*) */
  50. public final int style; // Enum for border style // CSOK: VisibilityModifier
  51. /** Border color */
  52. public final Color color; // CSOK: VisibilityModifier
  53. /** Border width */
  54. public final int width; // CSOK: VisibilityModifier
  55. private final int radiusStart;
  56. private final int radiusEnd;
  57. /** Border mode */
  58. private final Mode mode; // CSOK: VisibilityModifier
  59. /**
  60. * Constructs a new BorderProps instance.
  61. * @param style border style (one of EN_*)
  62. * @param width border width
  63. * @param radiusStart radius of start corner in the direction perpendicular to border segment
  64. * @param radiusEnd radius of end corner in the direction perpendicular to border segment
  65. * @param color border color
  66. * @param mode border mode ((one of SEPARATE, COLLAPSE_INNER and COLLAPSE_OUTER)
  67. */
  68. public BorderProps(int style, int width, int radiusStart, int radiusEnd, Color color, Mode mode) {
  69. this.style = style;
  70. this.width = width;
  71. this.radiusStart = radiusStart;
  72. this.radiusEnd = radiusEnd;
  73. this.color = color;
  74. this.mode = mode;
  75. }
  76. /**
  77. * Factory method for a new BorderProps instance with rectangular corners.
  78. * @param style border style (one of EN_*)
  79. * @param width border width
  80. * @param color border color
  81. * @param mode border mode ((one of SEPARATE, COLLAPSE_INNER and COLLAPSE_OUTER)
  82. */
  83. public static BorderProps makeRectangular(int style, int width, Color color, Mode mode) {
  84. return new BorderProps(style, width, 0, 0, color, mode);
  85. }
  86. private BorderProps(String style, int width, int radiusStart, int radiusEnd, Color color, Mode mode) {
  87. this(getConstantForStyle(style), width, radiusStart, radiusEnd, color, mode);
  88. }
  89. /**
  90. *
  91. * @return the radius of the corner adjacent to the before or start border
  92. */
  93. public int getRadiusStart() {
  94. return radiusStart;
  95. }
  96. /**
  97. * @return the radius of the corner adjacent to the after or end border
  98. */
  99. public int getRadiusEnd() {
  100. return radiusEnd;
  101. }
  102. /**
  103. * @param bp the border properties or null
  104. * @return the effective width of the clipped part of the border
  105. */
  106. public static int getClippedWidth(BorderProps bp) {
  107. return bp == null ? 0 : bp.mode.getClippedWidth(bp);
  108. }
  109. private String getStyleString() {
  110. return BorderStyle.valueOf(style).getName();
  111. }
  112. private static int getConstantForStyle(String style) {
  113. return BorderStyle.valueOf(style).getEnumValue();
  114. }
  115. public boolean isCollapseOuter() {
  116. return mode == Mode.COLLAPSE_OUTER;
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public int hashCode() {
  121. return toString().hashCode();
  122. }
  123. /** {@inheritDoc} */
  124. @Override
  125. public boolean equals(Object obj) {
  126. if (obj == null) {
  127. return false;
  128. } else if (obj == this) {
  129. return true;
  130. } else {
  131. if (obj instanceof BorderProps) {
  132. BorderProps other = (BorderProps) obj;
  133. return (style == other.style)
  134. && org.apache.xmlgraphics.java2d.color.ColorUtil.isSameColor(
  135. color, other.color)
  136. && width == other.width
  137. && mode == other.mode
  138. && radiusStart == other.radiusStart
  139. && radiusEnd == other.radiusEnd;
  140. }
  141. }
  142. return false;
  143. }
  144. /**
  145. * Returns a BorderProps represtation of a string of the format as written by
  146. * BorderProps.toString().
  147. * @param foUserAgent FOP user agent caching ICC profiles
  148. * @param s the string
  149. * @return a BorderProps instance
  150. */
  151. public static BorderProps valueOf(FOUserAgent foUserAgent, String s) {
  152. return BorderPropsDeserializer.INSTANCE.valueOf(foUserAgent, s);
  153. }
  154. /** {@inheritDoc} */
  155. @Override
  156. public String toString() {
  157. StringBuffer sbuf = new StringBuffer();
  158. sbuf.append('(')
  159. .append(getStyleString()).append(',')
  160. .append(ColorUtil.colorToString(color)).append(',')
  161. .append(width);
  162. if (!mode.equals(Mode.SEPARATE)) {
  163. sbuf.append(",").append(mode.value);
  164. }
  165. if (radiusStart != 0 || radiusEnd != 0) {
  166. if (mode.equals(Mode.SEPARATE)) {
  167. // Because of the corner radii properties the mode must be set
  168. // so that the parameter index is consistent
  169. sbuf.append(",").append(Mode.SEPARATE.value);
  170. }
  171. sbuf.append(',').append(radiusStart)
  172. .append(',').append(radiusEnd);
  173. }
  174. sbuf.append(')');
  175. return sbuf.toString();
  176. }
  177. private static final class BorderPropsDeserializer {
  178. private static final BorderPropsDeserializer INSTANCE = new BorderPropsDeserializer();
  179. private static final Pattern PATTERN = Pattern.compile("([^,\\(]+(?:\\(.*\\))?)");
  180. private BorderPropsDeserializer() {
  181. }
  182. public BorderProps valueOf(FOUserAgent foUserAgent, String s) {
  183. if (s.startsWith("(") && s.endsWith(")")) {
  184. s = s.substring(1, s.length() - 1);
  185. Matcher m = PATTERN.matcher(s);
  186. m.find();
  187. String style = m.group();
  188. m.find();
  189. String color = m.group();
  190. m.find();
  191. int width = Integer.parseInt(m.group());
  192. Mode mode = Mode.SEPARATE;
  193. if (m.find()) {
  194. String ms = m.group();
  195. if (Mode.COLLAPSE_INNER.value.equalsIgnoreCase(ms)) {
  196. mode = Mode.COLLAPSE_INNER;
  197. } else if (Mode.COLLAPSE_OUTER.value.equalsIgnoreCase(ms)) {
  198. mode = Mode.COLLAPSE_OUTER;
  199. }
  200. }
  201. Color c;
  202. try {
  203. c = ColorUtil.parseColorString(foUserAgent, color);
  204. } catch (PropertyException e) {
  205. throw new IllegalArgumentException(e.getMessage());
  206. }
  207. int startRadius = 0;
  208. int endRadius = 0;
  209. if (m.find()) {
  210. startRadius = Integer.parseInt(m.group());
  211. m.find();
  212. endRadius = Integer.parseInt(m.group());
  213. }
  214. return new BorderProps(style, width, startRadius, endRadius, c, mode);
  215. } else {
  216. throw new IllegalArgumentException("BorderProps must be surrounded by parentheses");
  217. }
  218. }
  219. }
  220. }