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.

PDFBorderPainter.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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.pdf;
  19. import java.awt.Color;
  20. import java.awt.Point;
  21. import java.awt.Rectangle;
  22. import java.io.IOException;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.apache.fop.fo.Constants;
  26. import org.apache.fop.render.intermediate.BorderPainter;
  27. import org.apache.fop.traits.RuleStyle;
  28. import org.apache.fop.util.ColorUtil;
  29. /**
  30. * PDF-specific implementation of the {@link BorderPainter}.
  31. */
  32. public class PDFBorderPainter extends BorderPainter {
  33. /** logging instance */
  34. private static final Log LOG = LogFactory.getLog(PDFBorderPainter.class);
  35. private PDFContentGenerator generator;
  36. /**
  37. * Construct a border painter.
  38. * @param generator a pdf content generator
  39. */
  40. public PDFBorderPainter(PDFContentGenerator generator) {
  41. this.generator = generator;
  42. }
  43. /** {@inheritDoc} */
  44. protected void drawBorderLine( // CSOK: ParameterNumber
  45. int x1, int y1, int x2, int y2, boolean horz,
  46. boolean startOrBefore, int style, Color col) {
  47. //TODO lose scale?
  48. drawBorderLine(generator, x1 / 1000f, y1 / 1000f, x2 / 1000f, y2 / 1000f,
  49. horz, startOrBefore, style, col);
  50. }
  51. /**
  52. * @param generator pdf content generator
  53. * @see BorderPainter#drawBorderLine
  54. */
  55. public static void drawBorderLine( // CSOK: ParameterNumber|MethodLength
  56. PDFContentGenerator generator,
  57. float x1, float y1, float x2, float y2, boolean horz, // CSOK: JavadocMethod
  58. boolean startOrBefore, int style, Color col) { // CSOK: JavadocMethod
  59. float colFactor;
  60. float w = x2 - x1;
  61. float h = y2 - y1;
  62. /*
  63. if ((w < 0) || (h < 0)) {
  64. LOG.error("Negative extent received (w=" + w + ", h=" + h
  65. + "). Border won't be painted.");
  66. return;
  67. }*/
  68. switch (style) {
  69. case Constants.EN_DASHED:
  70. generator.setColor(col, false);
  71. if (horz) {
  72. float unit = Math.abs(2 * h);
  73. int rep = (int)(w / unit);
  74. if (rep % 2 == 0) {
  75. rep++;
  76. }
  77. unit = w / rep;
  78. generator.add("[" + format(unit) + "] 0 d ");
  79. generator.add(format(h) + " w\n");
  80. float ym = y1 + (h / 2);
  81. generator.add(format(x1) + " " + format(ym) + " m "
  82. + format(x2) + " " + format(ym) + " l S\n");
  83. } else {
  84. float unit = Math.abs(2 * w);
  85. int rep = (int)(h / unit);
  86. if (rep % 2 == 0) {
  87. rep++;
  88. }
  89. unit = h / rep;
  90. generator.add("[" + format(unit) + "] 0 d ");
  91. generator.add(format(w) + " w\n");
  92. float xm = x1 + (w / 2);
  93. generator.add(format(xm) + " " + format(y1) + " m "
  94. + format(xm) + " " + format(y2) + " l S\n");
  95. }
  96. break;
  97. case Constants.EN_DOTTED:
  98. generator.setColor(col, false);
  99. generator.add("1 J ");
  100. if (horz) {
  101. float unit = Math.abs(2 * h);
  102. int rep = (int)(w / unit);
  103. if (rep % 2 == 0) {
  104. rep++;
  105. }
  106. unit = w / rep;
  107. generator.add("[0 " + format(unit) + "] 0 d ");
  108. generator.add(format(h) + " w\n");
  109. float ym = y1 + (h / 2);
  110. generator.add(format(x1) + " " + format(ym) + " m "
  111. + format(x2) + " " + format(ym) + " l S\n");
  112. } else {
  113. float unit = Math.abs(2 * w);
  114. int rep = (int)(h / unit);
  115. if (rep % 2 == 0) {
  116. rep++;
  117. }
  118. unit = h / rep;
  119. generator.add("[0 " + format(unit) + " ] 0 d ");
  120. generator.add(format(w) + " w\n");
  121. float xm = x1 + (w / 2);
  122. generator.add(format(xm) + " " + format(y1) + " m "
  123. + format(xm) + " " + format(y2) + " l S\n");
  124. }
  125. break;
  126. case Constants.EN_DOUBLE:
  127. generator.setColor(col, false);
  128. generator.add("[] 0 d ");
  129. if (horz) {
  130. float h3 = h / 3;
  131. generator.add(format(h3) + " w\n");
  132. float ym1 = y1 + (h3 / 2);
  133. float ym2 = ym1 + h3 + h3;
  134. generator.add(format(x1) + " " + format(ym1) + " m "
  135. + format(x2) + " " + format(ym1) + " l S\n");
  136. generator.add(format(x1) + " " + format(ym2) + " m "
  137. + format(x2) + " " + format(ym2) + " l S\n");
  138. } else {
  139. float w3 = w / 3;
  140. generator.add(format(w3) + " w\n");
  141. float xm1 = x1 + (w3 / 2);
  142. float xm2 = xm1 + w3 + w3;
  143. generator.add(format(xm1) + " " + format(y1) + " m "
  144. + format(xm1) + " " + format(y2) + " l S\n");
  145. generator.add(format(xm2) + " " + format(y1) + " m "
  146. + format(xm2) + " " + format(y2) + " l S\n");
  147. }
  148. break;
  149. case Constants.EN_GROOVE:
  150. case Constants.EN_RIDGE:
  151. colFactor = (style == Constants.EN_GROOVE ? 0.4f : -0.4f);
  152. generator.add("[] 0 d ");
  153. if (horz) {
  154. Color uppercol = ColorUtil.lightenColor(col, -colFactor);
  155. Color lowercol = ColorUtil.lightenColor(col, colFactor);
  156. float h3 = h / 3;
  157. generator.add(format(h3) + " w\n");
  158. float ym1 = y1 + (h3 / 2);
  159. generator.setColor(uppercol, false);
  160. generator.add(format(x1) + " " + format(ym1) + " m "
  161. + format(x2) + " " + format(ym1) + " l S\n");
  162. generator.setColor(col, false);
  163. generator.add(format(x1) + " " + format(ym1 + h3) + " m "
  164. + format(x2) + " " + format(ym1 + h3) + " l S\n");
  165. generator.setColor(lowercol, false);
  166. generator.add(format(x1) + " " + format(ym1 + h3 + h3) + " m "
  167. + format(x2) + " " + format(ym1 + h3 + h3) + " l S\n");
  168. } else {
  169. Color leftcol = ColorUtil.lightenColor(col, -colFactor);
  170. Color rightcol = ColorUtil.lightenColor(col, colFactor);
  171. float w3 = w / 3;
  172. generator.add(format(w3) + " w\n");
  173. float xm1 = x1 + (w3 / 2);
  174. generator.setColor(leftcol, false);
  175. generator.add(format(xm1) + " " + format(y1) + " m "
  176. + format(xm1) + " " + format(y2) + " l S\n");
  177. generator.setColor(col, false);
  178. generator.add(format(xm1 + w3) + " " + format(y1) + " m "
  179. + format(xm1 + w3) + " " + format(y2) + " l S\n");
  180. generator.setColor(rightcol, false);
  181. generator.add(format(xm1 + w3 + w3) + " " + format(y1) + " m "
  182. + format(xm1 + w3 + w3) + " " + format(y2) + " l S\n");
  183. }
  184. break;
  185. case Constants.EN_INSET:
  186. case Constants.EN_OUTSET:
  187. colFactor = (style == Constants.EN_OUTSET ? 0.4f : -0.4f);
  188. generator.add("[] 0 d ");
  189. Color c = col;
  190. if (horz) {
  191. c = ColorUtil.lightenColor(c, (startOrBefore ? 1 : -1) * colFactor);
  192. generator.add(format(h) + " w\n");
  193. float ym1 = y1 + (h / 2);
  194. generator.setColor(c, false);
  195. generator.add(format(x1) + " " + format(ym1) + " m "
  196. + format(x2) + " " + format(ym1) + " l S\n");
  197. } else {
  198. c = ColorUtil.lightenColor(c, (startOrBefore ? 1 : -1) * colFactor);
  199. generator.add(format(w) + " w\n");
  200. float xm1 = x1 + (w / 2);
  201. generator.setColor(c, false);
  202. generator.add(format(xm1) + " " + format(y1) + " m "
  203. + format(xm1) + " " + format(y2) + " l S\n");
  204. }
  205. break;
  206. case Constants.EN_HIDDEN:
  207. break;
  208. default:
  209. generator.setColor(col, false);
  210. generator.add("[] 0 d ");
  211. if (horz) {
  212. generator.add(format(h) + " w\n");
  213. float ym = y1 + (h / 2);
  214. generator.add(format(x1) + " " + format(ym) + " m "
  215. + format(x2) + " " + format(ym) + " l S\n");
  216. } else {
  217. generator.add(format(w) + " w\n");
  218. float xm = x1 + (w / 2);
  219. generator.add(format(xm) + " " + format(y1) + " m "
  220. + format(xm) + " " + format(y2) + " l S\n");
  221. }
  222. }
  223. }
  224. /** {@inheritDoc} */
  225. public void drawLine(Point start, Point end,
  226. int width, Color color, RuleStyle style) {
  227. if (start.y != end.y) {
  228. //TODO Support arbitrary lines if necessary
  229. throw new UnsupportedOperationException(
  230. "Can only deal with horizontal lines right now");
  231. }
  232. saveGraphicsState();
  233. int half = width / 2;
  234. int starty = start.y - half;
  235. Rectangle boundingRect = new Rectangle(start.x, start.y - half, end.x - start.x, width);
  236. switch (style.getEnumValue()) {
  237. case Constants.EN_SOLID:
  238. case Constants.EN_DASHED:
  239. case Constants.EN_DOUBLE:
  240. drawBorderLine(start.x, start.y - half, end.x, end.y + half,
  241. true, true, style.getEnumValue(), color);
  242. break;
  243. case Constants.EN_DOTTED:
  244. generator.clipRect(boundingRect);
  245. //This displaces the dots to the right by half a dot's width
  246. //TODO There's room for improvement here
  247. generator.add("1 0 0 1 " + format(half) + " 0 cm\n");
  248. drawBorderLine(start.x, start.y - half, end.x, end.y + half,
  249. true, true, style.getEnumValue(), color);
  250. break;
  251. case Constants.EN_GROOVE:
  252. case Constants.EN_RIDGE:
  253. generator.setColor(ColorUtil.lightenColor(color, 0.6f), true);
  254. generator.add(format(start.x) + " " + format(starty) + " m\n");
  255. generator.add(format(end.x) + " " + format(starty) + " l\n");
  256. generator.add(format(end.x) + " " + format(starty + 2 * half) + " l\n");
  257. generator.add(format(start.x) + " " + format(starty + 2 * half) + " l\n");
  258. generator.add("h\n");
  259. generator.add("f\n");
  260. generator.setColor(color, true);
  261. if (style == RuleStyle.GROOVE) {
  262. generator.add(format(start.x) + " " + format(starty) + " m\n");
  263. generator.add(format(end.x) + " " + format(starty) + " l\n");
  264. generator.add(format(end.x) + " " + format(starty + half) + " l\n");
  265. generator.add(format(start.x + half) + " " + format(starty + half) + " l\n");
  266. generator.add(format(start.x) + " " + format(starty + 2 * half) + " l\n");
  267. } else {
  268. generator.add(format(end.x) + " " + format(starty) + " m\n");
  269. generator.add(format(end.x) + " " + format(starty + 2 * half) + " l\n");
  270. generator.add(format(start.x) + " " + format(starty + 2 * half) + " l\n");
  271. generator.add(format(start.x) + " " + format(starty + half) + " l\n");
  272. generator.add(format(end.x - half) + " " + format(starty + half) + " l\n");
  273. }
  274. generator.add("h\n");
  275. generator.add("f\n");
  276. break;
  277. default:
  278. throw new UnsupportedOperationException("rule style not supported");
  279. }
  280. restoreGraphicsState();
  281. }
  282. static final String format(int coordinate) {
  283. //TODO lose scale?
  284. return format(coordinate / 1000f);
  285. }
  286. static final String format(float coordinate) {
  287. return PDFContentGenerator.format(coordinate);
  288. }
  289. /** {@inheritDoc} */
  290. protected void moveTo(int x, int y) {
  291. generator.add(format(x) + " " + format(y) + " m ");
  292. }
  293. /** {@inheritDoc} */
  294. protected void lineTo(int x, int y) {
  295. generator.add(format(x) + " " + format(y) + " l ");
  296. }
  297. /** {@inheritDoc} */
  298. protected void closePath() {
  299. generator.add("h ");
  300. }
  301. /** {@inheritDoc} */
  302. protected void clip() {
  303. generator.add("W\n" + "n\n");
  304. }
  305. /** {@inheritDoc} */
  306. protected void saveGraphicsState() {
  307. generator.add("q\n");
  308. }
  309. /** {@inheritDoc} */
  310. protected void restoreGraphicsState() {
  311. generator.add("Q\n");
  312. }
  313. /** {@inheritDoc} */
  314. protected void cubicBezierTo(int p1x, int p1y, int p2x, int p2y, int p3x, int p3y) {
  315. generator.add(format(p1x) + " " + format(p1y) + " " + format(p2x) + " " + format(p2y)
  316. + " " + format(p3x) + " " + format(p3y) + " c ");
  317. }
  318. private void transformCoordinates(int a, int b, int c, int d, int e, int f) {
  319. generator.add( "" + format(a) + " " + format(b) + " " + format(c) + " " + format(d)
  320. + " " + format(e) + " " + format(f) + " cm ");
  321. }
  322. private void transformCoordinates2(float a, float b, float c, float d, float e, float f) {
  323. generator.add( "" + format(a) + " " + format(b) + " " + format(c) + " " + format(d)
  324. + " " + format(e) + " " + format(f) + " cm ");
  325. }
  326. /** {@inheritDoc} */
  327. protected void rotateCoordinates(double angle) throws IOException {
  328. float s = (float)Math.sin(angle);
  329. float c = (float)Math.cos(angle);
  330. transformCoordinates2(c, s, -s, c, 0, 0);
  331. }
  332. /** {@inheritDoc} */
  333. protected void translateCoordinates(int xTranslate, int yTranslate) throws IOException {
  334. transformCoordinates(1000, 0, 0, 1000, xTranslate, yTranslate);
  335. }
  336. /** {@inheritDoc} */
  337. protected void scaleCoordinates(float xScale, float yScale) throws IOException {
  338. transformCoordinates2(xScale, 0, 0, yScale, 0, 0);
  339. }
  340. }