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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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) {
  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. case 0:
  54. x1 += at.getTranslateX();
  55. y1 += at.getTranslateY();
  56. x2 += at.getTranslateX();
  57. y2 += at.getTranslateY();
  58. break;
  59. case 90:
  60. x1 += at.getTranslateY();
  61. y1 += (float) (pageWidth - at.getTranslateX());
  62. x2 += at.getTranslateY();
  63. y2 += (float) (pageWidth - at.getTranslateX());
  64. break;
  65. case 180:
  66. x1 += (float) (pageWidth - at.getTranslateX());
  67. y1 += (float) (pageHeight - at.getTranslateY());
  68. x2 += (float) (pageWidth - at.getTranslateX());
  69. y2 += (float) (pageHeight - at.getTranslateY());
  70. break;
  71. case 270:
  72. x1 = (float) (pageHeight - at.getTranslateY());
  73. y1 += (float) at.getTranslateX();
  74. x2 += x1;
  75. y2 += (float) at.getTranslateX();
  76. break;
  77. }
  78. AFPLineDataInfo lineDataInfo = new AFPLineDataInfo();
  79. lineDataInfo.setColor(borderPaintInfo.getColor());
  80. lineDataInfo.setRotation(paintingState.getRotation());
  81. lineDataInfo.x1 = Math.round(x1);
  82. lineDataInfo.y1 = Math.round(y1);
  83. if (borderPaintInfo.isHorizontal()) {
  84. lineDataInfo.setThickness(Math.round(y2 - y1));
  85. } else {
  86. lineDataInfo.setThickness(Math.round(x2 - x1));
  87. }
  88. // handle border-*-style
  89. switch (borderPaintInfo.getStyle()) {
  90. case Constants.EN_DOUBLE:
  91. if (borderPaintInfo.isHorizontal()) {
  92. lineDataInfo.x2 = Math.round(x2);
  93. lineDataInfo.y2 = lineDataInfo.y1;
  94. dataStream.createLine(lineDataInfo);
  95. lineDataInfo.y1 += Math.round((lineDataInfo.thickness / 3) * 2);
  96. dataStream.createLine(lineDataInfo);
  97. } else {
  98. lineDataInfo.x2 = lineDataInfo.x1;
  99. lineDataInfo.y2 = Math.round(y2);
  100. dataStream.createLine(lineDataInfo);
  101. lineDataInfo.x1 += Math.round((lineDataInfo.thickness / 3) * 2);
  102. dataStream.createLine(lineDataInfo);
  103. }
  104. break;
  105. case Constants.EN_DASHED:
  106. int thick = lineDataInfo.thickness * 3;
  107. if (borderPaintInfo.isHorizontal()) {
  108. lineDataInfo.x2 = lineDataInfo.x1 + thick;
  109. lineDataInfo.y2 = lineDataInfo.y1;
  110. int ex2 = Math.round(x2);
  111. while (lineDataInfo.x1 + thick < ex2) {
  112. dataStream.createLine(lineDataInfo);
  113. lineDataInfo.x1 += 2 * thick;
  114. lineDataInfo.x2 = lineDataInfo.x1 + thick;
  115. }
  116. } else {
  117. lineDataInfo.x2 = lineDataInfo.x1;
  118. lineDataInfo.y2 = lineDataInfo.y1 + thick;
  119. int ey2 = Math.round(y2);
  120. while (lineDataInfo.y1 + thick < ey2) {
  121. dataStream.createLine(lineDataInfo);
  122. lineDataInfo.y1 += 2 * thick;
  123. lineDataInfo.y2 = lineDataInfo.y1 + thick;
  124. }
  125. }
  126. break;
  127. case Constants.EN_DOTTED:
  128. if (borderPaintInfo.isHorizontal()) {
  129. lineDataInfo.x2 = lineDataInfo.x1 + lineDataInfo.thickness;
  130. lineDataInfo.y2 = lineDataInfo.y1;
  131. int ex2 = Math.round(x2);
  132. while (lineDataInfo.x1 + lineDataInfo.thickness < ex2) {
  133. dataStream.createLine(lineDataInfo);
  134. lineDataInfo.x1 += 3 * lineDataInfo.thickness;
  135. lineDataInfo.x2 = lineDataInfo.x1 + lineDataInfo.thickness;
  136. }
  137. } else {
  138. lineDataInfo.x2 = lineDataInfo.x1;
  139. lineDataInfo.y2 = lineDataInfo.y1 + lineDataInfo.thickness;
  140. int ey2 = Math.round(y2);
  141. while (lineDataInfo.y1 + lineDataInfo.thickness < ey2) {
  142. dataStream.createLine(lineDataInfo);
  143. lineDataInfo.y1 += 3 * lineDataInfo.thickness;
  144. lineDataInfo.y2 = lineDataInfo.y1 + lineDataInfo.thickness;
  145. }
  146. }
  147. break;
  148. case Constants.EN_GROOVE:
  149. case Constants.EN_RIDGE:
  150. //TODO
  151. lineDataInfo.x2 = Math.round(x2);
  152. float colFactor = (borderPaintInfo.getStyle() == Constants.EN_GROOVE ? 0.4f : -0.4f);
  153. float h3 = (y2 - y1) / 3;
  154. lineDataInfo.color = ColorUtil.lightenColor(borderPaintInfo.getColor(), -colFactor);
  155. lineDataInfo.thickness = Math.round(h3);
  156. lineDataInfo.y1 = lineDataInfo.y2 = Math.round(y1);
  157. dataStream.createLine(lineDataInfo);
  158. lineDataInfo.color = borderPaintInfo.getColor();
  159. lineDataInfo.y1 = lineDataInfo.y2 = Math.round(y1 + h3);
  160. dataStream.createLine(lineDataInfo);
  161. lineDataInfo.color = ColorUtil.lightenColor(borderPaintInfo.getColor(), colFactor);
  162. lineDataInfo.y1 = lineDataInfo.y2 = Math.round(y1 + h3 + h3);
  163. dataStream.createLine(lineDataInfo);
  164. break;
  165. case Constants.EN_HIDDEN:
  166. break;
  167. case Constants.EN_INSET:
  168. case Constants.EN_OUTSET:
  169. case Constants.EN_SOLID:
  170. default:
  171. if (borderPaintInfo.isHorizontal()) {
  172. lineDataInfo.x2 = Math.round(x2);
  173. lineDataInfo.y2 = lineDataInfo.y1;
  174. } else {
  175. lineDataInfo.x2 = lineDataInfo.x1;
  176. lineDataInfo.y2 = Math.round(y2);
  177. }
  178. dataStream.createLine(lineDataInfo);
  179. }
  180. }
  181. }