pdf-util.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "mupdf/pdf.h"
  24. fz_display_list *
  25. pdf_new_display_list_from_annot(fz_context *ctx, pdf_annot *annot)
  26. {
  27. fz_display_list *list;
  28. fz_device *dev = NULL;
  29. fz_var(dev);
  30. list = fz_new_display_list(ctx, pdf_bound_annot(ctx, annot));
  31. fz_try(ctx)
  32. {
  33. dev = fz_new_list_device(ctx, list);
  34. pdf_run_annot(ctx, annot, dev, fz_identity, NULL);
  35. fz_close_device(ctx, dev);
  36. }
  37. fz_always(ctx)
  38. {
  39. fz_drop_device(ctx, dev);
  40. }
  41. fz_catch(ctx)
  42. {
  43. fz_drop_display_list(ctx, list);
  44. fz_rethrow(ctx);
  45. }
  46. return list;
  47. }
  48. fz_pixmap *
  49. pdf_new_pixmap_from_annot(fz_context *ctx, pdf_annot *annot, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha)
  50. {
  51. fz_rect rect;
  52. fz_irect bbox;
  53. fz_pixmap *pix;
  54. fz_device *dev = NULL;
  55. fz_var(dev);
  56. rect = pdf_bound_annot(ctx, annot);
  57. rect = fz_transform_rect(rect, ctm);
  58. bbox = fz_round_rect(rect);
  59. pix = fz_new_pixmap_with_bbox(ctx, cs, bbox, seps, alpha);
  60. if (alpha)
  61. fz_clear_pixmap(ctx, pix);
  62. else
  63. fz_clear_pixmap_with_value(ctx, pix, 0xFF);
  64. fz_try(ctx)
  65. {
  66. dev = fz_new_draw_device(ctx, ctm, pix);
  67. pdf_run_annot(ctx, annot, dev, fz_identity, NULL);
  68. fz_close_device(ctx, dev);
  69. }
  70. fz_always(ctx)
  71. {
  72. fz_drop_device(ctx, dev);
  73. }
  74. fz_catch(ctx)
  75. {
  76. fz_drop_pixmap(ctx, pix);
  77. fz_rethrow(ctx);
  78. }
  79. return pix;
  80. }
  81. fz_stext_page *
  82. pdf_new_stext_page_from_annot(fz_context *ctx, pdf_annot *annot, const fz_stext_options *options)
  83. {
  84. fz_stext_page *text;
  85. fz_device *dev = NULL;
  86. fz_var(dev);
  87. if (annot == NULL)
  88. return NULL;
  89. text = fz_new_stext_page(ctx, pdf_bound_annot(ctx, annot));
  90. fz_try(ctx)
  91. {
  92. dev = fz_new_stext_device(ctx, text, options);
  93. pdf_run_annot(ctx, annot, dev, fz_identity, NULL);
  94. fz_close_device(ctx, dev);
  95. }
  96. fz_always(ctx)
  97. {
  98. fz_drop_device(ctx, dev);
  99. }
  100. fz_catch(ctx)
  101. {
  102. fz_drop_stext_page(ctx, text);
  103. fz_rethrow(ctx);
  104. }
  105. return text;
  106. }
  107. fz_pixmap *
  108. pdf_new_pixmap_from_page_contents_with_separations_and_usage(fz_context *ctx, pdf_page *page, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha, const char *usage, fz_box_type box)
  109. {
  110. fz_rect rect;
  111. fz_irect bbox;
  112. fz_pixmap *pix;
  113. fz_device *dev = NULL;
  114. fz_var(dev);
  115. rect = pdf_bound_page(ctx, page, box);
  116. rect = fz_transform_rect(rect, ctm);
  117. bbox = fz_round_rect(rect);
  118. pix = fz_new_pixmap_with_bbox(ctx, cs, bbox, seps, alpha);
  119. if (alpha)
  120. fz_clear_pixmap(ctx, pix);
  121. else
  122. fz_clear_pixmap_with_value(ctx, pix, 0xFF);
  123. fz_try(ctx)
  124. {
  125. dev = fz_new_draw_device(ctx, ctm, pix);
  126. fz_try(ctx)
  127. {
  128. pdf_run_page_contents_with_usage(ctx, page, dev, fz_identity, usage, NULL);
  129. }
  130. fz_catch(ctx)
  131. {
  132. dev->close_device = NULL; /* aborted run, don't warn about unclosed device */
  133. fz_rethrow_unless(ctx, FZ_ERROR_ABORT);
  134. fz_ignore_error(ctx);
  135. }
  136. fz_close_device(ctx, dev);
  137. }
  138. fz_always(ctx)
  139. {
  140. fz_drop_device(ctx, dev);
  141. }
  142. fz_catch(ctx)
  143. {
  144. fz_drop_pixmap(ctx, pix);
  145. fz_rethrow(ctx);
  146. }
  147. return pix;
  148. }
  149. fz_pixmap *
  150. pdf_new_pixmap_from_page_contents_with_usage(fz_context *ctx, pdf_page *page, fz_matrix ctm, fz_colorspace *cs, int alpha, const char *usage, fz_box_type box)
  151. {
  152. return pdf_new_pixmap_from_page_contents_with_separations_and_usage(ctx, page, ctm, cs, NULL, alpha, usage, box);
  153. }
  154. fz_pixmap *
  155. pdf_new_pixmap_from_page_with_separations_and_usage(fz_context *ctx, pdf_page *page, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha, const char *usage, fz_box_type box)
  156. {
  157. fz_rect rect;
  158. fz_irect bbox;
  159. fz_pixmap *pix;
  160. fz_device *dev = NULL;
  161. fz_var(dev);
  162. rect = pdf_bound_page(ctx, page, box);
  163. rect = fz_transform_rect(rect, ctm);
  164. bbox = fz_round_rect(rect);
  165. pix = fz_new_pixmap_with_bbox(ctx, cs, bbox, seps, alpha);
  166. fz_try(ctx)
  167. {
  168. if (alpha)
  169. fz_clear_pixmap(ctx, pix);
  170. else
  171. fz_clear_pixmap_with_value(ctx, pix, 0xFF);
  172. dev = fz_new_draw_device(ctx, ctm, pix);
  173. fz_try(ctx)
  174. {
  175. pdf_run_page_with_usage(ctx, page, dev, fz_identity, usage, NULL);
  176. }
  177. fz_catch(ctx)
  178. {
  179. dev->close_device = NULL; /* aborted run, don't warn about unclosed device */
  180. fz_rethrow_unless(ctx, FZ_ERROR_ABORT);
  181. fz_ignore_error(ctx);
  182. }
  183. fz_close_device(ctx, dev);
  184. }
  185. fz_always(ctx)
  186. {
  187. fz_drop_device(ctx, dev);
  188. }
  189. fz_catch(ctx)
  190. {
  191. fz_drop_pixmap(ctx, pix);
  192. fz_rethrow(ctx);
  193. }
  194. return pix;
  195. }
  196. fz_pixmap *
  197. pdf_new_pixmap_from_page_with_usage(fz_context *ctx, pdf_page *page, fz_matrix ctm, fz_colorspace *cs, int alpha, const char *usage, fz_box_type box)
  198. {
  199. return pdf_new_pixmap_from_page_with_separations_and_usage(ctx, page, ctm, cs, NULL, alpha, usage, box);
  200. }