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.

AFPBorderPainter.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.afp;
  19. import java.awt.geom.AffineTransform;
  20. import org.apache.fop.fo.Constants;
  21. import org.apache.fop.util.ColorUtil;
  22. /**
  23. * Handles the drawing of borders/lines in AFP
  24. */
  25. public class AFPBorderPainter extends AbstractAFPPainter {
  26. /**
  27. * Main constructor
  28. *
  29. * @param paintingState the AFP painting state converter
  30. * @param dataStream the AFP datastream
  31. */
  32. public AFPBorderPainter(AFPPaintingState paintingState, DataStream dataStream) {
  33. super(paintingState, dataStream);
  34. }
  35. /** {@inheritDoc} */
  36. public void paint(PaintingInfo paintInfo) { // CSOK: MethodLength
  37. BorderPaintingInfo borderPaintInfo = (BorderPaintingInfo)paintInfo;
  38. float w = borderPaintInfo.getX2() - borderPaintInfo.getX1();
  39. float h = borderPaintInfo.getY2() - borderPaintInfo.getY1();
  40. if ((w < 0) || (h < 0)) {
  41. log.error("Negative extent received. Border won't be painted.");
  42. return;
  43. }
  44. int pageWidth = dataStream.getCurrentPage().getWidth();
  45. int pageHeight = dataStream.getCurrentPage().getHeight();
  46. AFPUnitConverter unitConv = paintingState.getUnitConverter();
  47. AffineTransform at = paintingState.getData().getTransform();
  48. float x1 = unitConv.pt2units(borderPaintInfo.getX1());
  49. float y1 = unitConv.pt2units(borderPaintInfo.getY1());
  50. float x2 = unitConv.pt2units(borderPaintInfo.getX2());
  51. float y2 = unitConv.pt2units(borderPaintInfo.getY2());
  52. switch (paintingState.getRotation()) {
  53. default:
  54. case 0:
  55. x1 += at.getTranslateX();
  56. y1 += at.getTranslateY();
  57. x2 += at.getTranslateX();
  58. y2 += at.getTranslateY();
  59. break;
  60. case 90:
  61. x1 += at.getTranslateY();
  62. y1 += (float) (pageWidth - at.getTranslateX());
  63. x2 += at.getTranslateY();
  64. y2 += (float) (pageWidth - at.getTranslateX());
  65. break;
  66. case 180:
  67. x1 += (float) (pageWidth - at.getTranslateX());
  68. y1 += (float) (pageHeight - at.getTranslateY());
  69. x2 += (float) (pageWidth - at.getTranslateX());
  70. y2 += (float) (pageHeight - at.getTranslateY());
  71. break;
  72. case 270:
  73. x1 = (float) (pageHeight - at.getTranslateY());
  74. y1 += (float) at.getTranslateX();
  75. x2 += x1;
  76. y2 += (float) at.getTranslateX();
  77. break;
  78. }
  79. AFPLineDataInfo lineDataInfo = new AFPLineDataInfo();
  80. lineDataInfo.setColor(borderPaintInfo.getColor());
  81. lineDataInfo.setRotation(paintingState.getRotation());
  82. lineDataInfo.setX1 ( Math.round(x1) );
  83. lineDataInfo.setY1 ( Math.round(y1) );
  84. float thickness;
  85. if (borderPaintInfo.isHorizontal()) {
  86. thickness = y2 - y1;
  87. } else {
  88. thickness = x2 - x1;
  89. }
  90. lineDataInfo.setThickness(Math.round(thickness));
  91. // handle border-*-style
  92. switch (borderPaintInfo.getStyle()) {
  93. case Constants.EN_DOUBLE:
  94. int thickness3 = (int)Math.floor(thickness / 3f);
  95. lineDataInfo.setThickness(thickness3);
  96. if (borderPaintInfo.isHorizontal()) {
  97. lineDataInfo.setX2 ( Math.round(x2) );
  98. lineDataInfo.setY2 ( lineDataInfo.getY1() );
  99. dataStream.createLine(lineDataInfo);
  100. int distance = thickness3 * 2;
  101. lineDataInfo = new AFPLineDataInfo(lineDataInfo);
  102. lineDataInfo.setY1 ( lineDataInfo.getY1() + distance );
  103. lineDataInfo.setY2 ( lineDataInfo.getY2() + distance );
  104. dataStream.createLine(lineDataInfo);
  105. } else {
  106. lineDataInfo.setX2 ( lineDataInfo.getX1() );
  107. lineDataInfo.setY2 ( Math.round(y2) );
  108. dataStream.createLine(lineDataInfo);
  109. int distance = thickness3 * 2;
  110. lineDataInfo = new AFPLineDataInfo(lineDataInfo);
  111. lineDataInfo.setX1 ( lineDataInfo.getX1() + distance );
  112. lineDataInfo.setX2 ( lineDataInfo.getX2() + distance );
  113. dataStream.createLine(lineDataInfo);
  114. }
  115. break;
  116. case Constants.EN_DASHED:
  117. int thick = lineDataInfo.getThickness() * 3;
  118. if (borderPaintInfo.isHorizontal()) {
  119. lineDataInfo.setX2 ( lineDataInfo.getX1() + thick );
  120. lineDataInfo.setY2 ( lineDataInfo.getY1() );
  121. int ex2 = Math.round(x2);
  122. while (lineDataInfo.getX1() + thick < ex2) {
  123. dataStream.createLine(lineDataInfo);
  124. lineDataInfo.setX1 ( lineDataInfo.getX1() + 2 * thick );
  125. lineDataInfo.setX2 ( lineDataInfo.getX1() + thick );
  126. }
  127. } else {
  128. lineDataInfo.setX2 ( lineDataInfo.getX1() );
  129. lineDataInfo.setY2 ( lineDataInfo.getY1() + thick );
  130. int ey2 = Math.round(y2);
  131. while (lineDataInfo.getY1() + thick < ey2) {
  132. dataStream.createLine(lineDataInfo);
  133. lineDataInfo.setY1 ( lineDataInfo.getY1() + 2 * thick );
  134. lineDataInfo.setY2 ( lineDataInfo.getY1() + thick );
  135. }
  136. }
  137. break;
  138. case Constants.EN_DOTTED:
  139. if (borderPaintInfo.isHorizontal()) {
  140. lineDataInfo.setX2 ( lineDataInfo.getX1() + lineDataInfo.getThickness() );
  141. lineDataInfo.setY2 ( lineDataInfo.getY1() );
  142. int ex2 = Math.round(x2);
  143. while (lineDataInfo.getX1() + lineDataInfo.getThickness() < ex2) {
  144. dataStream.createLine(lineDataInfo);
  145. lineDataInfo.setX1 ( lineDataInfo.getX1() + 3 * lineDataInfo.getThickness() );
  146. lineDataInfo.setX2 ( lineDataInfo.getX1() + lineDataInfo.getThickness() );
  147. }
  148. } else {
  149. lineDataInfo.setX2 ( lineDataInfo.getX1() );
  150. lineDataInfo.setY2 ( lineDataInfo.getY1() + lineDataInfo.getThickness() );
  151. int ey2 = Math.round(y2);
  152. while (lineDataInfo.getY1() + lineDataInfo.getThickness() < ey2) {
  153. dataStream.createLine(lineDataInfo);
  154. lineDataInfo.setY1 ( lineDataInfo.getY1() + 3 * lineDataInfo.getThickness() );
  155. lineDataInfo.setY2 ( lineDataInfo.getY1() + lineDataInfo.getThickness() );
  156. }
  157. }
  158. break;
  159. case Constants.EN_GROOVE:
  160. case Constants.EN_RIDGE:
  161. //TODO
  162. int yNew;
  163. lineDataInfo.setX2 ( Math.round(x2) );
  164. float colFactor = (borderPaintInfo.getStyle() == Constants.EN_GROOVE ? 0.4f : -0.4f);
  165. float h3 = (y2 - y1) / 3;
  166. lineDataInfo.setColor
  167. ( ColorUtil.lightenColor(borderPaintInfo.getColor(), -colFactor) );
  168. lineDataInfo.setThickness ( Math.round(h3) );
  169. yNew = Math.round(y1);
  170. lineDataInfo.setY1 ( yNew );
  171. lineDataInfo.setY2 ( yNew );
  172. dataStream.createLine(lineDataInfo);
  173. lineDataInfo.setColor ( borderPaintInfo.getColor() );
  174. yNew = Math.round(y1 + h3);
  175. lineDataInfo.setY1 ( yNew );
  176. lineDataInfo.setY2 ( yNew );
  177. dataStream.createLine(lineDataInfo);
  178. lineDataInfo.setColor ( ColorUtil.lightenColor(borderPaintInfo.getColor(), colFactor) );
  179. yNew = Math.round(y1 + h3 + h3);
  180. lineDataInfo.setY1 ( yNew );
  181. lineDataInfo.setY2 ( yNew );
  182. dataStream.createLine(lineDataInfo);
  183. break;
  184. case Constants.EN_HIDDEN:
  185. break;
  186. case Constants.EN_INSET:
  187. case Constants.EN_OUTSET:
  188. case Constants.EN_SOLID:
  189. default:
  190. if (borderPaintInfo.isHorizontal()) {
  191. lineDataInfo.setX2 ( Math.round(x2) );
  192. lineDataInfo.setY2 ( lineDataInfo.getY1() );
  193. } else {
  194. lineDataInfo.setX2 ( lineDataInfo.getX1() );
  195. lineDataInfo.setY2 ( Math.round(y2) );
  196. }
  197. dataStream.createLine(lineDataInfo);
  198. }
  199. }
  200. }