pdf-graft.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include <assert.h>
  25. struct pdf_graft_map
  26. {
  27. int refs;
  28. int len;
  29. pdf_document *src;
  30. pdf_document *dst;
  31. int *dst_from_src;
  32. };
  33. pdf_graft_map *
  34. pdf_new_graft_map(fz_context *ctx, pdf_document *dst)
  35. {
  36. pdf_graft_map *map = NULL;
  37. if (dst == NULL)
  38. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot create graft make without a destination document");
  39. map = fz_malloc_struct(ctx, pdf_graft_map);
  40. map->dst = pdf_keep_document(ctx, dst);
  41. map->refs = 1;
  42. return map;
  43. }
  44. pdf_graft_map *
  45. pdf_keep_graft_map(fz_context *ctx, pdf_graft_map *map)
  46. {
  47. return fz_keep_imp(ctx, map, &map->refs);
  48. }
  49. void
  50. pdf_drop_graft_map(fz_context *ctx, pdf_graft_map *map)
  51. {
  52. if (fz_drop_imp(ctx, map, &map->refs))
  53. {
  54. pdf_drop_document(ctx, map->src);
  55. pdf_drop_document(ctx, map->dst);
  56. fz_free(ctx, map->dst_from_src);
  57. fz_free(ctx, map);
  58. }
  59. }
  60. pdf_obj *
  61. pdf_graft_object(fz_context *ctx, pdf_document *dst, pdf_obj *obj)
  62. {
  63. pdf_document *src;
  64. pdf_graft_map *map;
  65. /* Primitive objects are not bound to a document, so can be re-used as is. */
  66. src = pdf_get_bound_document(ctx, obj);
  67. if (src == NULL)
  68. return pdf_keep_obj(ctx, obj);
  69. map = pdf_new_graft_map(ctx, dst);
  70. fz_try(ctx)
  71. obj = pdf_graft_mapped_object(ctx, map, obj);
  72. fz_always(ctx)
  73. pdf_drop_graft_map(ctx, map);
  74. fz_catch(ctx)
  75. fz_rethrow(ctx);
  76. return obj;
  77. }
  78. pdf_obj *
  79. pdf_graft_mapped_object(fz_context *ctx, pdf_graft_map *map, pdf_obj *obj)
  80. {
  81. pdf_obj *val, *key;
  82. pdf_obj *new_obj = NULL;
  83. pdf_obj *new_dict;
  84. pdf_obj *new_array;
  85. pdf_obj *ref = NULL;
  86. fz_buffer *buffer = NULL;
  87. pdf_document *src;
  88. int new_num, src_num, len, i;
  89. /* Primitive objects are not bound to a document, so can be re-used as is. */
  90. src = pdf_get_bound_document(ctx, obj);
  91. if (!src)
  92. return pdf_keep_obj(ctx, obj);
  93. if (map->src && src != map->src)
  94. fz_throw(ctx, FZ_ERROR_ARGUMENT, "grafted objects must all belong to the same source document");
  95. if (pdf_is_indirect(ctx, obj))
  96. {
  97. src_num = pdf_to_num(ctx, obj);
  98. if (map->src == NULL)
  99. {
  100. fz_try(ctx)
  101. {
  102. map->src = pdf_keep_document(ctx, src);
  103. map->len = pdf_xref_len(ctx, src);
  104. map->dst_from_src = fz_calloc(ctx, map->len, sizeof(int));
  105. }
  106. fz_catch(ctx)
  107. {
  108. pdf_drop_document(ctx, map->src);
  109. map->src = NULL;
  110. fz_rethrow(ctx);
  111. }
  112. }
  113. if (src_num < 1 || src_num >= map->len)
  114. fz_throw(ctx, FZ_ERROR_ARGUMENT, "source object number out of range");
  115. /* Check if we have done this one. If yes, then just
  116. * return our indirect ref */
  117. if (map->dst_from_src[src_num] != 0)
  118. {
  119. int dest_num = map->dst_from_src[src_num];
  120. return pdf_new_indirect(ctx, map->dst, dest_num, 0);
  121. }
  122. fz_var(buffer);
  123. fz_var(ref);
  124. fz_var(new_obj);
  125. fz_try(ctx)
  126. {
  127. /* Create new slot for our src object, set the mapping and call again
  128. * using the resolved indirect reference */
  129. new_num = pdf_create_object(ctx, map->dst);
  130. map->dst_from_src[src_num] = new_num;
  131. new_obj = pdf_graft_mapped_object(ctx, map, pdf_resolve_indirect(ctx, obj));
  132. /* Return a ref to the new_obj making sure to attach any stream */
  133. pdf_update_object(ctx, map->dst, new_num, new_obj);
  134. ref = pdf_new_indirect(ctx, map->dst, new_num, 0);
  135. if (pdf_is_stream(ctx, obj))
  136. {
  137. buffer = pdf_load_raw_stream_number(ctx, src, src_num);
  138. pdf_update_stream(ctx, map->dst, ref, buffer, 1);
  139. }
  140. }
  141. fz_always(ctx)
  142. {
  143. pdf_drop_obj(ctx, new_obj);
  144. fz_drop_buffer(ctx, buffer);
  145. }
  146. fz_catch(ctx)
  147. {
  148. pdf_drop_obj(ctx, ref);
  149. fz_rethrow(ctx);
  150. }
  151. return ref;
  152. }
  153. else if (pdf_is_dict(ctx, obj))
  154. {
  155. len = pdf_dict_len(ctx, obj);
  156. new_dict = pdf_new_dict(ctx, map->dst, len);
  157. fz_try(ctx)
  158. {
  159. for (i = 0; i < len; i++)
  160. {
  161. key = pdf_dict_get_key(ctx, obj, i);
  162. val = pdf_dict_get_val(ctx, obj, i);
  163. pdf_dict_put_drop(ctx, new_dict, key, pdf_graft_mapped_object(ctx, map, val));
  164. }
  165. }
  166. fz_catch(ctx)
  167. {
  168. pdf_drop_obj(ctx, new_dict);
  169. fz_rethrow(ctx);
  170. }
  171. return new_dict;
  172. }
  173. else if (pdf_is_array(ctx, obj))
  174. {
  175. /* Step through the array items handling indirect refs */
  176. len = pdf_array_len(ctx, obj);
  177. new_array = pdf_new_array(ctx, map->dst, len);
  178. fz_try(ctx)
  179. {
  180. for (i = 0; i < len; i++)
  181. {
  182. val = pdf_array_get(ctx, obj, i);
  183. pdf_array_push_drop(ctx, new_array, pdf_graft_mapped_object(ctx, map, val));
  184. }
  185. }
  186. fz_catch(ctx)
  187. {
  188. pdf_drop_obj(ctx, new_array);
  189. fz_rethrow(ctx);
  190. }
  191. return new_array;
  192. }
  193. else
  194. {
  195. assert("This never happens" == NULL);
  196. return NULL;
  197. }
  198. }
  199. void pdf_graft_mapped_page(fz_context *ctx, pdf_graft_map *map, int page_to, pdf_document *src, int page_from)
  200. {
  201. pdf_obj *page_ref;
  202. pdf_obj *page_dict = NULL;
  203. pdf_obj *obj;
  204. pdf_obj *ref = NULL;
  205. int i;
  206. pdf_document *dst = map->dst;
  207. /* Copy as few key/value pairs as we can. Do not include items that reference other pages. */
  208. static pdf_obj * const copy_list[] = {
  209. PDF_NAME(Contents),
  210. PDF_NAME(Resources),
  211. PDF_NAME(MediaBox),
  212. PDF_NAME(CropBox),
  213. PDF_NAME(BleedBox),
  214. PDF_NAME(TrimBox),
  215. PDF_NAME(ArtBox),
  216. PDF_NAME(Rotate),
  217. PDF_NAME(UserUnit)
  218. };
  219. fz_var(ref);
  220. fz_var(page_dict);
  221. fz_try(ctx)
  222. {
  223. page_ref = pdf_lookup_page_obj(ctx, src, page_from);
  224. /* Make a new page object dictionary to hold the items we copy from the source page. */
  225. page_dict = pdf_new_dict(ctx, dst, 4);
  226. pdf_dict_put(ctx, page_dict, PDF_NAME(Type), PDF_NAME(Page));
  227. for (i = 0; i < (int)nelem(copy_list); i++)
  228. {
  229. obj = pdf_dict_get_inheritable(ctx, page_ref, copy_list[i]);
  230. if (obj != NULL)
  231. pdf_dict_put_drop(ctx, page_dict, copy_list[i], pdf_graft_mapped_object(ctx, map, obj));
  232. }
  233. /* Add the page object to the destination document. */
  234. ref = pdf_add_object(ctx, dst, page_dict);
  235. /* Insert it into the page tree. */
  236. pdf_insert_page(ctx, dst, page_to, ref);
  237. }
  238. fz_always(ctx)
  239. {
  240. pdf_drop_obj(ctx, page_dict);
  241. pdf_drop_obj(ctx, ref);
  242. }
  243. fz_catch(ctx)
  244. {
  245. fz_rethrow(ctx);
  246. }
  247. }
  248. void pdf_graft_page(fz_context *ctx, pdf_document *dst, int page_to, pdf_document *src, int page_from)
  249. {
  250. pdf_graft_map *map = pdf_new_graft_map(ctx, dst);
  251. fz_try(ctx)
  252. pdf_graft_mapped_page(ctx, map, page_to, src, page_from);
  253. fz_always(ctx)
  254. pdf_drop_graft_map(ctx, map);
  255. fz_catch(ctx)
  256. fz_rethrow(ctx);
  257. }