gdevpdfb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /* Copyright (C) 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gdevpdfb.c,v 1.34 2005/10/10 19:09:30 leonardo Exp $ */
  14. /* Low-level bitmap image handling for PDF-writing driver */
  15. #include "string_.h"
  16. #include "gx.h"
  17. #include "gserrors.h"
  18. #include "gdevpdfx.h"
  19. #include "gdevpdfg.h"
  20. #include "gdevpdfo.h" /* for data stream */
  21. #include "gxcspace.h"
  22. #include "gxdcolor.h"
  23. #include "gxpcolor.h"
  24. #include "gxhldevc.h"
  25. /* We need this color space type for constructing temporary color spaces. */
  26. extern const gs_color_space_type gs_color_space_type_Indexed;
  27. /* ---------------- Utilities ---------------- */
  28. /* Fill in the image parameters for a bitmap image. */
  29. private void
  30. pdf_make_bitmap_image(gs_image_t * pim, int x, int y, int w, int h)
  31. {
  32. pim->Width = w;
  33. pim->Height = h;
  34. pdf_make_bitmap_matrix(&pim->ImageMatrix, x, y, w, h, h);
  35. }
  36. /* ---------------- Driver procedures ---------------- */
  37. /* Copy a mask bitmap. for_pattern = -1 means put the image in-line, */
  38. /* 1 means put the image in a resource. */
  39. private int
  40. pdf_copy_mask_data(gx_device_pdf * pdev, const byte * base, int sourcex,
  41. int raster, gx_bitmap_id id, int x, int y, int w, int h,
  42. gs_image_t *pim, pdf_image_writer *piw,
  43. int for_pattern)
  44. {
  45. ulong nbytes;
  46. int code;
  47. const byte *row_base;
  48. int row_step;
  49. bool in_line;
  50. gs_image_t_init_mask(pim, true);
  51. pdf_make_bitmap_image(pim, x, y, w, h);
  52. nbytes = ((ulong)w * h + 7) / 8;
  53. if (for_pattern) {
  54. /*
  55. * Patterns must be emitted in order of increasing user Y, i.e.,
  56. * the opposite of PDF's standard image order.
  57. */
  58. row_base = base + (h - 1) * raster;
  59. row_step = -raster;
  60. in_line = for_pattern < 0;
  61. } else {
  62. row_base = base;
  63. row_step = raster;
  64. in_line = nbytes < pdev->MaxInlineImageSize;
  65. pdf_put_image_matrix(pdev, &pim->ImageMatrix, 1.0);
  66. /*
  67. * Check whether we've already made an XObject resource for this
  68. * image.
  69. */
  70. if (id != gx_no_bitmap_id) {
  71. piw->pres = pdf_find_resource_by_gs_id(pdev, resourceXObject, id);
  72. if (piw->pres)
  73. return 0;
  74. }
  75. }
  76. /*
  77. * We have to be able to control whether to put Pattern images in line,
  78. * to avoid trying to create an XObject resource while we're in the
  79. * middle of writing a Pattern resource.
  80. */
  81. if (for_pattern < 0)
  82. stream_puts(pdev->strm, "q ");
  83. pdf_image_writer_init(piw);
  84. if ((code = pdf_begin_write_image(pdev, piw, id, w, h, NULL, in_line)) < 0 ||
  85. (code = psdf_setup_lossless_filters((gx_device_psdf *) pdev,
  86. &piw->binary[0],
  87. (gs_pixel_image_t *)pim)) < 0 ||
  88. (code = pdf_begin_image_data(pdev, piw, (const gs_pixel_image_t *)pim,
  89. NULL, 0)) < 0
  90. )
  91. return code;
  92. pdf_copy_mask_bits(piw->binary[0].strm, row_base, sourcex, row_step, w, h, 0);
  93. pdf_end_image_binary(pdev, piw, piw->height);
  94. return pdf_end_write_image(pdev, piw);
  95. }
  96. private void
  97. set_image_color(gx_device_pdf *pdev, gx_color_index c)
  98. {
  99. pdf_set_pure_color(pdev, c, &pdev->saved_fill_color,
  100. &pdev->fill_used_process_color,
  101. &psdf_set_fill_color_commands);
  102. if (!pdev->HaveStrokeColor)
  103. pdf_set_pure_color(pdev, c, &pdev->saved_stroke_color,
  104. &pdev->stroke_used_process_color,
  105. &psdf_set_stroke_color_commands);
  106. }
  107. /* Copy a monochrome bitmap or mask. */
  108. private int
  109. pdf_copy_mono(gx_device_pdf *pdev,
  110. const byte *base, int sourcex, int raster, gx_bitmap_id id,
  111. int x, int y, int w, int h, gx_color_index zero,
  112. gx_color_index one, const gx_clip_path *pcpath)
  113. {
  114. int code;
  115. gs_color_space cs;
  116. cos_value_t cs_value;
  117. cos_value_t *pcsvalue;
  118. byte palette[arch_sizeof_color_index * 2];
  119. gs_image_t image;
  120. pdf_image_writer writer;
  121. pdf_stream_position_t ipos;
  122. pdf_resource_t *pres = 0;
  123. byte invert = 0;
  124. bool in_line = false;
  125. /* Update clipping. */
  126. if (pdf_must_put_clip_path(pdev, pcpath)) {
  127. code = pdf_open_page(pdev, PDF_IN_STREAM);
  128. if (code < 0)
  129. return code;
  130. code = pdf_put_clip_path(pdev, pcpath);
  131. if (code < 0)
  132. return code;
  133. }
  134. /* We have 3 cases: mask, inverse mask, and solid. */
  135. if (zero == gx_no_color_index) {
  136. if (one == gx_no_color_index)
  137. return 0;
  138. /* If a mask has an id, assume it's a character. */
  139. if (id != gx_no_bitmap_id && sourcex == 0) {
  140. pres = pdf_find_resource_by_gs_id(pdev, resourceCharProc, id);
  141. if (pres == 0) { /* Define the character in an embedded font. */
  142. pdf_char_proc_t *pcp;
  143. double x_offset;
  144. int y_offset;
  145. gs_image_t_init_mask(&image, false);
  146. invert = 0xff;
  147. pdf_make_bitmap_image(&image, x, y, w, h);
  148. y_offset = pdf_char_image_y_offset(pdev, x, y, h);
  149. /*
  150. * The Y axis of the text matrix is inverted,
  151. * so we need to negate the Y offset appropriately.
  152. */
  153. code = pdf_begin_char_proc(pdev, w, h, 0, y_offset, id,
  154. &pcp, &ipos);
  155. if (code < 0)
  156. return code;
  157. y_offset = -y_offset;
  158. x_offset = psdf_round(pdev->char_width.x, 100, 10); /* See
  159. pdf_write_Widths about rounding. We need to provide
  160. a compatible data for Tj. */
  161. pprintg1(pdev->strm, "%g ", x_offset);
  162. pprintd3(pdev->strm, "0 0 %d %d %d d1\n", y_offset, w, h + y_offset);
  163. pprintd3(pdev->strm, "%d 0 0 %d 0 %d cm\n", w, h,
  164. y_offset);
  165. pdf_image_writer_init(&writer);
  166. code = pdf_begin_write_image(pdev, &writer, gs_no_id, w, h, NULL, true);
  167. if (code < 0)
  168. return code;
  169. pres = (pdf_resource_t *) pcp;
  170. goto wr;
  171. } else if (pdev->pte) {
  172. /* We're under pdf_text_process. It set a high level color. */
  173. } else
  174. set_image_color(pdev, one);
  175. pdf_make_bitmap_matrix(&image.ImageMatrix, x, y, w, h, h);
  176. goto rx;
  177. }
  178. set_image_color(pdev, one);
  179. gs_image_t_init_mask(&image, false);
  180. invert = 0xff;
  181. } else if (one == gx_no_color_index) {
  182. gs_image_t_init_mask(&image, false);
  183. set_image_color(pdev, zero);
  184. } else if (zero == pdev->black && one == pdev->white) {
  185. gs_cspace_init_DeviceGray(pdev->memory, &cs);
  186. gs_image_t_init(&image, &cs);
  187. } else if (zero == pdev->white && one == pdev->black) {
  188. gs_cspace_init_DeviceGray(pdev->memory, &cs);
  189. gs_image_t_init(&image, &cs);
  190. invert = 0xff;
  191. } else {
  192. /*
  193. * We think this code is never executed when interpreting PostScript
  194. * or PDF: the library never uses monobit non-mask images
  195. * internally, and high-level images don't go through this code.
  196. * However, we still want the code to work.
  197. */
  198. gs_color_space cs_base;
  199. gx_color_index c[2];
  200. int i, j;
  201. int ncomp = pdev->color_info.num_components;
  202. byte *p;
  203. code = pdf_cspace_init_Device(pdev->memory, &cs_base, ncomp);
  204. if (code < 0)
  205. return code;
  206. c[0] = psdf_adjust_color_index((gx_device_vector *)pdev, zero);
  207. c[1] = psdf_adjust_color_index((gx_device_vector *)pdev, one);
  208. gs_cspace_init(&cs, &gs_color_space_type_Indexed, pdev->memory, false);
  209. cs.params.indexed.base_space = *(gs_direct_color_space *)&cs_base;
  210. cs.params.indexed.hival = 1;
  211. p = palette;
  212. for (i = 0; i < 2; ++i)
  213. for (j = ncomp - 1; j >= 0; --j)
  214. *p++ = (byte)(c[i] >> (j * 8));
  215. cs.params.indexed.lookup.table.data = palette;
  216. cs.params.indexed.lookup.table.size = p - palette;
  217. cs.params.indexed.use_proc = false;
  218. gs_image_t_init(&image, &cs);
  219. image.BitsPerComponent = 1;
  220. }
  221. pdf_make_bitmap_image(&image, x, y, w, h);
  222. {
  223. ulong nbytes = (ulong) ((w + 7) >> 3) * h;
  224. in_line = nbytes < pdev->MaxInlineImageSize;
  225. if (in_line)
  226. pdf_put_image_matrix(pdev, &image.ImageMatrix, 1.0);
  227. code = pdf_open_page(pdev, PDF_IN_STREAM);
  228. if (code < 0)
  229. return code;
  230. pdf_image_writer_init(&writer);
  231. code = pdf_begin_write_image(pdev, &writer, gs_no_id, w, h, NULL, in_line);
  232. if (code < 0)
  233. return code;
  234. }
  235. wr:
  236. if (image.ImageMask)
  237. pcsvalue = NULL;
  238. else {
  239. /*
  240. * We don't have to worry about color space scaling: the color
  241. * space is always a Device space.
  242. */
  243. code = pdf_color_space(pdev, &cs_value, NULL, &cs,
  244. &writer.pin->color_spaces, in_line);
  245. if (code < 0)
  246. return code;
  247. pcsvalue = &cs_value;
  248. }
  249. /*
  250. * There are 3 different cases at this point:
  251. * - Writing an in-line image (pres == 0, writer.pres == 0);
  252. * - Writing an XObject image (pres == 0, writer.pres != 0);
  253. * - Writing the image for a CharProc (pres != 0).
  254. * We handle them with in-line code followed by a switch,
  255. * rather than making the shared code into a procedure,
  256. * simply because there would be an awful lot of parameters
  257. * that would need to be passed.
  258. */
  259. if (pres) {
  260. /*
  261. * Always use CCITTFax 2-D for character bitmaps. It takes less
  262. * space to invert the data with Decode than to set BlackIs1.
  263. */
  264. float d0 = image.Decode[0];
  265. image.Decode[0] = image.Decode[1];
  266. image.Decode[1] = d0;
  267. psdf_CFE_binary(&writer.binary[0], image.Width, image.Height, true);
  268. invert ^= 0xff;
  269. } else {
  270. /* Use the Distiller compression parameters. */
  271. psdf_setup_image_filters((gx_device_psdf *) pdev, &writer.binary[0],
  272. (gs_pixel_image_t *)&image, NULL, NULL, true);
  273. }
  274. pdf_begin_image_data(pdev, &writer, (const gs_pixel_image_t *)&image,
  275. pcsvalue, 0);
  276. code = pdf_copy_mask_bits(writer.binary[0].strm, base, sourcex, raster,
  277. w, h, invert);
  278. if (code < 0)
  279. return code;
  280. pdf_end_image_binary(pdev, &writer, writer.height);
  281. if (!pres) {
  282. switch ((code = pdf_end_write_image(pdev, &writer))) {
  283. default: /* error */
  284. return code;
  285. case 1:
  286. return 0;
  287. case 0:
  288. return pdf_do_image(pdev, writer.pres, &image.ImageMatrix,
  289. true);
  290. }
  291. }
  292. writer.end_string = ""; /* no Q */
  293. switch ((code = pdf_end_write_image(pdev, &writer))) {
  294. default: /* error */
  295. return code;
  296. case 0: /* not possible */
  297. return_error(gs_error_Fatal);
  298. case 1:
  299. break;
  300. }
  301. code = pdf_end_char_proc(pdev, &ipos);
  302. if (code < 0)
  303. return code;
  304. rx:{
  305. gs_matrix imat;
  306. imat = image.ImageMatrix;
  307. imat.xx /= w;
  308. imat.xy /= h;
  309. imat.yx /= w;
  310. imat.yy /= h;
  311. return pdf_do_char_image(pdev, (const pdf_char_proc_t *)pres, &imat);
  312. }
  313. }
  314. int
  315. gdev_pdf_copy_mono(gx_device * dev,
  316. const byte * base, int sourcex, int raster, gx_bitmap_id id,
  317. int x, int y, int w, int h, gx_color_index zero,
  318. gx_color_index one)
  319. {
  320. gx_device_pdf *pdev = (gx_device_pdf *) dev;
  321. if (w <= 0 || h <= 0)
  322. return 0;
  323. return pdf_copy_mono(pdev, base, sourcex, raster, id, x, y, w, h,
  324. zero, one, NULL);
  325. }
  326. /* Copy a color bitmap. for_pattern = -1 means put the image in-line, */
  327. /* 1 means put the image in a resource, 2 means image is a rasterized shading. */
  328. int
  329. pdf_copy_color_data(gx_device_pdf * pdev, const byte * base, int sourcex,
  330. int raster, gx_bitmap_id id, int x, int y, int w, int h,
  331. gs_image_t *pim, pdf_image_writer *piw,
  332. int for_pattern)
  333. {
  334. int depth = pdev->color_info.depth;
  335. int bytes_per_pixel = depth >> 3;
  336. gs_color_space cs;
  337. cos_value_t cs_value;
  338. ulong nbytes;
  339. int code = pdf_cspace_init_Device(pdev->memory, &cs, bytes_per_pixel);
  340. const byte *row_base;
  341. int row_step;
  342. bool in_line;
  343. if (code < 0)
  344. return code; /* can't happen */
  345. gs_image_t_init(pim, &cs);
  346. pdf_make_bitmap_image(pim, x, y, w, h);
  347. pim->BitsPerComponent = 8;
  348. nbytes = (ulong)w * bytes_per_pixel * h;
  349. if (for_pattern == 1) {
  350. /*
  351. * Patterns must be emitted in order of increasing user Y, i.e.,
  352. * the opposite of PDF's standard image order.
  353. */
  354. row_base = base + (h - 1) * raster;
  355. row_step = -raster;
  356. in_line = for_pattern < 0;
  357. } else {
  358. row_base = base;
  359. row_step = raster;
  360. in_line = nbytes < pdev->MaxInlineImageSize;
  361. pdf_put_image_matrix(pdev, &pim->ImageMatrix, 1.0);
  362. /*
  363. * Check whether we've already made an XObject resource for this
  364. * image.
  365. */
  366. if (id != gx_no_bitmap_id) {
  367. piw->pres = pdf_find_resource_by_gs_id(pdev, resourceXObject, id);
  368. if (piw->pres)
  369. return 0;
  370. }
  371. }
  372. /*
  373. * We have to be able to control whether to put Pattern images in line,
  374. * to avoid trying to create an XObject resource while we're in the
  375. * middle of writing a Pattern resource.
  376. */
  377. if (for_pattern < 0)
  378. stream_puts(pdev->strm, "q ");
  379. /*
  380. * We don't have to worry about color space scaling: the color
  381. * space is always a Device space.
  382. */
  383. pdf_image_writer_init(piw);
  384. if ((code = pdf_begin_write_image(pdev, piw, id, w, h, NULL, in_line)) < 0 ||
  385. (code = pdf_color_space(pdev, &cs_value, NULL, &cs,
  386. &piw->pin->color_spaces, in_line)) < 0 ||
  387. (for_pattern < 2 || nbytes < 512000 ?
  388. (code = psdf_setup_lossless_filters((gx_device_psdf *) pdev,
  389. &piw->binary[0], (gs_pixel_image_t *)pim)) :
  390. (code = psdf_setup_image_filters((gx_device_psdf *) pdev,
  391. &piw->binary[0], (gs_pixel_image_t *)pim, NULL, NULL, false))
  392. ) < 0 ||
  393. (code = pdf_begin_image_data(pdev, piw, (const gs_pixel_image_t *)pim,
  394. &cs_value, 0)) < 0
  395. )
  396. return code;
  397. pdf_copy_color_bits(piw->binary[0].strm, row_base, sourcex, row_step, w, h,
  398. bytes_per_pixel);
  399. pdf_end_image_binary(pdev, piw, piw->height);
  400. return pdf_end_write_image(pdev, piw);
  401. }
  402. int
  403. gdev_pdf_copy_color(gx_device * dev, const byte * base, int sourcex,
  404. int raster, gx_bitmap_id id, int x, int y, int w, int h)
  405. {
  406. gx_device_pdf *pdev = (gx_device_pdf *) dev;
  407. gs_image_t image;
  408. pdf_image_writer writer;
  409. int code;
  410. if (w <= 0 || h <= 0)
  411. return 0;
  412. code = pdf_open_page(pdev, PDF_IN_STREAM);
  413. if (code < 0)
  414. return code;
  415. /* Make sure we aren't being clipped. */
  416. code = pdf_put_clip_path(pdev, NULL);
  417. if (code < 0)
  418. return code;
  419. code = pdf_copy_color_data(pdev, base, sourcex, raster, id, x, y, w, h,
  420. &image, &writer, 0);
  421. switch (code) {
  422. default:
  423. return code; /* error */
  424. case 1:
  425. return 0;
  426. case 0:
  427. return pdf_do_image(pdev, writer.pres, NULL, true);
  428. }
  429. }
  430. /* Fill a mask. */
  431. int
  432. gdev_pdf_fill_mask(gx_device * dev,
  433. const byte * data, int data_x, int raster, gx_bitmap_id id,
  434. int x, int y, int width, int height,
  435. const gx_drawing_color * pdcolor, int depth,
  436. gs_logical_operation_t lop, const gx_clip_path * pcpath)
  437. {
  438. gx_device_pdf *pdev = (gx_device_pdf *) dev;
  439. if (width <= 0 || height <= 0)
  440. return 0;
  441. if (depth > 1 || (!gx_dc_is_pure(pdcolor) != 0 && pdcolor->type != &gx_dc_pattern))
  442. return gx_default_fill_mask(dev, data, data_x, raster, id,
  443. x, y, width, height, pdcolor, depth, lop,
  444. pcpath);
  445. return pdf_copy_mono(pdev, data, data_x, raster, id, x, y, width, height,
  446. gx_no_color_index, gx_dc_pure_color(pdcolor),
  447. pcpath);
  448. }
  449. /* Tile with a bitmap. This is important for pattern fills. */
  450. int
  451. gdev_pdf_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tiles,
  452. int x, int y, int w, int h,
  453. gx_color_index color0, gx_color_index color1,
  454. int px, int py)
  455. {
  456. gx_device_pdf *const pdev = (gx_device_pdf *) dev;
  457. int tw = tiles->rep_width, th = tiles->rep_height;
  458. double xscale = pdev->HWResolution[0] / 72.0,
  459. yscale = pdev->HWResolution[1] / 72.0;
  460. bool mask;
  461. int depth;
  462. int (*copy_data)(gx_device_pdf *, const byte *, int, int,
  463. gx_bitmap_id, int, int, int, int,
  464. gs_image_t *, pdf_image_writer *, int);
  465. pdf_resource_t *pres;
  466. cos_value_t cs_value;
  467. int code;
  468. if (tiles->id == gx_no_bitmap_id || tiles->shift != 0 ||
  469. (w < tw && h < th) ||
  470. color0 != gx_no_color_index
  471. )
  472. goto use_default;
  473. if (color1 != gx_no_color_index) {
  474. /* This is a mask pattern. */
  475. mask = true;
  476. depth = 1;
  477. copy_data = pdf_copy_mask_data;
  478. code = pdf_cs_Pattern_uncolored(pdev, &cs_value);
  479. } else {
  480. /* This is a colored pattern. */
  481. mask = false;
  482. depth = pdev->color_info.depth;
  483. copy_data = pdf_copy_color_data;
  484. code = pdf_cs_Pattern_colored(pdev, &cs_value);
  485. }
  486. if (code < 0)
  487. goto use_default;
  488. pres = pdf_find_resource_by_gs_id(pdev, resourcePattern, tiles->id);
  489. if (!pres) {
  490. /* Create the Pattern resource. */
  491. int code;
  492. long image_id, length_id, start, end;
  493. stream *s;
  494. gs_image_t image;
  495. pdf_image_writer writer;
  496. long image_bytes = ((long)tw * depth + 7) / 8 * th;
  497. bool in_line = image_bytes < pdev->MaxInlineImageSize;
  498. ulong tile_id =
  499. (tw == tiles->size.x && th == tiles->size.y ? tiles->id :
  500. gx_no_bitmap_id);
  501. if (in_line)
  502. image_id = 0;
  503. else if (image_bytes > 65500) {
  504. /*
  505. * Acrobat Reader can't handle image Patterns with more than
  506. * 64K of data. :-(
  507. */
  508. goto use_default;
  509. } else {
  510. /* Write the image as an XObject resource now. */
  511. code = copy_data(pdev, tiles->data, 0, tiles->raster,
  512. tile_id, 0, 0, tw, th, &image, &writer, 1);
  513. if (code < 0)
  514. goto use_default;
  515. image_id = pdf_resource_id(writer.pres);
  516. }
  517. code = pdf_begin_resource(pdev, resourcePattern, tiles->id, &pres);
  518. if (code < 0)
  519. goto use_default;
  520. s = pdev->strm;
  521. pprintd1(s, "/PatternType 1/PaintType %d/TilingType 1/Resources<<\n",
  522. (mask ? 2 : 1));
  523. if (image_id)
  524. pprintld2(s, "/XObject<</R%ld %ld 0 R>>", image_id, image_id);
  525. pprints1(s, "/ProcSet[/PDF/Image%s]>>\n", (mask ? "B" : "C"));
  526. /*
  527. * Because of bugs in Acrobat Reader's Print function, we can't use
  528. * the natural BBox and Step here: they have to be 1.
  529. */
  530. pprintg2(s, "/Matrix[%g 0 0 %g 0 0]", tw / xscale, th / yscale);
  531. stream_puts(s, "/BBox[0 0 1 1]/XStep 1/YStep 1/Length ");
  532. if (image_id) {
  533. char buf[MAX_REF_CHARS + 6 + 1]; /* +6 for /R# Do\n */
  534. sprintf(buf, "/R%ld Do\n", image_id);
  535. pprintd1(s, "%d>>stream\n", strlen(buf));
  536. pprints1(s, "%sendstream\n", buf);
  537. pdf_end_resource(pdev);
  538. } else {
  539. length_id = pdf_obj_ref(pdev);
  540. pprintld1(s, "%ld 0 R>>stream\n", length_id);
  541. start = pdf_stell(pdev);
  542. code = copy_data(pdev, tiles->data, 0, tiles->raster,
  543. tile_id, 0, 0, tw, th, &image, &writer, -1);
  544. switch (code) {
  545. default:
  546. return code; /* error */
  547. case 1:
  548. break;
  549. case 0: /* not possible */
  550. return_error(gs_error_Fatal);
  551. }
  552. end = pdf_stell(pdev);
  553. stream_puts(s, "\nendstream\n");
  554. pdf_end_resource(pdev);
  555. pdf_open_separate(pdev, length_id);
  556. pprintld1(pdev->strm, "%ld\n", end - start);
  557. pdf_end_separate(pdev);
  558. }
  559. pres->object->written = true; /* don't write at end of page */
  560. }
  561. /* Fill the rectangle with the Pattern. */
  562. {
  563. int code = pdf_open_page(pdev, PDF_IN_STREAM);
  564. stream *s;
  565. if (code < 0)
  566. goto use_default;
  567. /* Make sure we aren't being clipped. */
  568. code = pdf_put_clip_path(pdev, NULL);
  569. if (code < 0)
  570. return code;
  571. s = pdev->strm;
  572. /*
  573. * Because of bugs in Acrobat Reader's Print function, we can't
  574. * leave the CTM alone here: we have to reset it to the default.
  575. */
  576. pprintg2(s, "q %g 0 0 %g 0 0 cm\n", xscale, yscale);
  577. cos_value_write(&cs_value, pdev);
  578. stream_puts(s, " cs");
  579. if (mask)
  580. pprintg3(s, " %g %g %g", (int)(color1 >> 16) / 255.0,
  581. (int)((color1 >> 8) & 0xff) / 255.0,
  582. (int)(color1 & 0xff) / 255.0);
  583. pprintld1(s, "/R%ld scn", pdf_resource_id(pres));
  584. pprintg4(s, " %g %g %g %g re f Q\n",
  585. x / xscale, y / yscale, w / xscale, h / xscale);
  586. }
  587. return 0;
  588. use_default:
  589. return gx_default_strip_tile_rectangle(dev, tiles, x, y, w, h,
  590. color0, color1, px, py);
  591. }