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

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