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.

PDFGraphicsPainter.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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.fop.fo.Constants;
  24. import org.apache.fop.render.intermediate.ArcToBezierCurveTransformer;
  25. import org.apache.fop.render.intermediate.BezierCurvePainter;
  26. import org.apache.fop.render.intermediate.GraphicsPainter;
  27. import org.apache.fop.traits.RuleStyle;
  28. import org.apache.fop.util.ColorUtil;
  29. /**
  30. * PDF-specific implementation of the {@link GraphicsPainter}.
  31. */
  32. public class PDFGraphicsPainter implements GraphicsPainter, BezierCurvePainter {
  33. private final PDFContentGeneratorHelper generator;
  34. /** Used for drawing arcs since PS does not natively support drawing elliptic curves */
  35. private final ArcToBezierCurveTransformer arcToBezierCurveTransformer;
  36. public PDFGraphicsPainter(PDFContentGenerator generator) {
  37. this.generator = new PDFContentGeneratorHelper(generator);
  38. this.arcToBezierCurveTransformer = new ArcToBezierCurveTransformer(this);
  39. }
  40. /** {@inheritDoc} */
  41. public void drawBorderLine(int x1, int y1, int x2, int y2, boolean horz,
  42. boolean startOrBefore, int style, Color col) {
  43. //TODO lose scale?
  44. drawBorderLine2(x1 / 1000f, y1 / 1000f, x2 / 1000f, y2 / 1000f,
  45. horz, startOrBefore, style, col);
  46. }
  47. /** {@inheritDoc} */
  48. private void drawBorderLine2(float x1, float y1, float x2, float y2, boolean horz,
  49. boolean startOrBefore, int style, Color col) {
  50. float w = x2 - x1;
  51. float h = y2 - y1;
  52. float colFactor;
  53. switch (style) {
  54. case Constants.EN_DASHED:
  55. generator.setColor(col);
  56. if (horz) {
  57. float unit = Math.abs(2 * h);
  58. int rep = (int) (w / unit);
  59. if (rep % 2 == 0) {
  60. rep++;
  61. }
  62. unit = w / rep;
  63. float ym = y1 + (h / 2);
  64. generator.setDashLine(unit)
  65. .setLineWidth(h)
  66. .strokeLine(x1, ym, x2, ym);
  67. } else {
  68. float unit = Math.abs(2 * w);
  69. int rep = (int) (h / unit);
  70. if (rep % 2 == 0) {
  71. rep++;
  72. }
  73. unit = h / rep;
  74. float xm = x1 + (w / 2);
  75. generator.setDashLine(unit)
  76. .setLineWidth(w)
  77. .strokeLine(xm, y1, xm, y2);
  78. }
  79. break;
  80. case Constants.EN_DOTTED:
  81. generator.setColor(col).setRoundCap();
  82. if (horz) {
  83. float unit = Math.abs(2 * h);
  84. int rep = (int) (w / unit);
  85. if (rep % 2 == 0) {
  86. rep++;
  87. }
  88. unit = w / rep;
  89. float ym = y1 + (h / 2);
  90. generator.setDashLine(0, unit)
  91. .setLineWidth(h)
  92. .strokeLine(x1, ym, x2, ym);
  93. } else {
  94. float unit = Math.abs(2 * w);
  95. int rep = (int) (h / unit);
  96. if (rep % 2 == 0) {
  97. rep++;
  98. }
  99. unit = h / rep;
  100. float xm = x1 + (w / 2);
  101. generator.setDashLine(0, unit)
  102. .setLineWidth(w)
  103. .strokeLine(xm, y1, xm, y2);
  104. }
  105. break;
  106. case Constants.EN_DOUBLE:
  107. generator.setColor(col)
  108. .setSolidLine();
  109. if (horz) {
  110. float h3 = h / 3;
  111. float ym1 = y1 + (h3 / 2);
  112. float ym2 = ym1 + h3 + h3;
  113. generator.setLineWidth(h3)
  114. .strokeLine(x1, ym1, x2, ym1)
  115. .strokeLine(x1, ym2, x2, ym2);
  116. } else {
  117. float w3 = w / 3;
  118. float xm1 = x1 + (w3 / 2);
  119. float xm2 = xm1 + w3 + w3;
  120. generator.setLineWidth(w3)
  121. .strokeLine(xm1, y1, xm1, y2)
  122. .strokeLine(xm2, y1, xm2, y2);
  123. }
  124. break;
  125. case Constants.EN_GROOVE:
  126. case Constants.EN_RIDGE:
  127. colFactor = (style == Constants.EN_GROOVE ? 0.4f : -0.4f);
  128. generator.setSolidLine();
  129. if (horz) {
  130. Color uppercol = ColorUtil.lightenColor(col, -colFactor);
  131. Color lowercol = ColorUtil.lightenColor(col, colFactor);
  132. float h3 = h / 3;
  133. float ym1 = y1 + (h3 / 2);
  134. generator.setLineWidth(h3)
  135. .setColor(uppercol)
  136. .strokeLine(x1, ym1, x2, ym1)
  137. .setColor(col)
  138. .strokeLine(x1, ym1 + h3, x2, ym1 + h3)
  139. .setColor(lowercol)
  140. .strokeLine(x1, ym1 + h3 + h3, x2, ym1 + h3 + h3);
  141. } else {
  142. Color leftcol = ColorUtil.lightenColor(col, -colFactor);
  143. Color rightcol = ColorUtil.lightenColor(col, colFactor);
  144. float w3 = w / 3;
  145. float xm1 = x1 + (w3 / 2);
  146. generator.setLineWidth(w3)
  147. .setColor(leftcol)
  148. .strokeLine(xm1, y1, xm1, y2)
  149. .setColor(col)
  150. .strokeLine(xm1 + w3, y1, xm1 + w3, y2)
  151. .setColor(rightcol)
  152. .strokeLine(xm1 + w3 + w3, y1, xm1 + w3 + w3, y2);
  153. }
  154. break;
  155. case Constants.EN_INSET:
  156. case Constants.EN_OUTSET:
  157. colFactor = (style == Constants.EN_OUTSET ? 0.4f : -0.4f);
  158. generator.setSolidLine();
  159. Color c = col;
  160. if (horz) {
  161. c = ColorUtil.lightenColor(c, (startOrBefore ? 1 : -1) * colFactor);
  162. float ym1 = y1 + (h / 2);
  163. generator.setLineWidth(h)
  164. .setColor(c)
  165. .strokeLine(x1, ym1, x2, ym1);
  166. } else {
  167. c = ColorUtil.lightenColor(c, (startOrBefore ? 1 : -1) * colFactor);
  168. float xm1 = x1 + (w / 2);
  169. generator.setLineWidth(w)
  170. .setColor(c)
  171. .strokeLine(xm1, y1, xm1, y2);
  172. }
  173. break;
  174. case Constants.EN_HIDDEN:
  175. break;
  176. default:
  177. generator.setColor(col).setSolidLine();
  178. if (horz) {
  179. float ym = y1 + (h / 2);
  180. generator.setLineWidth(h)
  181. .strokeLine(x1, ym, x2, ym);
  182. } else {
  183. float xm = x1 + (w / 2);
  184. generator.setLineWidth(w)
  185. .strokeLine(xm, y1, xm, y2);
  186. }
  187. }
  188. }
  189. /** {@inheritDoc} */
  190. public void drawLine(Point start, Point end,
  191. int width, Color color, RuleStyle style) {
  192. if (start.y != end.y) {
  193. //TODO Support arbitrary lines if necessary
  194. throw new UnsupportedOperationException(
  195. "Can only deal with horizontal lines right now");
  196. }
  197. saveGraphicsState();
  198. int half = width / 2;
  199. int starty = start.y - half;
  200. Rectangle boundingRect = new Rectangle(start.x, start.y - half, end.x - start.x, width);
  201. switch (style.getEnumValue()) {
  202. case Constants.EN_SOLID:
  203. case Constants.EN_DASHED:
  204. case Constants.EN_DOUBLE:
  205. drawBorderLine(start.x, start.y - half, end.x, end.y + half,
  206. true, true, style.getEnumValue(), color);
  207. break;
  208. case Constants.EN_DOTTED:
  209. generator.clipRect(boundingRect)
  210. //This displaces the dots to the right by half a dot's width
  211. //TODO There's room for improvement here
  212. .transformCoordinatesLine(1, 0, 0 , 1, half, 0);
  213. drawBorderLine(start.x, start.y - half, end.x, end.y + half, true, true, style.getEnumValue(),
  214. color);
  215. break;
  216. case Constants.EN_GROOVE:
  217. case Constants.EN_RIDGE:
  218. generator.setFillColor(ColorUtil.lightenColor(color, 0.6f))
  219. .fillRect(start.x, start.y, end.x, starty + 2 * half)
  220. .setFillColor(color)
  221. .fillRidge(style, start.x, start.y, end.x, end.y, half);
  222. break;
  223. default:
  224. throw new UnsupportedOperationException("rule style not supported");
  225. }
  226. restoreGraphicsState();
  227. }
  228. private static String format(int coordinate) {
  229. //TODO lose scale?
  230. return format(coordinate / 1000f);
  231. }
  232. private static String format(float coordinate) {
  233. return PDFContentGenerator.format(coordinate);
  234. }
  235. /** {@inheritDoc} */
  236. public void moveTo(int x, int y) {
  237. generator.moveTo(x, y);
  238. }
  239. /** {@inheritDoc} */
  240. public void lineTo(int x, int y) {
  241. generator.lineTo(x, y);
  242. }
  243. /** {@inheritDoc} */
  244. public void arcTo(final double startAngle, final double endAngle, final int cx, final int cy,
  245. final int width, final int height) throws IOException {
  246. arcToBezierCurveTransformer.arcTo(startAngle, endAngle, cx, cy, width, height);
  247. }
  248. /** {@inheritDoc} */
  249. public void closePath() {
  250. generator.closePath();
  251. }
  252. /** {@inheritDoc} */
  253. public void clip() {
  254. generator.clip();
  255. }
  256. /** {@inheritDoc} */
  257. public void saveGraphicsState() {
  258. generator.saveGraphicsState();
  259. }
  260. /** {@inheritDoc} */
  261. public void restoreGraphicsState() {
  262. generator.restoreGraphicsState();
  263. }
  264. /** {@inheritDoc} */
  265. public void rotateCoordinates(double angle) throws IOException {
  266. float s = (float) Math.sin(angle);
  267. float c = (float) Math.cos(angle);
  268. generator.transformFloatCoordinates(c, s, -s, c, 0, 0);
  269. }
  270. /** {@inheritDoc} */
  271. public void translateCoordinates(int xTranslate, int yTranslate) throws IOException {
  272. generator.transformCoordinates(1000, 0, 0, 1000, xTranslate, yTranslate);
  273. }
  274. /** {@inheritDoc} */
  275. public void scaleCoordinates(float xScale, float yScale) throws IOException {
  276. generator.transformFloatCoordinates(xScale, 0, 0, yScale, 0, 0);
  277. }
  278. /** {@inheritDoc} */
  279. public void cubicBezierTo(int p1x, int p1y, int p2x, int p2y, int p3x, int p3y) {
  280. generator.cubicBezierTo(p1x, p1y, p2x, p2y, p3x, p3y);
  281. }
  282. // TODO consider enriching PDFContentGenerator with part of this API
  283. private static class PDFContentGeneratorHelper {
  284. private final PDFContentGenerator generator;
  285. public PDFContentGeneratorHelper(PDFContentGenerator generator) {
  286. this.generator = generator;
  287. }
  288. public PDFContentGeneratorHelper moveTo(int x, int y) {
  289. return add("m", format(x), format(y));
  290. }
  291. public PDFContentGeneratorHelper lineTo(int x, int y) {
  292. return add("l", format(x), format(y));
  293. }
  294. /** {@inheritDoc} */
  295. public PDFContentGeneratorHelper cubicBezierTo(int p1x, int p1y, int p2x, int p2y, int p3x, int p3y) {
  296. return add("c", format(p1x), format(p1y), format(p2x), format(p2y), format(p3x), format(p3y));
  297. }
  298. public PDFContentGeneratorHelper closePath() {
  299. return add("h");
  300. }
  301. public PDFContentGeneratorHelper clip() {
  302. return addLine("W\nn");
  303. }
  304. public PDFContentGeneratorHelper clipRect(Rectangle rectangle) {
  305. generator.clipRect(rectangle);
  306. return this;
  307. }
  308. public PDFContentGeneratorHelper saveGraphicsState() {
  309. return addLine("q");
  310. }
  311. public PDFContentGeneratorHelper restoreGraphicsState() {
  312. return addLine("Q");
  313. }
  314. public PDFContentGeneratorHelper setSolidLine() {
  315. generator.add("[] 0 d ");
  316. return this;
  317. }
  318. public PDFContentGeneratorHelper setRoundCap() {
  319. return add("J", "1");
  320. }
  321. public PDFContentGeneratorHelper strokeLine(float xStart, float yStart, float xEnd, float yEnd) {
  322. add("m", xStart, yStart);
  323. return addLine("l S", xEnd, yEnd);
  324. }
  325. public PDFContentGeneratorHelper fillRect(int xStart, int yStart, int xEnd, int yEnd) {
  326. String xS = format(xStart);
  327. String xE = format(xEnd);
  328. String yS = format(yStart);
  329. String yE = format(yEnd);
  330. return addLine("m", xS, yS)
  331. .addLine("l", xE, yS)
  332. .addLine("l", xE, yE)
  333. .addLine("l", xS, yE)
  334. .addLine("h")
  335. .addLine("f");
  336. }
  337. public PDFContentGeneratorHelper fillRidge(RuleStyle style, int xStart, int yStart, int xEnd,
  338. int yEnd, int half) {
  339. String xS = format(xStart);
  340. String xE = format(xEnd);
  341. String yS = format(yStart);
  342. if (style == RuleStyle.GROOVE) {
  343. addLine("m", xS, yS)
  344. .addLine("l", xE, yS)
  345. .addLine("l", xE, format(yStart + half))
  346. .addLine("l", format(xStart + half), format(yStart + half))
  347. .addLine("l", xS, format(yStart + 2 * half));
  348. } else {
  349. addLine("m", xE, yS)
  350. .addLine("l", xE, format(yStart + 2 * half))
  351. .addLine("l", xS, format(yStart + 2 * half))
  352. .addLine("l", xS, format(yStart + half))
  353. .addLine("l", format(xEnd - half), format(yStart + half));
  354. }
  355. return addLine("h").addLine("f");
  356. }
  357. public PDFContentGeneratorHelper setLineWidth(float width) {
  358. return addLine("w", width);
  359. }
  360. public PDFContentGeneratorHelper setDashLine(float first, float... rest) {
  361. StringBuilder sb = new StringBuilder();
  362. sb.append("[").append(format(first));
  363. for (float unit : rest) {
  364. sb.append(" ").append(format(unit));
  365. }
  366. sb.append("] 0 d ");
  367. generator.add(sb.toString());
  368. return this;
  369. }
  370. public PDFContentGeneratorHelper setColor(Color col) {
  371. generator.setColor(col, false);
  372. return this;
  373. }
  374. public PDFContentGeneratorHelper setFillColor(Color col) {
  375. generator.setColor(col, true);
  376. return this;
  377. }
  378. public PDFContentGeneratorHelper transformFloatCoordinates(float a, float b, float c, float d,
  379. float e, float f) {
  380. return add("cm", a, b, c, d, e, f);
  381. }
  382. public PDFContentGeneratorHelper transformCoordinates(int a, int b, int c, int d, int e, int f) {
  383. return add("cm", format(a), format(b), format(c), format(d), format(e), format(f));
  384. }
  385. public PDFContentGeneratorHelper transformCoordinatesLine(int a, int b, int c, int d, int e, int f) {
  386. return addLine("cm", format(a), format(b), format(c), format(d), format(e), format(f));
  387. }
  388. public PDFContentGeneratorHelper add(String op) {
  389. assert op.equals(op.trim());
  390. generator.add(op + " ");
  391. return this;
  392. }
  393. private PDFContentGeneratorHelper add(String op, String... args) {
  394. add(createArgs(args), op);
  395. return this;
  396. }
  397. public PDFContentGeneratorHelper addLine(String op) {
  398. assert op.equals(op.trim());
  399. generator.add(op + "\n");
  400. return this;
  401. }
  402. public PDFContentGeneratorHelper addLine(String op, String... args) {
  403. addLine(createArgs(args), op);
  404. return this;
  405. }
  406. private PDFContentGeneratorHelper add(String op, float... args) {
  407. add(createArgs(args), op);
  408. return this;
  409. }
  410. public PDFContentGeneratorHelper addLine(String op, float... args) {
  411. addLine(createArgs(args), op);
  412. return this;
  413. }
  414. private StringBuilder createArgs(float... args) {
  415. StringBuilder sb = new StringBuilder();
  416. for (float arg : args) {
  417. sb.append(format(arg)).append(" ");
  418. }
  419. return sb;
  420. }
  421. private StringBuilder createArgs(String... args) {
  422. StringBuilder sb = new StringBuilder();
  423. for (String arg : args) {
  424. sb.append(arg).append(" ");
  425. }
  426. return sb;
  427. }
  428. private void add(StringBuilder args, String op) {
  429. assert op.equals(op.trim());
  430. generator.add(args.append(op).append(" ").toString());
  431. }
  432. private void addLine(StringBuilder args, String op) {
  433. assert op.equals(op.trim());
  434. generator.add(args.append(op).append("\n").toString());
  435. }
  436. }
  437. }