void pdf_free_annot(fz_context *ctx, pdf_annot *annot) { if (annot->next) pdf_free_annot(ctx, annot->next); if (annot->ap) pdf_drop_xobject(ctx, annot->ap); if (annot->obj) fz_drop_obj(ctx, annot->obj); fz_free(ctx, annot); }
void pdf_free_page(pdf_document *doc, pdf_page *page) { if (page == NULL) return; pdf_drop_obj(page->resources); pdf_drop_obj(page->contents); if (page->links) fz_drop_link(doc->ctx, page->links); if (page->annots) pdf_free_annot(doc->ctx, page->annots); if (page->deleted_annots) pdf_free_annot(doc->ctx, page->deleted_annots); if (page->tmp_annots) pdf_free_annot(doc->ctx, page->tmp_annots); /* doc->focus, when not NULL, refers to one of * the annotations and must be NULLed when the * annotations are destroyed. doc->focus_obj * keeps track of the actual annotation object. */ doc->focus = NULL; pdf_drop_obj(page->me); fz_free(doc->ctx, page); }
pdf_annot * pdf_create_annot(pdf_document *doc, pdf_page *page, fz_annot_type type) { fz_context *ctx = doc->ctx; pdf_annot *annot = NULL; pdf_obj *annot_obj = pdf_new_dict(doc, 0); pdf_obj *ind_obj = NULL; fz_var(annot); fz_var(ind_obj); fz_try(ctx) { int ind_obj_num; fz_rect rect = {0.0, 0.0, 0.0, 0.0}; const char *type_str = annot_type_str(type); pdf_obj *annot_arr = pdf_dict_gets(page->me, "Annots"); if (annot_arr == NULL) { annot_arr = pdf_new_array(doc, 0); pdf_dict_puts_drop(page->me, "Annots", annot_arr); } pdf_dict_puts_drop(annot_obj, "Type", pdf_new_name(doc, "Annot")); pdf_dict_puts_drop(annot_obj, "Subtype", pdf_new_name(doc, type_str)); pdf_dict_puts_drop(annot_obj, "Rect", pdf_new_rect(doc, &rect)); /* Make printable as default */ pdf_dict_puts_drop(annot_obj, "F", pdf_new_int(doc, F_Print)); annot = fz_malloc_struct(ctx, pdf_annot); annot->page = page; annot->rect = rect; annot->pagerect = rect; annot->ap = NULL; annot->widget_type = PDF_WIDGET_TYPE_NOT_WIDGET; annot->annot_type = type; /* Both annotation object and annotation structure are now created. Insert the object in the hierarchy and the structure in the page's array. */ ind_obj_num = pdf_create_object(doc); pdf_update_object(doc, ind_obj_num, annot_obj); ind_obj = pdf_new_indirect(doc, ind_obj_num, 0); pdf_array_push(annot_arr, ind_obj); annot->obj = pdf_keep_obj(ind_obj); /* Linking must be done after any call that might throw because pdf_free_annot below actually frees a list. Put the new annot at the end of the list, so that it will be drawn last. */ *page->annot_tailp = annot; page->annot_tailp = &annot->next; doc->dirty = 1; } fz_always(ctx) { pdf_drop_obj(annot_obj); pdf_drop_obj(ind_obj); } fz_catch(ctx) { pdf_free_annot(ctx, annot); fz_rethrow(ctx); } return annot; }