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.

BorderPainter.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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.render.intermediate;
  19. import java.awt.Color;
  20. import java.awt.Point;
  21. import java.awt.Rectangle;
  22. import java.io.IOException;
  23. import org.apache.fop.traits.BorderProps;
  24. import org.apache.fop.traits.RuleStyle;
  25. /**
  26. * This is an abstract base class for handling border painting.
  27. */
  28. public abstract class BorderPainter {
  29. /**
  30. * Draws borders.
  31. * @param borderRect the border rectangle
  32. * @param bpsBefore the border specification on the before side
  33. * @param bpsAfter the border specification on the after side
  34. * @param bpsStart the border specification on the start side
  35. * @param bpsEnd the border specification on the end side
  36. * @throws IOException if an I/O error occurs while creating the borders
  37. */
  38. public void drawBorders(Rectangle borderRect, // CSOK: MethodLength
  39. BorderProps bpsBefore, BorderProps bpsAfter,
  40. BorderProps bpsStart, BorderProps bpsEnd) throws IOException {
  41. int startx = borderRect.x;
  42. int starty = borderRect.y;
  43. int width = borderRect.width;
  44. int height = borderRect.height;
  45. boolean[] b = new boolean[] {
  46. (bpsBefore != null), (bpsEnd != null),
  47. (bpsAfter != null), (bpsStart != null)};
  48. if (!b[0] && !b[1] && !b[2] && !b[3]) {
  49. return;
  50. }
  51. int[] bw = new int[] {
  52. (b[0] ? bpsBefore.width : 0),
  53. (b[1] ? bpsEnd.width : 0),
  54. (b[2] ? bpsAfter.width : 0),
  55. (b[3] ? bpsStart.width : 0)};
  56. int[] clipw = new int[] {
  57. BorderProps.getClippedWidth(bpsBefore),
  58. BorderProps.getClippedWidth(bpsEnd),
  59. BorderProps.getClippedWidth(bpsAfter),
  60. BorderProps.getClippedWidth(bpsStart)};
  61. starty += clipw[0];
  62. height -= clipw[0];
  63. height -= clipw[2];
  64. startx += clipw[3];
  65. width -= clipw[3];
  66. width -= clipw[1];
  67. boolean[] slant = new boolean[] {
  68. (b[3] && b[0]), (b[0] && b[1]), (b[1] && b[2]), (b[2] && b[3])};
  69. if (bpsBefore != null) {
  70. int sx1 = startx;
  71. int sx2 = (slant[0] ? sx1 + bw[3] - clipw[3] : sx1);
  72. int ex1 = startx + width;
  73. int ex2 = (slant[1] ? ex1 - bw[1] + clipw[1] : ex1);
  74. int outery = starty - clipw[0];
  75. int clipy = outery + clipw[0];
  76. int innery = outery + bw[0];
  77. saveGraphicsState();
  78. moveTo(sx1, clipy);
  79. int sx1a = sx1;
  80. int ex1a = ex1;
  81. if (bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  82. if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  83. sx1a -= clipw[3];
  84. }
  85. if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  86. ex1a += clipw[1];
  87. }
  88. lineTo(sx1a, outery);
  89. lineTo(ex1a, outery);
  90. }
  91. lineTo(ex1, clipy);
  92. lineTo(ex2, innery);
  93. lineTo(sx2, innery);
  94. closePath();
  95. clip();
  96. drawBorderLine(sx1a, outery, ex1a, innery, true, true,
  97. bpsBefore.style, bpsBefore.color);
  98. restoreGraphicsState();
  99. }
  100. if (bpsEnd != null) {
  101. int sy1 = starty;
  102. int sy2 = (slant[1] ? sy1 + bw[0] - clipw[0] : sy1);
  103. int ey1 = starty + height;
  104. int ey2 = (slant[2] ? ey1 - bw[2] + clipw[2] : ey1);
  105. int outerx = startx + width + clipw[1];
  106. int clipx = outerx - clipw[1];
  107. int innerx = outerx - bw[1];
  108. saveGraphicsState();
  109. moveTo(clipx, sy1);
  110. int sy1a = sy1;
  111. int ey1a = ey1;
  112. if (bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  113. if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  114. sy1a -= clipw[0];
  115. }
  116. if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  117. ey1a += clipw[2];
  118. }
  119. lineTo(outerx, sy1a);
  120. lineTo(outerx, ey1a);
  121. }
  122. lineTo(clipx, ey1);
  123. lineTo(innerx, ey2);
  124. lineTo(innerx, sy2);
  125. closePath();
  126. clip();
  127. drawBorderLine(innerx, sy1a, outerx, ey1a, false, false, bpsEnd.style, bpsEnd.color);
  128. restoreGraphicsState();
  129. }
  130. if (bpsAfter != null) {
  131. int sx1 = startx;
  132. int sx2 = (slant[3] ? sx1 + bw[3] - clipw[3] : sx1);
  133. int ex1 = startx + width;
  134. int ex2 = (slant[2] ? ex1 - bw[1] + clipw[1] : ex1);
  135. int outery = starty + height + clipw[2];
  136. int clipy = outery - clipw[2];
  137. int innery = outery - bw[2];
  138. saveGraphicsState();
  139. moveTo(ex1, clipy);
  140. int sx1a = sx1;
  141. int ex1a = ex1;
  142. if (bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  143. if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  144. sx1a -= clipw[3];
  145. }
  146. if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  147. ex1a += clipw[1];
  148. }
  149. lineTo(ex1a, outery);
  150. lineTo(sx1a, outery);
  151. }
  152. lineTo(sx1, clipy);
  153. lineTo(sx2, innery);
  154. lineTo(ex2, innery);
  155. closePath();
  156. clip();
  157. drawBorderLine(sx1a, innery, ex1a, outery, true, false, bpsAfter.style, bpsAfter.color);
  158. restoreGraphicsState();
  159. }
  160. if (bpsStart != null) {
  161. int sy1 = starty;
  162. int sy2 = (slant[0] ? sy1 + bw[0] - clipw[0] : sy1);
  163. int ey1 = sy1 + height;
  164. int ey2 = (slant[3] ? ey1 - bw[2] + clipw[2] : ey1);
  165. int outerx = startx - clipw[3];
  166. int clipx = outerx + clipw[3];
  167. int innerx = outerx + bw[3];
  168. saveGraphicsState();
  169. moveTo(clipx, ey1);
  170. int sy1a = sy1;
  171. int ey1a = ey1;
  172. if (bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  173. if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  174. sy1a -= clipw[0];
  175. }
  176. if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  177. ey1a += clipw[2];
  178. }
  179. lineTo(outerx, ey1a);
  180. lineTo(outerx, sy1a);
  181. }
  182. lineTo(clipx, sy1);
  183. lineTo(innerx, sy2);
  184. lineTo(innerx, ey2);
  185. closePath();
  186. clip();
  187. drawBorderLine(outerx, sy1a, innerx, ey1a, false, true, bpsStart.style, bpsStart.color);
  188. restoreGraphicsState();
  189. }
  190. }
  191. /**
  192. * Draws a border line.
  193. * @param x1 X coordinate of the upper left corner
  194. * of the line's bounding rectangle (in millipoints)
  195. * @param y1 start Y coordinate of the upper left corner
  196. * of the line's bounding rectangle (in millipoints)
  197. * @param x2 end X coordinate of the lower right corner
  198. * of the line's bounding rectangle (in millipoints)
  199. * @param y2 end y coordinate of the lower right corner
  200. * of the line's bounding rectangle (in millipoints)
  201. * @param horz true if it is a horizontal line
  202. * @param startOrBefore true if the line is the start or end edge of a border box
  203. * @param style the border style
  204. * @param color the border color
  205. * @throws IOException if an I/O error occurs
  206. */
  207. protected abstract void drawBorderLine( // CSOK: ParameterNumber
  208. int x1, int y1, int x2, int y2,
  209. boolean horz, boolean startOrBefore, int style, Color color) throws IOException;
  210. /**
  211. * Draws a line/rule.
  212. * @param start start point (coordinates in millipoints)
  213. * @param end end point (coordinates in millipoints)
  214. * @param width width of the line
  215. * @param color the line color
  216. * @param style the rule style
  217. * @throws IOException if an I/O error occurs
  218. */
  219. public abstract void drawLine(Point start, Point end,
  220. int width, Color color, RuleStyle style) throws IOException;
  221. /**
  222. * Moves the cursor to the given coordinate.
  223. * @param x the X coordinate (in millipoints)
  224. * @param y the Y coordinate (in millipoints)
  225. * @throws IOException if an I/O error occurs
  226. */
  227. protected abstract void moveTo(int x, int y) throws IOException;
  228. /**
  229. * Draws a line from the current cursor position to the given coordinates.
  230. * @param x the X coordinate (in millipoints)
  231. * @param y the Y coordinate (in millipoints)
  232. * @throws IOException if an I/O error occurs
  233. */
  234. protected abstract void lineTo(int x, int y) throws IOException;
  235. /**
  236. * Closes the current path.
  237. * @throws IOException if an I/O error occurs
  238. */
  239. protected abstract void closePath() throws IOException;
  240. /**
  241. * Reduces the current clipping region to the current path.
  242. * @throws IOException if an I/O error occurs
  243. */
  244. protected abstract void clip() throws IOException;
  245. /**
  246. * Save the graphics state on the stack.
  247. * @throws IOException if an I/O error occurs
  248. */
  249. protected abstract void saveGraphicsState() throws IOException;
  250. /**
  251. * Restore the last graphics state from the stack.
  252. * @throws IOException if an I/O error occurs
  253. */
  254. protected abstract void restoreGraphicsState() throws IOException;
  255. }