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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.Rectangle;
  21. import org.apache.fop.traits.BorderProps;
  22. /**
  23. * This is an abstract base class for handling border painting.
  24. */
  25. public abstract class BorderPainter {
  26. /**
  27. * Draws borders.
  28. * @param borderRect the border rectangle
  29. * @param bpsBefore the border specification on the before side
  30. * @param bpsAfter the border specification on the after side
  31. * @param bpsStart the border specification on the start side
  32. * @param bpsEnd the border specification on the end side
  33. */
  34. public void drawBorders(Rectangle borderRect,
  35. BorderProps bpsBefore, BorderProps bpsAfter,
  36. BorderProps bpsStart, BorderProps bpsEnd) {
  37. int startx = borderRect.x;
  38. int starty = borderRect.y;
  39. int width = borderRect.width;
  40. int height = borderRect.height;
  41. boolean[] b = new boolean[] {
  42. (bpsBefore != null), (bpsEnd != null),
  43. (bpsAfter != null), (bpsStart != null)};
  44. if (!b[0] && !b[1] && !b[2] && !b[3]) {
  45. return;
  46. }
  47. int[] bw = new int[] {
  48. (b[0] ? bpsBefore.width : 0),
  49. (b[1] ? bpsEnd.width : 0),
  50. (b[2] ? bpsAfter.width : 0),
  51. (b[3] ? bpsStart.width : 0)};
  52. int[] clipw = new int[] {
  53. BorderProps.getClippedWidth(bpsBefore),
  54. BorderProps.getClippedWidth(bpsEnd),
  55. BorderProps.getClippedWidth(bpsAfter),
  56. BorderProps.getClippedWidth(bpsStart)};
  57. starty += clipw[0];
  58. height -= clipw[0];
  59. height -= clipw[2];
  60. startx += clipw[3];
  61. width -= clipw[3];
  62. width -= clipw[1];
  63. boolean[] slant = new boolean[] {
  64. (b[3] && b[0]), (b[0] && b[1]), (b[1] && b[2]), (b[2] && b[3])};
  65. if (bpsBefore != null) {
  66. int sx1 = startx;
  67. int sx2 = (slant[0] ? sx1 + bw[3] - clipw[3] : sx1);
  68. int ex1 = startx + width;
  69. int ex2 = (slant[1] ? ex1 - bw[1] + clipw[1] : ex1);
  70. int outery = starty - clipw[0];
  71. int clipy = outery + clipw[0];
  72. int innery = outery + bw[0];
  73. saveGraphicsState();
  74. moveTo(sx1, clipy);
  75. int sx1a = sx1;
  76. int ex1a = ex1;
  77. if (bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  78. if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  79. sx1a -= clipw[3];
  80. }
  81. if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  82. ex1a += clipw[1];
  83. }
  84. lineTo(sx1a, outery);
  85. lineTo(ex1a, outery);
  86. }
  87. lineTo(ex1, clipy);
  88. lineTo(ex2, innery);
  89. lineTo(sx2, innery);
  90. closePath();
  91. clip();
  92. drawBorderLine(sx1a, outery, ex1a, innery, true, true,
  93. bpsBefore.style, bpsBefore.color);
  94. restoreGraphicsState();
  95. }
  96. if (bpsEnd != null) {
  97. int sy1 = starty;
  98. int sy2 = (slant[1] ? sy1 + bw[0] - clipw[0] : sy1);
  99. int ey1 = starty + height;
  100. int ey2 = (slant[2] ? ey1 - bw[2] + clipw[2] : ey1);
  101. int outerx = startx + width + clipw[1];
  102. int clipx = outerx - clipw[1];
  103. int innerx = outerx - bw[1];
  104. saveGraphicsState();
  105. moveTo(clipx, sy1);
  106. int sy1a = sy1;
  107. int ey1a = ey1;
  108. if (bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  109. if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  110. sy1a -= clipw[0];
  111. }
  112. if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  113. ey1a += clipw[2];
  114. }
  115. lineTo(outerx, sy1a);
  116. lineTo(outerx, ey1a);
  117. }
  118. lineTo(clipx, ey1);
  119. lineTo(innerx, ey2);
  120. lineTo(innerx, sy2);
  121. closePath();
  122. clip();
  123. drawBorderLine(innerx, sy1a, outerx, ey1a, false, false, bpsEnd.style, bpsEnd.color);
  124. restoreGraphicsState();
  125. }
  126. if (bpsAfter != null) {
  127. int sx1 = startx;
  128. int sx2 = (slant[3] ? sx1 + bw[3] - clipw[3] : sx1);
  129. int ex1 = startx + width;
  130. int ex2 = (slant[2] ? ex1 - bw[1] + clipw[1] : ex1);
  131. int outery = starty + height + clipw[2];
  132. int clipy = outery - clipw[2];
  133. int innery = outery - bw[2];
  134. saveGraphicsState();
  135. moveTo(ex1, clipy);
  136. int sx1a = sx1;
  137. int ex1a = ex1;
  138. if (bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  139. if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  140. sx1a -= clipw[3];
  141. }
  142. if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  143. ex1a += clipw[1];
  144. }
  145. lineTo(ex1a, outery);
  146. lineTo(sx1a, outery);
  147. }
  148. lineTo(sx1, clipy);
  149. lineTo(sx2, innery);
  150. lineTo(ex2, innery);
  151. closePath();
  152. clip();
  153. drawBorderLine(sx1a, innery, ex1a, outery, true, false, bpsAfter.style, bpsAfter.color);
  154. restoreGraphicsState();
  155. }
  156. if (bpsStart != null) {
  157. int sy1 = starty;
  158. int sy2 = (slant[0] ? sy1 + bw[0] - clipw[0] : sy1);
  159. int ey1 = sy1 + height;
  160. int ey2 = (slant[3] ? ey1 - bw[2] + clipw[2] : ey1);
  161. int outerx = startx - clipw[3];
  162. int clipx = outerx + clipw[3];
  163. int innerx = outerx + bw[3];
  164. saveGraphicsState();
  165. moveTo(clipx, ey1);
  166. int sy1a = sy1;
  167. int ey1a = ey1;
  168. if (bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  169. if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  170. sy1a -= clipw[0];
  171. }
  172. if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  173. ey1a += clipw[2];
  174. }
  175. lineTo(outerx, ey1a);
  176. lineTo(outerx, sy1a);
  177. }
  178. lineTo(clipx, sy1);
  179. lineTo(innerx, sy2);
  180. lineTo(innerx, ey2);
  181. closePath();
  182. clip();
  183. drawBorderLine(outerx, sy1a, innerx, ey1a, false, true, bpsStart.style, bpsStart.color);
  184. restoreGraphicsState();
  185. }
  186. }
  187. protected abstract void drawBorderLine(int x1, int y1, int x2, int y2,
  188. boolean horz, boolean startOrBefore, int style, Color col);
  189. protected abstract void moveTo(int x, int y);
  190. protected abstract void lineTo(int x, int y);
  191. protected abstract void closePath();
  192. protected abstract void clip();
  193. protected abstract void saveGraphicsState();
  194. protected abstract void restoreGraphicsState();
  195. }