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 14KB

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