pdf_annot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. #include "fitz-internal.h"
  2. #include "mupdf-internal.h"
  3. static pdf_obj *
  4. resolve_dest_rec(pdf_document *xref, pdf_obj *dest, int depth)
  5. {
  6. if (depth > 10) /* Arbitrary to avoid infinite recursion */
  7. return NULL;
  8. if (pdf_is_name(dest) || pdf_is_string(dest))
  9. {
  10. dest = pdf_lookup_dest(xref, dest);
  11. return resolve_dest_rec(xref, dest, depth+1);
  12. }
  13. else if (pdf_is_array(dest))
  14. {
  15. return dest;
  16. }
  17. else if (pdf_is_dict(dest))
  18. {
  19. dest = pdf_dict_gets(dest, "D");
  20. return resolve_dest_rec(xref, dest, depth+1);
  21. }
  22. else if (pdf_is_indirect(dest))
  23. return dest;
  24. return NULL;
  25. }
  26. static pdf_obj *
  27. resolve_dest(pdf_document *xref, pdf_obj *dest)
  28. {
  29. return resolve_dest_rec(xref, dest, 0);
  30. }
  31. fz_link_dest
  32. pdf_parse_link_dest(pdf_document *xref, pdf_obj *dest)
  33. {
  34. fz_link_dest ld;
  35. pdf_obj *obj;
  36. int l_from_2 = 0;
  37. int b_from_3 = 0;
  38. int r_from_4 = 0;
  39. int t_from_5 = 0;
  40. int t_from_3 = 0;
  41. int t_from_2 = 0;
  42. int z_from_4 = 0;
  43. dest = resolve_dest(xref, dest);
  44. if (dest == NULL || !pdf_is_array(dest))
  45. {
  46. ld.kind = FZ_LINK_NONE;
  47. return ld;
  48. }
  49. obj = pdf_array_get(dest, 0);
  50. if (pdf_is_int(obj))
  51. ld.ld.gotor.page = pdf_to_int(obj);
  52. else
  53. ld.ld.gotor.page = pdf_lookup_page_number(xref, obj);
  54. ld.kind = FZ_LINK_GOTO;
  55. ld.ld.gotor.flags = 0;
  56. ld.ld.gotor.lt.x = 0;
  57. ld.ld.gotor.lt.y = 0;
  58. ld.ld.gotor.rb.x = 0;
  59. ld.ld.gotor.rb.y = 0;
  60. ld.ld.gotor.file_spec = NULL;
  61. ld.ld.gotor.new_window = 0;
  62. obj = pdf_array_get(dest, 1);
  63. if (!pdf_is_name(obj))
  64. return ld;
  65. if (!strcmp("XYZ", pdf_to_name(obj)))
  66. {
  67. l_from_2 = t_from_3 = z_from_4 = 1;
  68. ld.ld.gotor.flags |= fz_link_flag_r_is_zoom;
  69. }
  70. else if ((!strcmp("Fit", pdf_to_name(obj))) || (!strcmp("FitB", pdf_to_name(obj))))
  71. {
  72. ld.ld.gotor.flags |= fz_link_flag_fit_h;
  73. ld.ld.gotor.flags |= fz_link_flag_fit_v;
  74. }
  75. else if ((!strcmp("FitH", pdf_to_name(obj))) || (!strcmp("FitBH", pdf_to_name(obj))))
  76. {
  77. t_from_2 = 1;
  78. ld.ld.gotor.flags |= fz_link_flag_fit_h;
  79. }
  80. else if ((!strcmp("FitV", pdf_to_name(obj))) || (!strcmp("FitBV", pdf_to_name(obj))))
  81. {
  82. l_from_2 = 1;
  83. ld.ld.gotor.flags |= fz_link_flag_fit_v;
  84. }
  85. else if (!strcmp("FitR", pdf_to_name(obj)))
  86. {
  87. l_from_2 = b_from_3 = r_from_4 = t_from_5 = 1;
  88. ld.ld.gotor.flags |= fz_link_flag_fit_h;
  89. ld.ld.gotor.flags |= fz_link_flag_fit_v;
  90. }
  91. if (l_from_2)
  92. {
  93. obj = pdf_array_get(dest, 2);
  94. if (pdf_is_int(obj))
  95. {
  96. ld.ld.gotor.flags |= fz_link_flag_l_valid;
  97. ld.ld.gotor.lt.x = pdf_to_int(obj);
  98. }
  99. else if (pdf_is_real(obj))
  100. {
  101. ld.ld.gotor.flags |= fz_link_flag_l_valid;
  102. ld.ld.gotor.lt.x = pdf_to_real(obj);
  103. }
  104. }
  105. if (b_from_3)
  106. {
  107. obj = pdf_array_get(dest, 3);
  108. if (pdf_is_int(obj))
  109. {
  110. ld.ld.gotor.flags |= fz_link_flag_b_valid;
  111. ld.ld.gotor.rb.y = pdf_to_int(obj);
  112. }
  113. else if (pdf_is_real(obj))
  114. {
  115. ld.ld.gotor.flags |= fz_link_flag_b_valid;
  116. ld.ld.gotor.rb.y = pdf_to_real(obj);
  117. }
  118. }
  119. if (r_from_4)
  120. {
  121. obj = pdf_array_get(dest, 4);
  122. if (pdf_is_int(obj))
  123. {
  124. ld.ld.gotor.flags |= fz_link_flag_r_valid;
  125. ld.ld.gotor.rb.x = pdf_to_int(obj);
  126. }
  127. else if (pdf_is_real(obj))
  128. {
  129. ld.ld.gotor.flags |= fz_link_flag_r_valid;
  130. ld.ld.gotor.rb.x = pdf_to_real(obj);
  131. }
  132. }
  133. if (t_from_5 || t_from_3 || t_from_2)
  134. {
  135. if (t_from_5)
  136. obj = pdf_array_get(dest, 5);
  137. else if (t_from_3)
  138. obj = pdf_array_get(dest, 3);
  139. else
  140. obj = pdf_array_get(dest, 2);
  141. if (pdf_is_int(obj))
  142. {
  143. ld.ld.gotor.flags |= fz_link_flag_t_valid;
  144. ld.ld.gotor.lt.y = pdf_to_int(obj);
  145. }
  146. else if (pdf_is_real(obj))
  147. {
  148. ld.ld.gotor.flags |= fz_link_flag_t_valid;
  149. ld.ld.gotor.lt.y = pdf_to_real(obj);
  150. }
  151. }
  152. if (z_from_4)
  153. {
  154. obj = pdf_array_get(dest, 4);
  155. if (pdf_is_int(obj))
  156. {
  157. ld.ld.gotor.flags |= fz_link_flag_r_valid;
  158. ld.ld.gotor.rb.x = pdf_to_int(obj);
  159. }
  160. else if (pdf_is_real(obj))
  161. {
  162. ld.ld.gotor.flags |= fz_link_flag_r_valid;
  163. ld.ld.gotor.rb.x = pdf_to_real(obj);
  164. }
  165. }
  166. /* Duplicate the values out for the sake of stupid clients */
  167. if ((ld.ld.gotor.flags & (fz_link_flag_l_valid | fz_link_flag_r_valid)) == fz_link_flag_l_valid)
  168. ld.ld.gotor.rb.x = ld.ld.gotor.lt.x;
  169. if ((ld.ld.gotor.flags & (fz_link_flag_l_valid | fz_link_flag_r_valid | fz_link_flag_r_is_zoom)) == fz_link_flag_r_valid)
  170. ld.ld.gotor.lt.x = ld.ld.gotor.rb.x;
  171. if ((ld.ld.gotor.flags & (fz_link_flag_t_valid | fz_link_flag_b_valid)) == fz_link_flag_t_valid)
  172. ld.ld.gotor.rb.y = ld.ld.gotor.lt.y;
  173. if ((ld.ld.gotor.flags & (fz_link_flag_t_valid | fz_link_flag_b_valid)) == fz_link_flag_b_valid)
  174. ld.ld.gotor.lt.y = ld.ld.gotor.rb.y;
  175. return ld;
  176. }
  177. fz_link_dest
  178. pdf_parse_action(pdf_document *xref, pdf_obj *action)
  179. {
  180. fz_link_dest ld;
  181. pdf_obj *obj, *dest;
  182. fz_context *ctx = xref->ctx;
  183. UNUSED(ctx);
  184. ld.kind = FZ_LINK_NONE;
  185. if (!action)
  186. return ld;
  187. obj = pdf_dict_gets(action, "S");
  188. if (!strcmp(pdf_to_name(obj), "GoTo"))
  189. {
  190. dest = pdf_dict_gets(action, "D");
  191. ld = pdf_parse_link_dest(xref, dest);
  192. }
  193. else if (!strcmp(pdf_to_name(obj), "URI"))
  194. {
  195. ld.kind = FZ_LINK_URI;
  196. ld.ld.uri.is_map = pdf_to_bool(pdf_dict_gets(action, "IsMap"));
  197. ld.ld.uri.uri = pdf_to_utf8(xref, pdf_dict_gets(action, "URI"));
  198. }
  199. else if (!strcmp(pdf_to_name(obj), "Launch"))
  200. {
  201. dest = pdf_dict_gets(action, "F");
  202. ld.kind = FZ_LINK_LAUNCH;
  203. if (pdf_is_dict(dest))
  204. dest = pdf_dict_gets(dest, "F");
  205. ld.ld.launch.file_spec = pdf_to_utf8(xref, dest);
  206. ld.ld.launch.new_window = pdf_to_int(pdf_dict_gets(action, "NewWindow"));
  207. }
  208. else if (!strcmp(pdf_to_name(obj), "Named"))
  209. {
  210. ld.kind = FZ_LINK_NAMED;
  211. ld.ld.named.named = pdf_to_utf8(xref, pdf_dict_gets(action, "N"));
  212. }
  213. else if (!strcmp(pdf_to_name(obj), "GoToR"))
  214. {
  215. dest = pdf_dict_gets(action, "D");
  216. ld = pdf_parse_link_dest(xref, dest);
  217. ld.kind = FZ_LINK_GOTOR;
  218. ld.ld.gotor.file_spec = pdf_to_utf8(xref, pdf_dict_gets(action, "F"));
  219. ld.ld.gotor.new_window = pdf_to_int(pdf_dict_gets(action, "NewWindow"));
  220. }
  221. return ld;
  222. }
  223. static fz_link *
  224. pdf_load_link(pdf_document *xref, pdf_obj *dict, const fz_matrix *page_ctm)
  225. {
  226. pdf_obj *dest = NULL;
  227. pdf_obj *action;
  228. pdf_obj *obj;
  229. fz_rect bbox;
  230. fz_context *ctx = xref->ctx;
  231. fz_link_dest ld;
  232. obj = pdf_dict_gets(dict, "Rect");
  233. if (obj)
  234. pdf_to_rect(ctx, obj, &bbox);
  235. else
  236. bbox = fz_empty_rect;
  237. fz_transform_rect(&bbox, page_ctm);
  238. obj = pdf_dict_gets(dict, "Dest");
  239. if (obj)
  240. {
  241. dest = resolve_dest(xref, obj);
  242. ld = pdf_parse_link_dest(xref, dest);
  243. }
  244. else
  245. {
  246. action = pdf_dict_gets(dict, "A");
  247. /* fall back to additional action button's down/up action */
  248. if (!action)
  249. action = pdf_dict_getsa(pdf_dict_gets(dict, "AA"), "U", "D");
  250. ld = pdf_parse_action(xref, action);
  251. }
  252. if (ld.kind == FZ_LINK_NONE)
  253. return NULL;
  254. return fz_new_link(ctx, &bbox, ld);
  255. }
  256. fz_link *
  257. pdf_load_link_annots(pdf_document *xref, pdf_obj *annots, const fz_matrix *page_ctm)
  258. {
  259. fz_link *link, *head, *tail;
  260. pdf_obj *obj;
  261. int i, n;
  262. head = tail = NULL;
  263. link = NULL;
  264. n = pdf_array_len(annots);
  265. for (i = 0; i < n; i++)
  266. {
  267. fz_try(xref->ctx)
  268. {
  269. obj = pdf_array_get(annots, i);
  270. link = pdf_load_link(xref, obj, page_ctm);
  271. }
  272. fz_catch(xref->ctx)
  273. {
  274. link = NULL;
  275. }
  276. if (link)
  277. {
  278. if (!head)
  279. head = tail = link;
  280. else
  281. {
  282. tail->next = link;
  283. tail = link;
  284. }
  285. }
  286. }
  287. return head;
  288. }
  289. void
  290. pdf_free_annot(fz_context *ctx, pdf_annot *annot)
  291. {
  292. pdf_annot *next;
  293. while (annot)
  294. {
  295. next = annot->next;
  296. if (annot->ap)
  297. pdf_drop_xobject(ctx, annot->ap);
  298. pdf_drop_obj(annot->obj);
  299. fz_free(ctx, annot);
  300. annot = next;
  301. }
  302. }
  303. static void
  304. pdf_transform_annot(pdf_annot *annot)
  305. {
  306. fz_rect bbox = annot->ap->bbox;
  307. fz_rect rect = annot->rect;
  308. float w, h, x, y;
  309. fz_transform_rect(&bbox, &annot->ap->matrix);
  310. if (bbox.x1 == bbox.x0)
  311. w = 0;
  312. else
  313. w = (rect.x1 - rect.x0) / (bbox.x1 - bbox.x0);
  314. if (bbox.y1 == bbox.y0)
  315. h = 0;
  316. else
  317. h = (rect.y1 - rect.y0) / (bbox.y1 - bbox.y0);
  318. x = rect.x0 - bbox.x0;
  319. y = rect.y0 - bbox.y0;
  320. fz_pre_scale(fz_translate(&annot->matrix, x, y), w, h);
  321. }
  322. pdf_annot *
  323. pdf_load_annots(pdf_document *xref, pdf_obj *annots, pdf_page *page)
  324. {
  325. pdf_annot *annot, *head, *tail;
  326. pdf_obj *obj, *ap, *as, *n, *rect;
  327. int i, len, is_dict;
  328. fz_context *ctx = xref->ctx;
  329. fz_var(annot);
  330. head = tail = NULL;
  331. len = pdf_array_len(annots);
  332. for (i = 0; i < len; i++)
  333. {
  334. fz_try(ctx)
  335. {
  336. obj = pdf_array_get(annots, i);
  337. pdf_update_appearance(xref, obj);
  338. rect = pdf_dict_gets(obj, "Rect");
  339. ap = pdf_dict_gets(obj, "AP");
  340. as = pdf_dict_gets(obj, "AS");
  341. is_dict = pdf_is_dict(ap);
  342. }
  343. fz_catch(ctx)
  344. {
  345. ap = NULL;
  346. is_dict = 0;
  347. }
  348. if (!is_dict)
  349. continue;
  350. annot = NULL;
  351. fz_try(ctx)
  352. {
  353. pdf_hotspot *hp = &xref->hotspot;
  354. n = NULL;
  355. if (hp->num == pdf_to_num(obj)
  356. && hp->gen == pdf_to_gen(obj)
  357. && (hp->state & HOTSPOT_POINTER_DOWN))
  358. {
  359. n = pdf_dict_gets(ap, "D"); /* down state */
  360. }
  361. if (n == NULL)
  362. n = pdf_dict_gets(ap, "N"); /* normal state */
  363. /* lookup current state in sub-dictionary */
  364. if (!pdf_is_stream(xref, pdf_to_num(n), pdf_to_gen(n)))
  365. n = pdf_dict_get(n, as);
  366. annot = fz_malloc_struct(ctx, pdf_annot);
  367. annot->page = page;
  368. annot->obj = pdf_keep_obj(obj);
  369. pdf_to_rect(ctx, rect, &annot->rect);
  370. annot->pagerect = annot->rect;
  371. fz_transform_rect(&annot->pagerect, &page->ctm);
  372. annot->ap = NULL;
  373. annot->type = pdf_field_type(xref, obj);
  374. if (pdf_is_stream(xref, pdf_to_num(n), pdf_to_gen(n)))
  375. {
  376. annot->ap = pdf_load_xobject(xref, n);
  377. pdf_transform_annot(annot);
  378. annot->ap_iteration = annot->ap->iteration;
  379. }
  380. annot->next = NULL;
  381. if (obj == xref->focus_obj)
  382. xref->focus = annot;
  383. if (!head)
  384. head = tail = annot;
  385. else
  386. {
  387. tail->next = annot;
  388. tail = annot;
  389. }
  390. }
  391. fz_catch(ctx)
  392. {
  393. pdf_free_annot(ctx, annot);
  394. fz_warn(ctx, "ignoring broken annotation");
  395. }
  396. }
  397. return head;
  398. }
  399. void
  400. pdf_update_annot(pdf_document *xref, pdf_annot *annot)
  401. {
  402. pdf_obj *obj, *ap, *as, *n;
  403. fz_context *ctx = xref->ctx;
  404. obj = annot->obj;
  405. pdf_update_appearance(xref, obj);
  406. ap = pdf_dict_gets(obj, "AP");
  407. as = pdf_dict_gets(obj, "AS");
  408. if (pdf_is_dict(ap))
  409. {
  410. pdf_hotspot *hp = &xref->hotspot;
  411. n = NULL;
  412. if (hp->num == pdf_to_num(obj)
  413. && hp->gen == pdf_to_gen(obj)
  414. && (hp->state & HOTSPOT_POINTER_DOWN))
  415. {
  416. n = pdf_dict_gets(ap, "D"); /* down state */
  417. }
  418. if (n == NULL)
  419. n = pdf_dict_gets(ap, "N"); /* normal state */
  420. /* lookup current state in sub-dictionary */
  421. if (!pdf_is_stream(xref, pdf_to_num(n), pdf_to_gen(n)))
  422. n = pdf_dict_get(n, as);
  423. pdf_drop_xobject(ctx, annot->ap);
  424. annot->ap = NULL;
  425. if (pdf_is_stream(xref, pdf_to_num(n), pdf_to_gen(n)))
  426. {
  427. fz_try(ctx)
  428. {
  429. annot->ap = pdf_load_xobject(xref, n);
  430. pdf_transform_annot(annot);
  431. annot->ap_iteration = annot->ap->iteration;
  432. }
  433. fz_catch(ctx)
  434. {
  435. fz_warn(ctx, "ignoring broken annotation");
  436. }
  437. }
  438. }
  439. }
  440. pdf_annot *
  441. pdf_first_annot(pdf_document *doc, pdf_page *page)
  442. {
  443. return page ? page->annots : NULL;
  444. }
  445. pdf_annot *
  446. pdf_next_annot(pdf_document *doc, pdf_annot *annot)
  447. {
  448. return annot ? annot->next : NULL;
  449. }
  450. fz_rect *
  451. pdf_bound_annot(pdf_document *doc, pdf_annot *annot, fz_rect *rect)
  452. {
  453. if (rect == NULL)
  454. return NULL;
  455. if (annot)
  456. *rect = annot->pagerect;
  457. else
  458. *rect = fz_empty_rect;
  459. return rect;
  460. }
  461. pdf_annot *
  462. pdf_create_annot(pdf_document *doc, pdf_page *page, fz_annot_type type)
  463. {
  464. fz_context *ctx = doc->ctx;
  465. pdf_annot *annot = NULL;
  466. pdf_obj *annot_obj = pdf_new_dict(ctx, 0);
  467. pdf_obj *ind_obj = NULL;
  468. fz_var(annot);
  469. fz_var(ind_obj);
  470. fz_try(ctx)
  471. {
  472. int ind_obj_num;
  473. fz_rect rect = {0.0, 0.0, 0.0, 0.0};
  474. char *type_str = "";
  475. pdf_obj *annot_arr = pdf_dict_gets(page->me, "Annots");
  476. if (annot_arr == NULL)
  477. {
  478. annot_arr = pdf_new_array(ctx, 0);
  479. pdf_dict_puts_drop(page->me, "Annots", annot_arr);
  480. }
  481. pdf_dict_puts_drop(annot_obj, "Type", pdf_new_name(ctx, "Annot"));
  482. switch(type)
  483. {
  484. case FZ_ANNOT_STRIKEOUT:
  485. type_str = "StrikeOut";
  486. break;
  487. }
  488. pdf_dict_puts_drop(annot_obj, "Subtype", pdf_new_name(ctx, type_str));
  489. pdf_dict_puts_drop(annot_obj, "Rect", pdf_new_rect(ctx, &rect));
  490. annot = fz_malloc_struct(ctx, pdf_annot);
  491. annot->page = page;
  492. annot->obj = pdf_keep_obj(annot_obj);
  493. annot->rect = rect;
  494. annot->pagerect = rect;
  495. annot->ap = NULL;
  496. annot->type = FZ_WIDGET_TYPE_NOT_WIDGET;
  497. /*
  498. Both annotation object and annotation structure are now created.
  499. Insert the object in the hierarchy and the structure in the
  500. page's array.
  501. */
  502. ind_obj_num = pdf_create_object(doc);
  503. pdf_update_object(doc, ind_obj_num, annot_obj);
  504. ind_obj = pdf_new_indirect(ctx, ind_obj_num, 0, doc);
  505. pdf_array_push(annot_arr, ind_obj);
  506. /*
  507. Linking must be done before any call that might throw because
  508. pdf_free_annot below actually frees a list
  509. */
  510. annot->next = page->annots;
  511. page->annots = annot;
  512. doc->dirty = 1;
  513. }
  514. fz_always(ctx)
  515. {
  516. pdf_drop_obj(annot_obj);
  517. pdf_drop_obj(ind_obj);
  518. }
  519. fz_catch(ctx)
  520. {
  521. pdf_free_annot(ctx, annot);
  522. fz_rethrow(ctx);
  523. }
  524. return annot;
  525. }
  526. void
  527. pdf_set_annot_appearance(pdf_document *doc, pdf_annot *annot, fz_display_list *disp_list)
  528. {
  529. fz_context *ctx = doc->ctx;
  530. fz_matrix ctm;
  531. fz_rect rect;
  532. fz_matrix mat = fz_identity;
  533. fz_device *dev = fz_new_bbox_device(ctx, &rect);
  534. fz_invert_matrix(&ctm, &annot->page->ctm);
  535. fz_try(ctx)
  536. {
  537. pdf_obj *ap_obj;
  538. fz_run_display_list(disp_list, dev, &ctm, &fz_infinite_rect, NULL);
  539. fz_free_device(dev);
  540. dev = NULL;
  541. pdf_dict_puts_drop(annot->obj, "Rect", pdf_new_rect(ctx, &rect));
  542. /* See if there is a current normal appearance */
  543. ap_obj = pdf_dict_getp(annot->obj, "AP/N");
  544. if (!pdf_is_stream(doc, pdf_to_num(annot->obj), pdf_to_gen(annot->obj)))
  545. ap_obj = NULL;
  546. if (ap_obj == NULL)
  547. {
  548. ap_obj = pdf_new_xobject(doc, &rect, &mat);
  549. pdf_dict_putp_drop(annot->obj, "AP/N", ap_obj);
  550. }
  551. else
  552. {
  553. pdf_dict_puts_drop(ap_obj, "Rect", pdf_new_rect(ctx, &rect));
  554. pdf_dict_puts_drop(ap_obj, "Matrix", pdf_new_matrix(ctx, &mat));
  555. }
  556. /* Remove annot reference to the xobject and don't recreate it
  557. so that pdf_update_page counts it as dirty */
  558. pdf_drop_xobject(ctx, annot->ap);
  559. annot->ap = NULL;
  560. annot->rect = rect;
  561. annot->pagerect = rect;
  562. fz_transform_rect(&annot->pagerect, &annot->page->ctm);
  563. dev = pdf_new_pdf_device(doc, ap_obj, pdf_dict_gets(ap_obj, "Resources"), &mat);
  564. fz_run_display_list(disp_list, dev, &ctm, &fz_infinite_rect, NULL);
  565. fz_free_device(dev);
  566. doc->dirty = 1;
  567. }
  568. fz_catch(ctx)
  569. {
  570. fz_free_device(dev);
  571. fz_rethrow(ctx);
  572. }
  573. }