gdevpdfd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gdevpdfd.c,v 1.16 2001/07/27 22:28:31 lpd Exp $ */
  16. /* Path drawing procedures for pdfwrite driver */
  17. #include "math_.h"
  18. #include "gx.h"
  19. #include "gxdevice.h"
  20. #include "gxfixed.h"
  21. #include "gxistate.h"
  22. #include "gxpaint.h"
  23. #include "gzpath.h"
  24. #include "gzcpath.h"
  25. #include "gdevpdfx.h"
  26. #include "gdevpdfg.h"
  27. /* ---------------- Drawing ---------------- */
  28. /* Fill a rectangle. */
  29. int
  30. gdev_pdf_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  31. gx_color_index color)
  32. {
  33. gx_device_pdf *pdev = (gx_device_pdf *) dev;
  34. int code;
  35. /* Make a special check for the initial fill with white, */
  36. /* which shouldn't cause the page to be opened. */
  37. if (color == pdev->white && !is_in_page(pdev))
  38. return 0;
  39. code = pdf_open_page(pdev, PDF_IN_STREAM);
  40. if (code < 0)
  41. return code;
  42. /* Make sure we aren't being clipped. */
  43. pdf_put_clip_path(pdev, NULL);
  44. pdf_set_pure_color(pdev, color, &pdev->fill_color,
  45. &psdf_set_fill_color_commands);
  46. pprintd4(pdev->strm, "%d %d %d %d re f\n", x, y, w, h);
  47. return 0;
  48. }
  49. /* ---------------- Path drawing ---------------- */
  50. /* ------ Vector device implementation ------ */
  51. private int
  52. pdf_setlinewidth(gx_device_vector * vdev, floatp width)
  53. {
  54. /* Acrobat Reader doesn't accept negative line widths. */
  55. return psdf_setlinewidth(vdev, fabs(width));
  56. }
  57. private int
  58. pdf_setfillcolor(gx_device_vector * vdev, const gx_drawing_color * pdc)
  59. {
  60. gx_device_pdf *const pdev = (gx_device_pdf *)vdev;
  61. return pdf_set_drawing_color(pdev, pdc, &pdev->fill_color,
  62. &psdf_set_fill_color_commands);
  63. }
  64. private int
  65. pdf_setstrokecolor(gx_device_vector * vdev, const gx_drawing_color * pdc)
  66. {
  67. gx_device_pdf *const pdev = (gx_device_pdf *)vdev;
  68. return pdf_set_drawing_color(pdev, pdc, &pdev->stroke_color,
  69. &psdf_set_stroke_color_commands);
  70. }
  71. private int
  72. pdf_dorect(gx_device_vector * vdev, fixed x0, fixed y0, fixed x1, fixed y1,
  73. gx_path_type_t type)
  74. {
  75. fixed xmax = int2fixed(vdev->width), ymax = int2fixed(vdev->height);
  76. fixed xmin = 0, ymin = 0;
  77. /*
  78. * If we're doing a stroke operation, expand the checking box by the
  79. * stroke width.
  80. */
  81. if (type & gx_path_type_stroke) {
  82. double w = vdev->state.line_params.half_width;
  83. double xw = w * (fabs(vdev->state.ctm.xx) + fabs(vdev->state.ctm.yx));
  84. double yw = w * (fabs(vdev->state.ctm.xy) + fabs(vdev->state.ctm.yy));
  85. xmin = -(float2fixed(xw) + fixed_1);
  86. xmax -= xmin;
  87. ymin = -(float2fixed(yw) + fixed_1);
  88. ymax -= ymin;
  89. }
  90. if (!(type & gx_path_type_clip) &&
  91. (x0 > xmax || x1 < xmin || y0 > ymax || y1 < ymin ||
  92. x0 > x1 || y0 > y1)
  93. )
  94. return 0; /* nothing to fill or stroke */
  95. /*
  96. * Clamp coordinates to avoid tripping over Acrobat Reader's limit
  97. * of 32K on user coordinate values.
  98. */
  99. if (x0 < xmin)
  100. x0 = xmin;
  101. if (x1 > xmax)
  102. x1 = xmax;
  103. if (y0 < ymin)
  104. y0 = ymin;
  105. if (y1 > ymax)
  106. y1 = ymax;
  107. return psdf_dorect(vdev, x0, y0, x1, y1, type);
  108. }
  109. private int
  110. pdf_endpath(gx_device_vector * vdev, gx_path_type_t type)
  111. {
  112. return 0; /* always handled by caller */
  113. }
  114. const gx_device_vector_procs pdf_vector_procs = {
  115. /* Page management */
  116. NULL,
  117. /* Imager state */
  118. pdf_setlinewidth,
  119. psdf_setlinecap,
  120. psdf_setlinejoin,
  121. psdf_setmiterlimit,
  122. psdf_setdash,
  123. psdf_setflat,
  124. psdf_setlogop,
  125. /* Other state */
  126. pdf_setfillcolor,
  127. pdf_setstrokecolor,
  128. /* Paths */
  129. psdf_dopath,
  130. pdf_dorect,
  131. psdf_beginpath,
  132. psdf_moveto,
  133. psdf_lineto,
  134. psdf_curveto,
  135. psdf_closepath,
  136. pdf_endpath
  137. };
  138. /* ------ Utilities ------ */
  139. /* Test whether we will need to put the clipping path. */
  140. bool
  141. pdf_must_put_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath)
  142. {
  143. if (pcpath == NULL)
  144. return pdev->clip_path_id != pdev->no_clip_path_id;
  145. if (pdev->clip_path_id == pcpath->id)
  146. return false;
  147. if (gx_cpath_includes_rectangle(pcpath, fixed_0, fixed_0,
  148. int2fixed(pdev->width),
  149. int2fixed(pdev->height))
  150. )
  151. return pdev->clip_path_id != pdev->no_clip_path_id;
  152. return true;
  153. }
  154. /* Put a clipping path on the output file. */
  155. int
  156. pdf_put_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath)
  157. {
  158. stream *s = pdev->strm;
  159. if (pcpath == NULL) {
  160. if (pdev->clip_path_id == pdev->no_clip_path_id)
  161. return 0;
  162. stream_puts(s, "Q\nq\n");
  163. pdev->clip_path_id = pdev->no_clip_path_id;
  164. } else {
  165. if (pdev->clip_path_id == pcpath->id)
  166. return 0;
  167. if (gx_cpath_includes_rectangle(pcpath, fixed_0, fixed_0,
  168. int2fixed(pdev->width),
  169. int2fixed(pdev->height))
  170. ) {
  171. if (pdev->clip_path_id == pdev->no_clip_path_id)
  172. return 0;
  173. stream_puts(s, "Q\nq\n");
  174. pdev->clip_path_id = pdev->no_clip_path_id;
  175. } else {
  176. gdev_vector_dopath_state_t state;
  177. gs_cpath_enum cenum;
  178. gs_fixed_point vs[3];
  179. int pe_op;
  180. stream_puts(s, "Q\nq\n");
  181. gdev_vector_dopath_init(&state, (gx_device_vector *)pdev,
  182. gx_path_type_fill, NULL);
  183. /*
  184. * We have to break 'const' here because the clip path
  185. * enumeration logic uses some internal mark bits.
  186. * This is very unfortunate, but until we can come up with
  187. * a better algorithm, it's necessary.
  188. */
  189. gx_cpath_enum_init(&cenum, (gx_clip_path *) pcpath);
  190. while ((pe_op = gx_cpath_enum_next(&cenum, vs)) > 0)
  191. gdev_vector_dopath_segment(&state, pe_op, vs);
  192. pprints1(s, "%s n\n", (pcpath->rule <= 0 ? "W" : "W*"));
  193. if (pe_op < 0)
  194. return pe_op;
  195. pdev->clip_path_id = pcpath->id;
  196. }
  197. }
  198. pdev->text.font = 0;
  199. if (pdev->context == PDF_IN_TEXT)
  200. pdev->context = PDF_IN_STREAM;
  201. pdf_reset_graphics(pdev);
  202. return 0;
  203. }
  204. /*
  205. * Compute the scaling to ensure that user coordinates for a path are within
  206. * Acrobat's range. Return true if scaling was needed. In this case, the
  207. * CTM will be multiplied by *pscale, and all coordinates will be divided by
  208. * *pscale.
  209. */
  210. private bool
  211. make_path_scaling(const gx_device_pdf *pdev, gx_path *ppath,
  212. floatp prescale, double *pscale)
  213. {
  214. gs_fixed_rect bbox;
  215. double bmin, bmax;
  216. gx_path_bbox(ppath, &bbox);
  217. bmin = min(bbox.p.x / pdev->scale.x, bbox.p.y / pdev->scale.y) * prescale;
  218. bmax = max(bbox.q.x / pdev->scale.x, bbox.q.y / pdev->scale.y) * prescale;
  219. if (bmin <= int2fixed(-MAX_USER_COORD) ||
  220. bmax > int2fixed(MAX_USER_COORD)
  221. ) {
  222. /* Rescale the path. */
  223. *pscale = max(bmin / int2fixed(-MAX_USER_COORD),
  224. bmax / int2fixed(MAX_USER_COORD));
  225. return true;
  226. } else {
  227. *pscale = 1;
  228. return false;
  229. }
  230. #undef MAX_USER_COORD
  231. }
  232. /* ------ Driver procedures ------ */
  233. /* Fill a path. */
  234. int
  235. gdev_pdf_fill_path(gx_device * dev, const gs_imager_state * pis, gx_path * ppath,
  236. const gx_fill_params * params,
  237. const gx_drawing_color * pdcolor, const gx_clip_path * pcpath)
  238. {
  239. gx_device_pdf *pdev = (gx_device_pdf *) dev;
  240. int code;
  241. /*
  242. * HACK: we fill an empty path in order to set the clipping path
  243. * and the color for writing text. If it weren't for this, we
  244. * could detect and skip empty paths before putting out the clip
  245. * path or the color. We also clip with an empty path in order
  246. * to advance currentpoint for show operations without actually
  247. * drawing anything.
  248. */
  249. bool have_path;
  250. /*
  251. * Check for an empty clipping path.
  252. */
  253. if (pcpath) {
  254. gs_fixed_rect box;
  255. gx_cpath_outer_box(pcpath, &box);
  256. if (box.p.x >= box.q.x || box.p.y >= box.q.y)
  257. return 0; /* empty clipping path */
  258. }
  259. code = pdf_prepare_fill(pdev, pis);
  260. if (code < 0)
  261. return code;
  262. if (gx_dc_is_pure(pdcolor)) {
  263. /*
  264. * Make a special check for the initial fill with white,
  265. * which shouldn't cause the page to be opened.
  266. */
  267. if (gx_dc_pure_color(pdcolor) == pdev->white && !is_in_page(pdev))
  268. return 0;
  269. }
  270. have_path = !gx_path_is_void(ppath);
  271. if (have_path || pdev->context == PDF_IN_NONE ||
  272. pdf_must_put_clip_path(pdev, pcpath)
  273. ) {
  274. code = pdf_open_page(pdev, PDF_IN_STREAM);
  275. if (code < 0)
  276. return code;
  277. }
  278. pdf_put_clip_path(pdev, pcpath);
  279. if (pdf_setfillcolor((gx_device_vector *)pdev, pdcolor) < 0)
  280. return gx_default_fill_path(dev, pis, ppath, params, pdcolor,
  281. pcpath);
  282. if (have_path) {
  283. stream *s = pdev->strm;
  284. double scale;
  285. gs_matrix smat;
  286. gs_matrix *psmat = NULL;
  287. if (params->flatness != pdev->state.flatness) {
  288. pprintg1(s, "%g i\n", params->flatness);
  289. pdev->state.flatness = params->flatness;
  290. }
  291. if (make_path_scaling(pdev, ppath, 1.0, &scale)) {
  292. gs_make_scaling(pdev->scale.x * scale, pdev->scale.y * scale,
  293. &smat);
  294. pdf_put_matrix(pdev, "q ", &smat, "cm\n");
  295. psmat = &smat;
  296. }
  297. gdev_vector_dopath((gx_device_vector *)pdev, ppath,
  298. gx_path_type_fill | gx_path_type_optimize,
  299. psmat);
  300. stream_puts(s, (params->rule < 0 ? "f\n" : "f*\n"));
  301. if (psmat)
  302. stream_puts(s, "Q\n");
  303. }
  304. return 0;
  305. }
  306. /* Stroke a path. */
  307. int
  308. gdev_pdf_stroke_path(gx_device * dev, const gs_imager_state * pis,
  309. gx_path * ppath, const gx_stroke_params * params,
  310. const gx_drawing_color * pdcolor, const gx_clip_path * pcpath)
  311. {
  312. gx_device_pdf *pdev = (gx_device_pdf *) dev;
  313. stream *s;
  314. int code;
  315. double scale, path_scale;
  316. bool set_ctm;
  317. gs_matrix mat;
  318. double prescale = 1;
  319. if (gx_path_is_void(ppath))
  320. return 0; /* won't mark the page */
  321. code = pdf_prepare_stroke(pdev, pis);
  322. if (code < 0)
  323. return code;
  324. code = pdf_open_page(pdev, PDF_IN_STREAM);
  325. if (code < 0)
  326. return code;
  327. /*
  328. * If the CTM is not uniform, stroke width depends on angle.
  329. * We'd like to avoid resetting the CTM, so we check for uniform
  330. * CTMs explicitly. Note that in PDF, unlike PostScript, it is
  331. * the CTM at the time of the stroke operation, not the CTM at
  332. * the time the path was constructed, that is used for transforming
  333. * the points of the path; so if we have to reset the CTM, we must
  334. * do it before constructing the path, and inverse-transform all
  335. * the coordinates.
  336. */
  337. set_ctm = (bool)gdev_vector_stroke_scaling((gx_device_vector *)pdev,
  338. pis, &scale, &mat);
  339. if (set_ctm) {
  340. /*
  341. * We want a scaling factor that will bring the largest reasonable
  342. * user coordinate within bounds. We choose a factor based on the
  343. * minor axis of the transformation. Thanks to Raph Levien for
  344. * the following formula.
  345. */
  346. double a = mat.xx, b = mat.xy, c = mat.yx, d = mat.yy;
  347. double u = fabs(a * d - b * c);
  348. double v = a * a + b * b + c * c + d * d;
  349. double minor = (sqrt(v + 2 * u) - sqrt(v - 2 * u)) * 0.5;
  350. prescale = (minor == 0 || minor > 1 ? 1 : 1 / minor);
  351. }
  352. if (make_path_scaling(pdev, ppath, prescale, &path_scale)) {
  353. scale /= path_scale;
  354. if (set_ctm)
  355. gs_matrix_scale(&mat, path_scale, path_scale, &mat);
  356. else {
  357. gs_make_scaling(path_scale, path_scale, &mat);
  358. set_ctm = true;
  359. }
  360. }
  361. pdf_put_clip_path(pdev, pcpath);
  362. code = gdev_vector_prepare_stroke((gx_device_vector *)pdev, pis, params,
  363. pdcolor, scale);
  364. if (code < 0)
  365. return gx_default_stroke_path(dev, pis, ppath, params, pdcolor,
  366. pcpath);
  367. if (set_ctm)
  368. pdf_put_matrix(pdev, "q ", &mat, "cm\n");
  369. code = gdev_vector_dopath((gx_device_vector *)pdev, ppath,
  370. gx_path_type_stroke | gx_path_type_optimize,
  371. (set_ctm ? &mat : (const gs_matrix *)0));
  372. if (code < 0)
  373. return code;
  374. s = pdev->strm;
  375. stream_puts(s, (code ? "s" : "S"));
  376. stream_puts(s, (set_ctm ? " Q\n" : "\n"));
  377. return 0;
  378. }