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.

Java2DGraphicsPainter.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.java2d;
  19. import java.awt.BasicStroke;
  20. import java.awt.Color;
  21. import java.awt.Graphics2D;
  22. import java.awt.Point;
  23. import java.awt.Rectangle;
  24. import java.awt.geom.GeneralPath;
  25. import java.awt.geom.Line2D;
  26. import java.io.IOException;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. import org.apache.fop.fo.Constants;
  30. import org.apache.fop.render.intermediate.GraphicsPainter;
  31. import org.apache.fop.traits.RuleStyle;
  32. import org.apache.fop.util.ColorUtil;
  33. class Java2DGraphicsPainter implements GraphicsPainter {
  34. /** logging instance */
  35. static final Log log = LogFactory.getLog(Java2DGraphicsPainter.class);
  36. private GeneralPath currentPath;
  37. private final Java2DPainter painter;
  38. Java2DGraphicsPainter(Java2DPainter painter) {
  39. this.painter = painter;
  40. }
  41. private Java2DGraphicsState getG2DState() {
  42. return this.painter.g2dState;
  43. }
  44. private Graphics2D getG2D() {
  45. return getG2DState().getGraph();
  46. }
  47. public void drawBorderLine(int x1, int y1, int x2, int y2,
  48. boolean horz, boolean startOrBefore, int style, Color color)
  49. throws IOException {
  50. float w = x2 - x1;
  51. float h = y2 - y1;
  52. if ((w < 0) || (h < 0)) {
  53. log.error("Negative extent received. Border won't be painted.");
  54. return;
  55. }
  56. switch (style) {
  57. case Constants.EN_DASHED:
  58. getG2D().setColor(color);
  59. if (horz) {
  60. float unit = Math.abs(2 * h);
  61. int rep = (int)(w / unit);
  62. if (rep % 2 == 0) {
  63. rep++;
  64. }
  65. unit = w / rep;
  66. float ym = y1 + (h / 2);
  67. BasicStroke s = new BasicStroke(h, BasicStroke.CAP_BUTT,
  68. BasicStroke.JOIN_MITER, 10.0f, new float[] {unit}, 0);
  69. getG2D().setStroke(s);
  70. getG2D().draw(new Line2D.Float(x1, ym, x2, ym));
  71. } else {
  72. float unit = Math.abs(2 * w);
  73. int rep = (int)(h / unit);
  74. if (rep % 2 == 0) {
  75. rep++;
  76. }
  77. unit = h / rep;
  78. float xm = x1 + (w / 2);
  79. BasicStroke s = new BasicStroke(w, BasicStroke.CAP_BUTT,
  80. BasicStroke.JOIN_MITER, 10.0f, new float[] {unit}, 0);
  81. getG2D().setStroke(s);
  82. getG2D().draw(new Line2D.Float(xm, y1, xm, y2));
  83. }
  84. break;
  85. case Constants.EN_DOTTED:
  86. getG2D().setColor(color);
  87. if (horz) {
  88. float unit = Math.abs(2 * h);
  89. int rep = (int)(w / unit);
  90. if (rep % 2 == 0) {
  91. rep++;
  92. }
  93. unit = w / rep;
  94. float ym = y1 + (h / 2);
  95. BasicStroke s = new BasicStroke(h, BasicStroke.CAP_ROUND,
  96. BasicStroke.JOIN_MITER, 10.0f, new float[] {0, unit}, 0);
  97. getG2D().setStroke(s);
  98. getG2D().draw(new Line2D.Float(x1, ym, x2, ym));
  99. } else {
  100. float unit = Math.abs(2 * w);
  101. int rep = (int)(h / unit);
  102. if (rep % 2 == 0) {
  103. rep++;
  104. }
  105. unit = h / rep;
  106. float xm = x1 + (w / 2);
  107. BasicStroke s = new BasicStroke(w, BasicStroke.CAP_ROUND,
  108. BasicStroke.JOIN_MITER, 10.0f, new float[] {0, unit}, 0);
  109. getG2D().setStroke(s);
  110. getG2D().draw(new Line2D.Float(xm, y1, xm, y2));
  111. }
  112. break;
  113. case Constants.EN_DOUBLE:
  114. getG2D().setColor(color);
  115. if (horz) {
  116. float h3 = h / 3;
  117. float ym1 = y1 + (h3 / 2);
  118. float ym2 = ym1 + h3 + h3;
  119. BasicStroke s = new BasicStroke(h3);
  120. getG2D().setStroke(s);
  121. getG2D().draw(new Line2D.Float(x1, ym1, x2, ym1));
  122. getG2D().draw(new Line2D.Float(x1, ym2, x2, ym2));
  123. } else {
  124. float w3 = w / 3;
  125. float xm1 = x1 + (w3 / 2);
  126. float xm2 = xm1 + w3 + w3;
  127. BasicStroke s = new BasicStroke(w3);
  128. getG2D().setStroke(s);
  129. getG2D().draw(new Line2D.Float(xm1, y1, xm1, y2));
  130. getG2D().draw(new Line2D.Float(xm2, y1, xm2, y2));
  131. }
  132. break;
  133. case Constants.EN_GROOVE:
  134. case Constants.EN_RIDGE:
  135. float colFactor = (style == Constants.EN_GROOVE ? 0.4f : -0.4f);
  136. if (horz) {
  137. Color uppercol = ColorUtil.lightenColor(color, -colFactor);
  138. Color lowercol = ColorUtil.lightenColor(color, colFactor);
  139. float h3 = h / 3;
  140. float ym1 = y1 + (h3 / 2);
  141. getG2D().setStroke(new BasicStroke(h3));
  142. getG2D().setColor(uppercol);
  143. getG2D().draw(new Line2D.Float(x1, ym1, x2, ym1));
  144. getG2D().setColor(color);
  145. getG2D().draw(new Line2D.Float(x1, ym1 + h3, x2, ym1 + h3));
  146. getG2D().setColor(lowercol);
  147. getG2D().draw(new Line2D.Float(x1, ym1 + h3 + h3, x2, ym1 + h3 + h3));
  148. } else {
  149. Color leftcol = ColorUtil.lightenColor(color, -colFactor);
  150. Color rightcol = ColorUtil.lightenColor(color, colFactor);
  151. float w3 = w / 3;
  152. float xm1 = x1 + (w3 / 2);
  153. getG2D().setStroke(new BasicStroke(w3));
  154. getG2D().setColor(leftcol);
  155. getG2D().draw(new Line2D.Float(xm1, y1, xm1, y2));
  156. getG2D().setColor(color);
  157. getG2D().draw(new Line2D.Float(xm1 + w3, y1, xm1 + w3, y2));
  158. getG2D().setColor(rightcol);
  159. getG2D().draw(new Line2D.Float(xm1 + w3 + w3, y1, xm1 + w3 + w3, y2));
  160. }
  161. break;
  162. case Constants.EN_INSET:
  163. case Constants.EN_OUTSET:
  164. colFactor = (style == Constants.EN_OUTSET ? 0.4f : -0.4f);
  165. if (horz) {
  166. color = ColorUtil.lightenColor(color, (startOrBefore ? 1 : -1) * colFactor);
  167. getG2D().setStroke(new BasicStroke(h));
  168. float ym1 = y1 + (h / 2);
  169. getG2D().setColor(color);
  170. getG2D().draw(new Line2D.Float(x1, ym1, x2, ym1));
  171. } else {
  172. color = ColorUtil.lightenColor(color, (startOrBefore ? 1 : -1) * colFactor);
  173. float xm1 = x1 + (w / 2);
  174. getG2D().setStroke(new BasicStroke(w));
  175. getG2D().setColor(color);
  176. getG2D().draw(new Line2D.Float(xm1, y1, xm1, y2));
  177. }
  178. break;
  179. case Constants.EN_HIDDEN:
  180. break;
  181. default:
  182. getG2D().setColor(color);
  183. if (horz) {
  184. float ym = y1 + (h / 2);
  185. getG2D().setStroke(new BasicStroke(h));
  186. getG2D().draw(new Line2D.Float(x1, ym, x2, ym));
  187. } else {
  188. float xm = x1 + (w / 2);
  189. getG2D().setStroke(new BasicStroke(w));
  190. getG2D().draw(new Line2D.Float(xm, y1, xm, y2));
  191. }
  192. }
  193. }
  194. public void drawLine(Point start, Point end, int width, Color color,
  195. RuleStyle style) throws IOException {
  196. if (start.y != end.y) {
  197. //TODO Support arbitrary lines if necessary
  198. throw new UnsupportedOperationException(
  199. "Can only deal with horizontal lines right now");
  200. }
  201. saveGraphicsState();
  202. int half = width / 2;
  203. int starty = start.y - half;
  204. Rectangle boundingRect = new Rectangle(start.x, start.y - half, end.x - start.x, width);
  205. getG2DState().updateClip(boundingRect);
  206. switch (style.getEnumValue()) {
  207. case Constants.EN_SOLID:
  208. case Constants.EN_DASHED:
  209. case Constants.EN_DOUBLE:
  210. drawBorderLine(start.x, start.y - half, end.x, end.y + half,
  211. true, true, style.getEnumValue(), color);
  212. break;
  213. case Constants.EN_DOTTED:
  214. int shift = half; //This shifts the dots to the right by half a dot's width
  215. drawBorderLine(start.x + shift, start.y - half, end.x + shift, end.y + half,
  216. true, true, style.getEnumValue(), color);
  217. break;
  218. case Constants.EN_GROOVE:
  219. case Constants.EN_RIDGE:
  220. getG2DState().updateColor(ColorUtil.lightenColor(color, 0.6f));
  221. moveTo(start.x, starty);
  222. lineTo(end.x, starty);
  223. lineTo(end.x, starty + 2 * half);
  224. lineTo(start.x, starty + 2 * half);
  225. closePath();
  226. getG2D().fill(currentPath);
  227. currentPath = null;
  228. getG2DState().updateColor(color);
  229. if (style.getEnumValue() == Constants.EN_GROOVE) {
  230. moveTo(start.x, starty);
  231. lineTo(end.x, starty);
  232. lineTo(end.x, starty + half);
  233. lineTo(start.x + half, starty + half);
  234. lineTo(start.x, starty + 2 * half);
  235. } else {
  236. moveTo(end.x, starty);
  237. lineTo(end.x, starty + 2 * half);
  238. lineTo(start.x, starty + 2 * half);
  239. lineTo(start.x, starty + half);
  240. lineTo(end.x - half, starty + half);
  241. }
  242. closePath();
  243. getG2D().fill(currentPath);
  244. currentPath = null;
  245. case Constants.EN_NONE:
  246. // No rule is drawn
  247. break;
  248. default:
  249. } // end switch
  250. restoreGraphicsState();
  251. }
  252. /** {@inheritDoc} */
  253. public void moveTo(int x, int y) throws IOException {
  254. if (currentPath == null) {
  255. currentPath = new GeneralPath();
  256. }
  257. currentPath.moveTo(x, y);
  258. }
  259. /** {@inheritDoc} */
  260. public void lineTo(int x, int y) throws IOException {
  261. if (currentPath == null) {
  262. currentPath = new GeneralPath();
  263. }
  264. currentPath.lineTo(x, y);
  265. }
  266. /** {@inheritDoc} */
  267. public void arcTo(double startAngle, double endAngle, int cx, int cy,
  268. int width, int height) throws IOException {
  269. }
  270. /** {@inheritDoc} */
  271. public void rotateCoordinates(double angle) throws IOException {
  272. }
  273. /** {@inheritDoc} */
  274. public void translateCoordinates(int xTranslate, int yTranslate)
  275. throws IOException {
  276. }
  277. /** {@inheritDoc} */
  278. public void scaleCoordinates(float xScale, float yScale)
  279. throws IOException {
  280. }
  281. /** {@inheritDoc} */
  282. public void closePath() throws IOException {
  283. currentPath.closePath();
  284. }
  285. /** {@inheritDoc} */
  286. public void clip() throws IOException {
  287. if (currentPath == null) {
  288. throw new IllegalStateException("No current path available!");
  289. }
  290. getG2DState().updateClip(currentPath);
  291. currentPath = null;
  292. }
  293. /** {@inheritDoc} */
  294. public void saveGraphicsState() throws IOException {
  295. this.painter.saveGraphicsState();
  296. }
  297. /** {@inheritDoc} */
  298. public void restoreGraphicsState() throws IOException {
  299. this.painter.restoreGraphicsState();
  300. this.currentPath = null;
  301. }
  302. }