Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Java2DBorderPainter.java 13KB

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