| 1 |
|
| /* |
| 2 |
|
| * pdfmake_page.c — Page and catalog construction. |
| 3 |
|
| */ |
| 4 |
|
| |
| 5 |
|
| #include "pdfmake_page.h" |
| 6 |
|
| #include "pdfmake_font.h" |
| 7 |
|
| #include "pdfmake_ocg.h" |
| 8 |
|
| #include "pdfmake_attach.h" |
| 9 |
|
| #include "pdfmake_tag.h" |
| 10 |
|
| #include "pdfmake_arena.h" |
| 11 |
|
| #include "pdfmake_outline.h" |
| 12 |
|
| #include "pdfmake_form.h" |
| 13 |
|
| #include <stdlib.h> |
| 14 |
|
| #include <string.h> |
| 15 |
|
| #include <stdio.h> |
| 16 |
|
| |
| 17 |
|
| /*---------------------------------------------------------------------------- |
| 18 |
|
| * Standard 14 font names (§9.6.2.2) |
| 19 |
|
| *--------------------------------------------------------------------------*/ |
| 20 |
|
| |
| 21 |
|
| static const char *std14_names[] = { |
| 22 |
|
| "Helvetica", |
| 23 |
|
| "Helvetica-Bold", |
| 24 |
|
| "Helvetica-Oblique", |
| 25 |
|
| "Helvetica-BoldOblique", |
| 26 |
|
| "Times-Roman", |
| 27 |
|
| "Times-Bold", |
| 28 |
|
| "Times-Italic", |
| 29 |
|
| "Times-BoldItalic", |
| 30 |
|
| "Courier", |
| 31 |
|
| "Courier-Bold", |
| 32 |
|
| "Courier-Oblique", |
| 33 |
|
| "Courier-BoldOblique", |
| 34 |
|
| "Symbol", |
| 35 |
|
| "ZapfDingbats" |
| 36 |
|
| }; |
| 37 |
|
| |
| 38 |
|
| /* std14_names is indexed by pdfmake_std14_font_t (pdfmake_page.h), while the XS |
| 39 |
|
| * layer hands Perl the parallel pdfmake_std14_id_t (pdfmake_font.h). Two enums |
| 40 |
|
| * over one table: if they ever disagree, a caller naming one font gets another |
| 41 |
|
| * and nothing complains. Fail the build on the spot instead. */ |
| 42 |
|
| typedef char pdfmake_std14_enums_agree[ |
| 43 |
|
| (PDFMAKE_STD14_HELVETICA == (int)PDFMAKE_FONT_HELVETICA && |
| 44 |
|
| PDFMAKE_STD14_HELVETICA_BOLD == (int)PDFMAKE_FONT_HELVETICA_BOLD && |
| 45 |
|
| PDFMAKE_STD14_HELVETICA_OBLIQUE == (int)PDFMAKE_FONT_HELVETICA_OBLIQUE && |
| 46 |
|
| PDFMAKE_STD14_HELVETICA_BOLDOBLIQUE == (int)PDFMAKE_FONT_HELVETICA_BOLD_OBLIQUE && |
| 47 |
|
| PDFMAKE_STD14_TIMES_ROMAN == (int)PDFMAKE_FONT_TIMES_ROMAN && |
| 48 |
|
| PDFMAKE_STD14_TIMES_BOLD == (int)PDFMAKE_FONT_TIMES_BOLD && |
| 49 |
|
| PDFMAKE_STD14_TIMES_ITALIC == (int)PDFMAKE_FONT_TIMES_ITALIC && |
| 50 |
|
| PDFMAKE_STD14_TIMES_BOLDITALIC == (int)PDFMAKE_FONT_TIMES_BOLD_ITALIC && |
| 51 |
|
| PDFMAKE_STD14_COURIER == (int)PDFMAKE_FONT_COURIER && |
| 52 |
|
| PDFMAKE_STD14_COURIER_BOLD == (int)PDFMAKE_FONT_COURIER_BOLD && |
| 53 |
|
| PDFMAKE_STD14_COURIER_OBLIQUE == (int)PDFMAKE_FONT_COURIER_OBLIQUE && |
| 54 |
|
| PDFMAKE_STD14_COURIER_BOLDOBLIQUE == (int)PDFMAKE_FONT_COURIER_BOLD_OBLIQUE && |
| 55 |
|
| PDFMAKE_STD14_SYMBOL == (int)PDFMAKE_FONT_SYMBOL && |
| 56 |
|
| PDFMAKE_STD14_ZAPFDINGBATS == (int)PDFMAKE_FONT_ZAPF_DINGBATS && |
| 57 |
|
| PDFMAKE_STD14_COUNT == (int)PDFMAKE_FONT_COUNT && |
| 58 |
|
| (int)PDFMAKE_FONT_COUNT == (int)(sizeof(std14_names) / sizeof(std14_names[0]))) |
| 59 |
|
| ? 1 : -1]; |
| 60 |
|
| |
| 61 |
698 |
| const char *pdfmake_std14_name(pdfmake_std14_font_t font) { |
| 62 |
698 |
▶ | if (font < 0 || font >= PDFMAKE_FONT_COUNT) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (font < 0 || font >= PDFMAKE_FONT_COUNT) return NULL; |
0 |
698 |
698 |
|
| 63 |
698 |
| return std14_names[font]; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
928 |
| int pdfmake_std14_lookup(const char *name) { |
| 67 |
|
| int i; |
| 68 |
928 |
▶ | if (!name) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!name) return -1; |
0 |
928 |
928 |
|
| 69 |
2463 |
▶ | for (i = 0; i < PDFMAKE_FONT_COUNT; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < PDFMAKE_FONT_COUNT; i++) { |
2414 |
49 |
2463 |
|
| 70 |
2414 |
▶ | if (strcmp(name, std14_names[i]) == 0) return i; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(name, std14_names[i]) == 0) return i; |
879 |
1535 |
2414 |
|
| 71 |
|
| } |
| 72 |
49 |
| return -1; |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
|
| /*---------------------------------------------------------------------------- |
| 76 |
|
| * Internal: grow pages array |
| 77 |
|
| *--------------------------------------------------------------------------*/ |
| 78 |
|
| |
| 79 |
710 |
| static int doc_grow_pages(pdfmake_doc_t *doc) { |
| 80 |
710 |
▶ | size_t new_cap = doc->page_cap == 0 ? 4 : doc->page_cap * 2; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t new_cap = doc->page_cap == 0 ? 4 : doc->page_cap * 2; |
7 |
703 |
710 |
|
| 81 |
710 |
| pdfmake_page_t **new_arr = realloc(doc->pages, new_cap * sizeof(pdfmake_page_t *)); |
| 82 |
710 |
▶ | if (!new_arr) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!new_arr) return 0; |
0 |
710 |
710 |
|
| 83 |
710 |
| doc->pages = new_arr; |
| 84 |
710 |
| doc->page_cap = new_cap; |
| 85 |
710 |
| return 1; |
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
|
| /*---------------------------------------------------------------------------- |
| 89 |
|
| * Page creation |
| 90 |
|
| *--------------------------------------------------------------------------*/ |
| 91 |
|
| |
| 92 |
812 |
| pdfmake_page_t *pdfmake_doc_add_page(pdfmake_doc_t *doc, double width, double height) { |
| 93 |
|
| pdfmake_page_t *page; |
| 94 |
812 |
▶ | if (!doc) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!doc) return NULL; |
0 |
812 |
812 |
|
| 95 |
812 |
▶ | if (doc->finalized) return NULL; /* Can't add pages after finalize */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (doc->finalized) return NULL; /* Can't add pages after finalize */ |
0 |
812 |
812 |
|
| 96 |
|
| |
| 97 |
|
| /* Grow pages array if needed */ |
| 98 |
812 |
▶ | if (doc->page_count >= doc->page_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (doc->page_count >= doc->page_cap) { |
710 |
102 |
812 |
|
| 99 |
710 |
▶ | if (!doc_grow_pages(doc)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!doc_grow_pages(doc)) return NULL; |
0 |
710 |
710 |
|
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
|
| /* Allocate page structure from arena */ |
| 103 |
812 |
| page = pdfmake_arena_calloc(doc->arena, sizeof(pdfmake_page_t)); |
| 104 |
812 |
▶ | if (!page) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!page) return NULL; |
0 |
812 |
812 |
|
| 105 |
|
| |
| 106 |
812 |
| page->doc = doc; |
| 107 |
812 |
| page->width = width; |
| 108 |
812 |
| page->height = height; |
| 109 |
812 |
| page->rotation = 0; |
| 110 |
812 |
| page->font_count = 0; |
| 111 |
812 |
| page->image_count = 0; |
| 112 |
812 |
| page->prop_count = 0; |
| 113 |
812 |
| page->extgstate_count = 0; |
| 114 |
812 |
| page->redactions = NULL; |
| 115 |
812 |
| page->redact_count = 0; |
| 116 |
812 |
| page->redact_cap = 0; |
| 117 |
812 |
| page->has_content = 0; |
| 118 |
812 |
| page->contents_num = 0; |
| 119 |
|
| |
| 120 |
|
| /* The /Page dict will be created during finalize when we know the /Pages ref */ |
| 121 |
812 |
| page->page_num = 0; |
| 122 |
|
| |
| 123 |
|
| /* Add to document's page list */ |
| 124 |
812 |
| doc->pages[doc->page_count++] = page; |
| 125 |
|
| |
| 126 |
812 |
| return page; |
| 127 |
|
| } |
| 128 |
|
| |
| 129 |
116 |
| size_t pdfmake_doc_page_count(pdfmake_doc_t *doc) { |
| 130 |
116 |
▶ | return doc ? doc->page_count : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return doc ? doc->page_count : 0; |
116 |
0 |
116 |
|
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
78 |
| pdfmake_page_t *pdfmake_doc_get_page(pdfmake_doc_t *doc, size_t index) { |
| 134 |
78 |
▶ | if (!doc || index >= doc->page_count) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!doc || index >= doc->page_count) return NULL; |
78 |
0 |
78 |
| if (!doc || index >= doc->page_count) return NULL; |
1 |
77 |
78 |
|
| 135 |
77 |
| return doc->pages[index]; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| /*---------------------------------------------------------------------------- |
| 139 |
|
| * Page resources - fonts |
| 140 |
|
| *--------------------------------------------------------------------------*/ |
| 141 |
|
| |
| 142 |
704 |
| uint32_t pdfmake_page_add_font(pdfmake_page_t *page, |
| 143 |
|
| const char *name, |
| 144 |
|
| const char *base_font) { |
| 145 |
|
| pdfmake_doc_t *doc; |
| 146 |
|
| pdfmake_arena_t *arena; |
| 147 |
|
| pdfmake_obj_t font_dict; |
| 148 |
|
| uint32_t type_key; |
| 149 |
|
| uint32_t subtype_key; |
| 150 |
|
| uint32_t basefont_key; |
| 151 |
|
| uint32_t font_num; |
| 152 |
|
| pdfmake_font_entry_t *entry; |
| 153 |
|
| |
| 154 |
704 |
▶ | if (!page || !name || !base_font) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!page || !name || !base_font) return 0; |
704 |
0 |
704 |
| if (!page || !name || !base_font) return 0; |
704 |
0 |
704 |
| if (!page || !name || !base_font) return 0; |
0 |
704 |
704 |
|
| 155 |
704 |
▶ | if (page->font_count >= PDFMAKE_MAX_PAGE_FONTS) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (page->font_count >= PDFMAKE_MAX_PAGE_FONTS) return 0; |
0 |
704 |
704 |
|
| 156 |
|
| |
| 157 |
|
| /* Verify it's a Standard 14 font */ |
| 158 |
704 |
▶ | if (pdfmake_std14_lookup(base_font) < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pdfmake_std14_lookup(base_font) < 0) return 0; |
0 |
704 |
704 |
|
| 159 |
|
| |
| 160 |
704 |
| doc = page->doc; |
| 161 |
704 |
| arena = pdfmake_doc_arena(doc); |
| 162 |
|
| |
| 163 |
|
| /* Create /Font dictionary: |
| 164 |
|
| * << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> */ |
| 165 |
704 |
| font_dict = pdfmake_dict_new(arena); |
| 166 |
704 |
▶ | if (font_dict.kind != PDFMAKE_DICT) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (font_dict.kind != PDFMAKE_DICT) return 0; |
0 |
704 |
704 |
|
| 167 |
|
| |
| 168 |
704 |
| type_key = pdfmake_arena_intern_name(arena, "Type", 4); |
| 169 |
704 |
| subtype_key = pdfmake_arena_intern_name(arena, "Subtype", 7); |
| 170 |
704 |
| basefont_key = pdfmake_arena_intern_name(arena, "BaseFont", 8); |
| 171 |
|
| |
| 172 |
704 |
| pdfmake_dict_set(arena, &font_dict, type_key, pdfmake_name_cstr(arena, "Font")); |
| 173 |
704 |
| pdfmake_dict_set(arena, &font_dict, subtype_key, pdfmake_name_cstr(arena, "Type1")); |
| 174 |
704 |
| pdfmake_dict_set(arena, &font_dict, basefont_key, pdfmake_name_cstr(arena, base_font)); |
| 175 |
|
| |
| 176 |
|
| /* Add font dict as indirect object */ |
| 177 |
704 |
| font_num = pdfmake_doc_add(doc, font_dict); |
| 178 |
704 |
▶ | if (font_num == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (font_num == 0) return 0; |
0 |
704 |
704 |
|
| 179 |
|
| |
| 180 |
|
| /* Record in page's font list */ |
| 181 |
704 |
| entry = &page->fonts[page->font_count++]; |
| 182 |
704 |
| strncpy(entry->name, name, sizeof(entry->name) - 1); |
| 183 |
704 |
| entry->name[sizeof(entry->name) - 1] = '\0'; |
| 184 |
704 |
| entry->font_num = font_num; |
| 185 |
|
| |
| 186 |
704 |
| return font_num; |
| 187 |
|
| } |
| 188 |
|
| |
| 189 |
698 |
| uint32_t pdfmake_page_add_std14_font(pdfmake_page_t *page, |
| 190 |
|
| const char *name, |
| 191 |
|
| pdfmake_std14_font_t font) { |
| 192 |
698 |
| const char *base_font = pdfmake_std14_name(font); |
| 193 |
698 |
▶ | if (!base_font) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!base_font) return 0; |
0 |
698 |
698 |
|
| 194 |
698 |
| return pdfmake_page_add_font(page, name, base_font); |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
3 |
| uint32_t pdfmake_page_add_font_ref(pdfmake_page_t *page, |
| 198 |
|
| const char *name, |
| 199 |
|
| uint32_t font_obj_num) { |
| 200 |
|
| pdfmake_font_entry_t *entry; |
| 201 |
|
| size_t i; |
| 202 |
|
| |
| 203 |
3 |
▶ | if (!page || !name || font_obj_num == 0) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!page || !name || font_obj_num == 0) return 0; |
3 |
0 |
3 |
| if (!page || !name || font_obj_num == 0) return 0; |
3 |
0 |
3 |
| if (!page || !name || font_obj_num == 0) return 0; |
0 |
3 |
3 |
|
| 204 |
|
| |
| 205 |
|
| /* Re-pointing a name that is already on the page is how a font written |
| 206 |
|
| * once and used on several pages gets registered; adding a second entry |
| 207 |
|
| * would emit a duplicate key. */ |
| 208 |
6 |
▶ | for (i = 0; i < page->font_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < page->font_count; i++) { |
3 |
3 |
6 |
|
| 209 |
3 |
▶ | if (strcmp(page->fonts[i].name, name) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(page->fonts[i].name, name) == 0) { |
0 |
3 |
3 |
|
| 210 |
0 |
| page->fonts[i].font_num = font_obj_num; |
| 211 |
0 |
| return font_obj_num; |
| 212 |
|
| } |
| 213 |
|
| } |
| 214 |
|
| |
| 215 |
3 |
▶ | if (page->font_count >= PDFMAKE_MAX_PAGE_FONTS) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (page->font_count >= PDFMAKE_MAX_PAGE_FONTS) return 0; |
0 |
3 |
3 |
|
| 216 |
|
| |
| 217 |
3 |
| entry = &page->fonts[page->font_count++]; |
| 218 |
3 |
| strncpy(entry->name, name, sizeof(entry->name) - 1); |
| 219 |
3 |
| entry->name[sizeof(entry->name) - 1] = '\0'; |
| 220 |
3 |
| entry->font_num = font_obj_num; |
| 221 |
|
| |
| 222 |
3 |
| return font_obj_num; |
| 223 |
|
| } |
| 224 |
|
| |
| 225 |
3 |
| int pdfmake_page_add_extgstate(pdfmake_page_t *page, |
| 226 |
|
| const char *name, |
| 227 |
|
| uint32_t extgstate_obj_num) |
| 228 |
|
| { |
| 229 |
|
| pdfmake_extgstate_entry_t *entry; |
| 230 |
|
| |
| 231 |
3 |
▶ | if (!page || !name || extgstate_obj_num == 0) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!page || !name || extgstate_obj_num == 0) return -1; |
3 |
0 |
3 |
| if (!page || !name || extgstate_obj_num == 0) return -1; |
3 |
0 |
3 |
| if (!page || !name || extgstate_obj_num == 0) return -1; |
0 |
3 |
3 |
|
| 232 |
3 |
▶ | if (page->extgstate_count >= PDFMAKE_MAX_PAGE_EXTGSTATES) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (page->extgstate_count >= PDFMAKE_MAX_PAGE_EXTGSTATES) return -1; |
0 |
3 |
3 |
|
| 233 |
|
| |
| 234 |
3 |
| entry = &page->extgstates[page->extgstate_count++]; |
| 235 |
3 |
| strncpy(entry->name, name, sizeof(entry->name) - 1); |
| 236 |
3 |
| entry->name[sizeof(entry->name) - 1] = '\0'; |
| 237 |
3 |
| entry->extgstate_num = extgstate_obj_num; |
| 238 |
|
| |
| 239 |
3 |
| return (int)(page->extgstate_count - 1); |
| 240 |
|
| } |
| 241 |
|
| |
| 242 |
|
| /*---------------------------------------------------------------------------- |
| 243 |
|
| * Content stream |
| 244 |
|
| *--------------------------------------------------------------------------*/ |
| 245 |
|
| |
| 246 |
662 |
| pdfmake_err_t pdfmake_page_set_content(pdfmake_page_t *page, |
| 247 |
|
| const uint8_t *data, |
| 248 |
|
| size_t len) { |
| 249 |
|
| pdfmake_doc_t *doc; |
| 250 |
|
| pdfmake_arena_t *arena; |
| 251 |
|
| pdfmake_obj_t stream; |
| 252 |
|
| uint32_t contents_num; |
| 253 |
|
| |
| 254 |
662 |
▶ | if (!page || !data) return PDFMAKE_EINVAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!page || !data) return PDFMAKE_EINVAL; |
662 |
0 |
662 |
| if (!page || !data) return PDFMAKE_EINVAL; |
0 |
662 |
662 |
|
| 255 |
|
| |
| 256 |
662 |
| doc = page->doc; |
| 257 |
662 |
| arena = pdfmake_doc_arena(doc); |
| 258 |
|
| |
| 259 |
|
| /* Create a stream object with the content */ |
| 260 |
662 |
| stream = pdfmake_stream_new(arena); |
| 261 |
662 |
▶ | if (stream.kind != PDFMAKE_STREAM) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (stream.kind != PDFMAKE_STREAM) return PDFMAKE_ENOMEM; |
0 |
662 |
662 |
|
| 262 |
|
| |
| 263 |
662 |
▶ | if (!pdfmake_stream_set_data(arena, &stream, data, len)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pdfmake_stream_set_data(arena, &stream, data, len)) { |
0 |
662 |
662 |
|
| 264 |
0 |
| return PDFMAKE_ENOMEM; |
| 265 |
|
| } |
| 266 |
|
| |
| 267 |
|
| /* Add as indirect object */ |
| 268 |
662 |
| contents_num = pdfmake_doc_add(doc, stream); |
| 269 |
662 |
▶ | if (contents_num == 0) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (contents_num == 0) return PDFMAKE_ENOMEM; |
0 |
662 |
662 |
|
| 270 |
|
| |
| 271 |
662 |
| page->contents_num = contents_num; |
| 272 |
662 |
| page->has_content = 1; |
| 273 |
|
| |
| 274 |
662 |
| return PDFMAKE_OK; |
| 275 |
|
| } |
| 276 |
|
| |
| 277 |
0 |
| pdfmake_err_t pdfmake_page_set_content_str(pdfmake_page_t *page, |
| 278 |
|
| const char *content) { |
| 279 |
0 |
▶ | if (!content) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!content) return PDFMAKE_EINVAL; |
0 |
0 |
0 |
|
| 280 |
0 |
| return pdfmake_page_set_content(page, (const uint8_t *)content, strlen(content)); |
| 281 |
|
| } |
| 282 |
|
| |
| 283 |
0 |
| pdfmake_err_t pdfmake_page_append_content(pdfmake_page_t *page, |
| 284 |
|
| const uint8_t *data, |
| 285 |
|
| size_t len) |
| 286 |
|
| { |
| 287 |
|
| pdfmake_doc_t *doc; |
| 288 |
|
| pdfmake_arena_t *arena; |
| 289 |
|
| pdfmake_obj_t *existing; |
| 290 |
|
| pdfmake_stream_t *stm; |
| 291 |
|
| const uint8_t *old_bytes; |
| 292 |
|
| size_t old_len; |
| 293 |
|
| size_t cap; |
| 294 |
|
| uint8_t *combined; |
| 295 |
|
| size_t off; |
| 296 |
|
| |
| 297 |
0 |
▶ | if (!page || !data) return PDFMAKE_EINVAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!page || !data) return PDFMAKE_EINVAL; |
0 |
0 |
0 |
| if (!page || !data) return PDFMAKE_EINVAL; |
0 |
0 |
0 |
|
| 298 |
0 |
▶ | if (!page->has_content || page->contents_num == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!page->has_content || page->contents_num == 0) { |
0 |
0 |
0 |
| if (!page->has_content || page->contents_num == 0) { |
0 |
0 |
0 |
|
| 299 |
0 |
| return pdfmake_page_set_content(page, data, len); |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
0 |
| doc = page->doc; |
| 303 |
0 |
| arena = pdfmake_doc_arena(doc); |
| 304 |
|
| |
| 305 |
|
| /* Fetch the existing content stream so we can concatenate. Its raw |
| 306 |
|
| * bytes may be compressed (e.g. /Filter /FlateDecode imported from a |
| 307 |
|
| * source PDF); we decompress before concatenating so the combined |
| 308 |
|
| * stream is plain and we don't have to synthesize the filter chain. */ |
| 309 |
0 |
| existing = pdfmake_doc_get(doc, page->contents_num); |
| 310 |
0 |
▶ | if (!existing || existing->kind != PDFMAKE_STREAM) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!existing || existing->kind != PDFMAKE_STREAM) { |
0 |
0 |
0 |
| if (!existing || existing->kind != PDFMAKE_STREAM) { |
0 |
0 |
0 |
|
| 311 |
0 |
| return pdfmake_page_set_content(page, data, len); |
| 312 |
|
| } |
| 313 |
|
| |
| 314 |
|
| /* Use the stream's raw bytes directly. Pages created via |
| 315 |
|
| * pdfmake_page_set_content always store plain (unfiltered) content, |
| 316 |
|
| * and pdfmake_doc_import_page feeds pre-decoded bytes in through the |
| 317 |
|
| * same path — so the raw pointer is the content we need, regardless |
| 318 |
|
| * of how the page was built. */ |
| 319 |
0 |
| stm = existing->as.stream; |
| 320 |
0 |
| old_bytes = stm->raw; |
| 321 |
0 |
| old_len = stm->raw_len; |
| 322 |
|
| |
| 323 |
|
| /* Build `<<existing>>\nq\n<<overlay>>\nQ\n` so overlay starts from a |
| 324 |
|
| * clean graphics state but the existing content is preserved. */ |
| 325 |
0 |
| cap = old_len + 1 + 2 + 1 + len + 1 + 2 + 1; |
| 326 |
0 |
| combined = pdfmake_arena_alloc(arena, cap); |
| 327 |
0 |
▶ | if (!combined) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!combined) return PDFMAKE_ENOMEM; |
0 |
0 |
0 |
|
| 328 |
|
| |
| 329 |
0 |
| off = 0; |
| 330 |
0 |
| memcpy(combined + off, old_bytes, old_len); off += old_len; |
| 331 |
0 |
▶ | if (old_len == 0 || old_bytes[old_len - 1] != '\n') combined[off++] = '\n'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (old_len == 0 || old_bytes[old_len - 1] != '\n') combined[off++] = '\n'; |
0 |
0 |
0 |
| if (old_len == 0 || old_bytes[old_len - 1] != '\n') combined[off++] = '\n'; |
0 |
0 |
0 |
|
| 332 |
0 |
| combined[off++] = 'q'; combined[off++] = '\n'; |
| 333 |
0 |
| memcpy(combined + off, data, len); off += len; |
| 334 |
0 |
▶ | if (len == 0 || data[len - 1] != '\n') combined[off++] = '\n'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 0 || data[len - 1] != '\n') combined[off++] = '\n'; |
0 |
0 |
0 |
| if (len == 0 || data[len - 1] != '\n') combined[off++] = '\n'; |
0 |
0 |
0 |
|
| 335 |
0 |
| combined[off++] = 'Q'; combined[off++] = '\n'; |
| 336 |
|
| |
| 337 |
0 |
| return pdfmake_page_set_content(page, combined, off); |
| 338 |
|
| } |
| 339 |
|
| |
| 340 |
|
| /*---------------------------------------------------------------------------- |
| 341 |
|
| * Internal: build Resources dictionary for a page |
| 342 |
|
| *--------------------------------------------------------------------------*/ |
| 343 |
|
| |
| 344 |
636 |
| static pdfmake_obj_t build_resources_dict(pdfmake_page_t *page) { |
| 345 |
636 |
| pdfmake_doc_t *doc = page->doc; |
| 346 |
636 |
| pdfmake_arena_t *arena = pdfmake_doc_arena(doc); |
| 347 |
|
| pdfmake_obj_t resources; |
| 348 |
|
| pdfmake_obj_t font_dict; |
| 349 |
|
| pdfmake_obj_t xobj_dict; |
| 350 |
|
| pdfmake_obj_t prop_dict; |
| 351 |
|
| pdfmake_obj_t gs_dict; |
| 352 |
|
| size_t i; |
| 353 |
|
| uint32_t font_key; |
| 354 |
|
| uint32_t xobj_key; |
| 355 |
|
| uint32_t prop_key; |
| 356 |
|
| uint32_t gs_key; |
| 357 |
|
| |
| 358 |
636 |
| resources = pdfmake_dict_new(arena); |
| 359 |
636 |
▶ | if (resources.kind != PDFMAKE_DICT) return resources; |
Branch: 1 branch
| Branch | True | False | Total |
| if (resources.kind != PDFMAKE_DICT) return resources; |
0 |
636 |
636 |
|
| 360 |
|
| |
| 361 |
|
| /* Build /Font subdictionary if page has fonts */ |
| 362 |
636 |
▶ | if (page->font_count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (page->font_count > 0) { |
566 |
70 |
636 |
|
| 363 |
566 |
| font_dict = pdfmake_dict_new(arena); |
| 364 |
566 |
▶ | if (font_dict.kind != PDFMAKE_DICT) return font_dict; |
Branch: 1 branch
| Branch | True | False | Total |
| if (font_dict.kind != PDFMAKE_DICT) return font_dict; |
0 |
566 |
566 |
|
| 365 |
|
| |
| 366 |
1253 |
▶ | for (i = 0; i < page->font_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < page->font_count; i++) { |
687 |
566 |
1253 |
|
| 367 |
687 |
| pdfmake_font_entry_t *entry = &page->fonts[i]; |
| 368 |
687 |
| uint32_t key = pdfmake_arena_intern_name(arena, entry->name, strlen(entry->name)); |
| 369 |
687 |
| pdfmake_obj_t ref = pdfmake_ref(entry->font_num, 0); |
| 370 |
687 |
| pdfmake_dict_set(arena, &font_dict, key, ref); |
| 371 |
|
| } |
| 372 |
|
| |
| 373 |
566 |
| font_key = pdfmake_arena_intern_name(arena, "Font", 4); |
| 374 |
566 |
| pdfmake_dict_set(arena, &resources, font_key, font_dict); |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
|
| /* Build /XObject subdictionary if page has images */ |
| 378 |
636 |
▶ | if (page->image_count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (page->image_count > 0) { |
12 |
624 |
636 |
|
| 379 |
12 |
| xobj_dict = pdfmake_dict_new(arena); |
| 380 |
12 |
▶ | if (xobj_dict.kind != PDFMAKE_DICT) return xobj_dict; |
Branch: 1 branch
| Branch | True | False | Total |
| if (xobj_dict.kind != PDFMAKE_DICT) return xobj_dict; |
0 |
12 |
12 |
|
| 381 |
|
| |
| 382 |
29 |
▶ | for (i = 0; i < page->image_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < page->image_count; i++) { |
17 |
12 |
29 |
|
| 383 |
17 |
| pdfmake_image_entry_t *entry = &page->images[i]; |
| 384 |
17 |
| uint32_t key = pdfmake_arena_intern_name(arena, entry->name, strlen(entry->name)); |
| 385 |
17 |
| pdfmake_obj_t ref = pdfmake_ref(entry->image_num, 0); |
| 386 |
17 |
| pdfmake_dict_set(arena, &xobj_dict, key, ref); |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
12 |
| xobj_key = pdfmake_arena_intern_name(arena, "XObject", 7); |
| 390 |
12 |
| pdfmake_dict_set(arena, &resources, xobj_key, xobj_dict); |
| 391 |
|
| } |
| 392 |
|
| |
| 393 |
|
| /* Build /Properties subdictionary if page has OCG references */ |
| 394 |
636 |
▶ | if (page->prop_count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (page->prop_count > 0) { |
5 |
631 |
636 |
|
| 395 |
5 |
| prop_dict = pdfmake_dict_new(arena); |
| 396 |
5 |
▶ | if (prop_dict.kind != PDFMAKE_DICT) return prop_dict; |
Branch: 1 branch
| Branch | True | False | Total |
| if (prop_dict.kind != PDFMAKE_DICT) return prop_dict; |
0 |
5 |
5 |
|
| 397 |
|
| |
| 398 |
13 |
▶ | for (i = 0; i < page->prop_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < page->prop_count; i++) { |
8 |
5 |
13 |
|
| 399 |
8 |
| pdfmake_prop_entry_t *entry = &page->properties[i]; |
| 400 |
8 |
| uint32_t key = pdfmake_arena_intern_name(arena, entry->name, strlen(entry->name)); |
| 401 |
8 |
| pdfmake_obj_t ref = pdfmake_ref(entry->prop_num, 0); |
| 402 |
8 |
| pdfmake_dict_set(arena, &prop_dict, key, ref); |
| 403 |
|
| } |
| 404 |
|
| |
| 405 |
5 |
| prop_key = pdfmake_arena_intern_name(arena, "Properties", 10); |
| 406 |
5 |
| pdfmake_dict_set(arena, &resources, prop_key, prop_dict); |
| 407 |
|
| } |
| 408 |
|
| |
| 409 |
|
| /* Build /ExtGState subdictionary if page has graphics states */ |
| 410 |
636 |
▶ | if (page->extgstate_count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (page->extgstate_count > 0) { |
1 |
635 |
636 |
|
| 411 |
1 |
| gs_dict = pdfmake_dict_new(arena); |
| 412 |
1 |
▶ | if (gs_dict.kind != PDFMAKE_DICT) return gs_dict; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gs_dict.kind != PDFMAKE_DICT) return gs_dict; |
0 |
1 |
1 |
|
| 413 |
|
| |
| 414 |
2 |
▶ | for (i = 0; i < page->extgstate_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < page->extgstate_count; i++) { |
1 |
1 |
2 |
|
| 415 |
1 |
| pdfmake_extgstate_entry_t *entry = &page->extgstates[i]; |
| 416 |
1 |
| uint32_t key = pdfmake_arena_intern_name(arena, entry->name, strlen(entry->name)); |
| 417 |
1 |
| pdfmake_obj_t ref = pdfmake_ref(entry->extgstate_num, 0); |
| 418 |
1 |
| pdfmake_dict_set(arena, &gs_dict, key, ref); |
| 419 |
|
| } |
| 420 |
|
| |
| 421 |
1 |
| gs_key = pdfmake_arena_intern_name(arena, "ExtGState", 9); |
| 422 |
1 |
| pdfmake_dict_set(arena, &resources, gs_key, gs_dict); |
| 423 |
|
| } |
| 424 |
|
| |
| 425 |
636 |
| return resources; |
| 426 |
|
| } |
| 427 |
|
| |
| 428 |
|
| /*---------------------------------------------------------------------------- |
| 429 |
|
| * Document finalization |
| 430 |
|
| *--------------------------------------------------------------------------*/ |
| 431 |
|
| |
| 432 |
0 |
| int pdfmake_doc_is_finalized(pdfmake_doc_t *doc) { |
| 433 |
0 |
▶ | return doc ? doc->finalized : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return doc ? doc->finalized : 0; |
0 |
0 |
0 |
|
| 434 |
|
| } |
| 435 |
|
| |
| 436 |
587 |
| pdfmake_err_t pdfmake_doc_finalize(pdfmake_doc_t *doc) { |
| 437 |
|
| pdfmake_arena_t *arena; |
| 438 |
|
| uint32_t type_key; |
| 439 |
|
| uint32_t kids_key; |
| 440 |
|
| uint32_t count_key; |
| 441 |
|
| uint32_t parent_key; |
| 442 |
|
| uint32_t mediabox_key; |
| 443 |
|
| uint32_t resources_key; |
| 444 |
|
| uint32_t contents_key; |
| 445 |
|
| uint32_t pages_key; |
| 446 |
|
| pdfmake_obj_t pages_dict; |
| 447 |
|
| pdfmake_obj_t kids; |
| 448 |
|
| uint32_t pages_num; |
| 449 |
|
| pdfmake_obj_t pages_ref; |
| 450 |
|
| size_t i; |
| 451 |
|
| size_t j; |
| 452 |
|
| pdfmake_page_t *page; |
| 453 |
|
| pdfmake_obj_t page_dict; |
| 454 |
|
| pdfmake_obj_t mediabox; |
| 455 |
|
| pdfmake_obj_t resources; |
| 456 |
|
| pdfmake_obj_t contents_ref; |
| 457 |
|
| uint32_t rotate_key; |
| 458 |
|
| uint32_t page_num; |
| 459 |
|
| pdfmake_obj_t *pages_obj; |
| 460 |
|
| pdfmake_obj_t catalog; |
| 461 |
|
| pdfmake_outline_item_t *outline; |
| 462 |
|
| uint32_t outlines_num; |
| 463 |
|
| uint32_t outlines_key; |
| 464 |
|
| pdfmake_form_t *form; |
| 465 |
|
| uint32_t acroform_key; |
| 466 |
|
| uint32_t catalog_num; |
| 467 |
|
| pdfmake_err_t ocerr; |
| 468 |
|
| pdfmake_err_t aterr; |
| 469 |
|
| pdfmake_err_t tagerr; |
| 470 |
|
| pdfmake_obj_t *page_obj; |
| 471 |
|
| pdfmake_obj_t annots_arr; |
| 472 |
|
| uint32_t annots_key; |
| 473 |
|
| |
| 474 |
587 |
▶ | if (!doc) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!doc) return PDFMAKE_EINVAL; |
0 |
587 |
587 |
|
| 475 |
587 |
▶ | if (doc->finalized) return PDFMAKE_OK; /* Already finalized */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (doc->finalized) return PDFMAKE_OK; /* Already finalized */ |
0 |
587 |
587 |
|
| 476 |
|
| |
| 477 |
587 |
| arena = pdfmake_doc_arena(doc); |
| 478 |
|
| |
| 479 |
|
| /* Intern common keys */ |
| 480 |
587 |
| type_key = pdfmake_arena_intern_name(arena, "Type", 4); |
| 481 |
587 |
| kids_key = pdfmake_arena_intern_name(arena, "Kids", 4); |
| 482 |
587 |
| count_key = pdfmake_arena_intern_name(arena, "Count", 5); |
| 483 |
587 |
| parent_key = pdfmake_arena_intern_name(arena, "Parent", 6); |
| 484 |
587 |
| mediabox_key = pdfmake_arena_intern_name(arena, "MediaBox", 8); |
| 485 |
587 |
| resources_key = pdfmake_arena_intern_name(arena, "Resources", 9); |
| 486 |
587 |
| contents_key = pdfmake_arena_intern_name(arena, "Contents", 8); |
| 487 |
587 |
| pages_key = pdfmake_arena_intern_name(arena, "Pages", 5); |
| 488 |
|
| |
| 489 |
|
| /* Create /Pages dictionary first (we need its object number for /Parent) */ |
| 490 |
587 |
| pages_dict = pdfmake_dict_new(arena); |
| 491 |
587 |
▶ | if (pages_dict.kind != PDFMAKE_DICT) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pages_dict.kind != PDFMAKE_DICT) return PDFMAKE_ENOMEM; |
0 |
587 |
587 |
|
| 492 |
|
| |
| 493 |
587 |
| pdfmake_dict_set(arena, &pages_dict, type_key, pdfmake_name_cstr(arena, "Pages")); |
| 494 |
|
| |
| 495 |
|
| /* Placeholder Kids array - will be filled below */ |
| 496 |
587 |
| kids = pdfmake_array_new(arena); |
| 497 |
587 |
▶ | if (kids.kind != PDFMAKE_ARRAY) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (kids.kind != PDFMAKE_ARRAY) return PDFMAKE_ENOMEM; |
0 |
587 |
587 |
|
| 498 |
|
| |
| 499 |
|
| /* Add /Pages as indirect object */ |
| 500 |
587 |
| pages_num = pdfmake_doc_add(doc, pages_dict); |
| 501 |
587 |
▶ | if (pages_num == 0) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pages_num == 0) return PDFMAKE_ENOMEM; |
0 |
587 |
587 |
|
| 502 |
587 |
| doc->pages_num = pages_num; |
| 503 |
|
| |
| 504 |
587 |
| pages_ref = pdfmake_ref(pages_num, 0); |
| 505 |
|
| |
| 506 |
|
| /* Create /Page dicts for each page */ |
| 507 |
1257 |
▶ | for (i = 0; i < doc->page_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < doc->page_count; i++) { |
670 |
587 |
1257 |
|
| 508 |
670 |
| page = doc->pages[i]; |
| 509 |
|
| |
| 510 |
670 |
| page_dict = pdfmake_dict_new(arena); |
| 511 |
670 |
▶ | if (page_dict.kind != PDFMAKE_DICT) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (page_dict.kind != PDFMAKE_DICT) return PDFMAKE_ENOMEM; |
0 |
670 |
670 |
|
| 512 |
|
| |
| 513 |
|
| /* /Type /Page */ |
| 514 |
670 |
| pdfmake_dict_set(arena, &page_dict, type_key, pdfmake_name_cstr(arena, "Page")); |
| 515 |
|
| |
| 516 |
|
| /* /Parent -> /Pages ref */ |
| 517 |
670 |
| pdfmake_dict_set(arena, &page_dict, parent_key, pages_ref); |
| 518 |
|
| |
| 519 |
|
| /* /MediaBox [0 0 width height] */ |
| 520 |
670 |
| mediabox = pdfmake_array_new(arena); |
| 521 |
670 |
| pdfmake_array_push(arena, &mediabox, pdfmake_int(0)); |
| 522 |
670 |
| pdfmake_array_push(arena, &mediabox, pdfmake_int(0)); |
| 523 |
670 |
| pdfmake_array_push(arena, &mediabox, pdfmake_real(page->width)); |
| 524 |
670 |
| pdfmake_array_push(arena, &mediabox, pdfmake_real(page->height)); |
| 525 |
670 |
| pdfmake_dict_set(arena, &page_dict, mediabox_key, mediabox); |
| 526 |
|
| |
| 527 |
|
| /* /Resources — imported pages carry a verbatim dict; otherwise |
| 528 |
|
| * compose one from per-page resource arrays. */ |
| 529 |
670 |
▶ | if (page->imported_resources) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (page->imported_resources) { |
34 |
636 |
670 |
|
| 530 |
34 |
| resources.kind = PDFMAKE_DICT; |
| 531 |
34 |
| resources.as.dict = page->imported_resources; |
| 532 |
|
| } else { |
| 533 |
636 |
| resources = build_resources_dict(page); |
| 534 |
|
| } |
| 535 |
670 |
| pdfmake_dict_set(arena, &page_dict, resources_key, resources); |
| 536 |
|
| |
| 537 |
|
| /* /Contents (if set) */ |
| 538 |
670 |
▶ | if (page->has_content) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (page->has_content) { |
643 |
27 |
670 |
|
| 539 |
643 |
| contents_ref = pdfmake_ref(page->contents_num, 0); |
| 540 |
643 |
| pdfmake_dict_set(arena, &page_dict, contents_key, contents_ref); |
| 541 |
|
| } |
| 542 |
|
| |
| 543 |
|
| /* /Rotate (if non-zero) */ |
| 544 |
670 |
▶ | if (page->rotation != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (page->rotation != 0) { |
1 |
669 |
670 |
|
| 545 |
1 |
| rotate_key = pdfmake_arena_intern_name(arena, "Rotate", 6); |
| 546 |
1 |
| pdfmake_dict_set(arena, &page_dict, rotate_key, |
| 547 |
1 |
| pdfmake_int(page->rotation)); |
| 548 |
|
| } |
| 549 |
|
| |
| 550 |
|
| /* Add /Page as indirect object */ |
| 551 |
670 |
| page_num = pdfmake_doc_add(doc, page_dict); |
| 552 |
670 |
▶ | if (page_num == 0) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (page_num == 0) return PDFMAKE_ENOMEM; |
0 |
670 |
670 |
|
| 553 |
670 |
| page->page_num = page_num; |
| 554 |
|
| |
| 555 |
|
| /* Add to Kids array */ |
| 556 |
670 |
| pdfmake_array_push(arena, &kids, pdfmake_ref(page_num, 0)); |
| 557 |
|
| } |
| 558 |
|
| |
| 559 |
|
| /* Update /Pages dict with Kids and Count */ |
| 560 |
587 |
| pages_obj = pdfmake_doc_get(doc, pages_num); |
| 561 |
587 |
▶ | if (!pages_obj || pages_obj->kind != PDFMAKE_DICT) return PDFMAKE_EINVAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!pages_obj || pages_obj->kind != PDFMAKE_DICT) return PDFMAKE_EINVAL; |
587 |
0 |
587 |
| if (!pages_obj || pages_obj->kind != PDFMAKE_DICT) return PDFMAKE_EINVAL; |
0 |
587 |
587 |
|
| 562 |
|
| |
| 563 |
587 |
| pdfmake_dict_set(arena, pages_obj, kids_key, kids); |
| 564 |
587 |
| pdfmake_dict_set(arena, pages_obj, count_key, pdfmake_int((int64_t)doc->page_count)); |
| 565 |
|
| |
| 566 |
|
| /* Create /Catalog dictionary */ |
| 567 |
587 |
| catalog = pdfmake_dict_new(arena); |
| 568 |
587 |
▶ | if (catalog.kind != PDFMAKE_DICT) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (catalog.kind != PDFMAKE_DICT) return PDFMAKE_ENOMEM; |
0 |
587 |
587 |
|
| 569 |
|
| |
| 570 |
587 |
| pdfmake_dict_set(arena, &catalog, type_key, pdfmake_name_cstr(arena, "Catalog")); |
| 571 |
587 |
| pdfmake_dict_set(arena, &catalog, pages_key, pages_ref); |
| 572 |
|
| |
| 573 |
|
| /* Add outline if present */ |
| 574 |
587 |
| outline = pdfmake_doc_get_outline(doc); |
| 575 |
587 |
▶ | if (outline) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (outline) { |
6 |
581 |
587 |
|
| 576 |
6 |
| outlines_num = pdfmake_outline_finalize(doc, outline); |
| 577 |
6 |
▶ | if (outlines_num > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (outlines_num > 0) { |
6 |
0 |
6 |
|
| 578 |
6 |
| outlines_key = pdfmake_arena_intern_name(arena, "Outlines", 8); |
| 579 |
6 |
| pdfmake_dict_set(arena, &catalog, outlines_key, pdfmake_ref(outlines_num, 0)); |
| 580 |
|
| } |
| 581 |
|
| } |
| 582 |
|
| |
| 583 |
|
| /* Add AcroForm if present */ |
| 584 |
587 |
| form = pdfmake_doc_get_form(doc); |
| 585 |
587 |
▶ | if (form && form->form_num > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (form && form->form_num > 0) { |
15 |
572 |
587 |
| if (form && form->form_num > 0) { |
11 |
4 |
15 |
|
| 586 |
11 |
| acroform_key = pdfmake_arena_intern_name(arena, "AcroForm", 8); |
| 587 |
11 |
| pdfmake_dict_set(arena, &catalog, acroform_key, pdfmake_ref(form->form_num, 0)); |
| 588 |
|
| } |
| 589 |
|
| |
| 590 |
|
| /* Add /Catalog as indirect object */ |
| 591 |
587 |
| catalog_num = pdfmake_doc_add(doc, catalog); |
| 592 |
587 |
▶ | if (catalog_num == 0) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (catalog_num == 0) return PDFMAKE_ENOMEM; |
0 |
587 |
587 |
|
| 593 |
|
| |
| 594 |
|
| /* Set as document root */ |
| 595 |
587 |
| pdfmake_doc_set_root(doc, catalog_num, 0); |
| 596 |
|
| |
| 597 |
|
| /* Add /OCProperties if document has layers */ |
| 598 |
587 |
▶ | if (doc->ocg_count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (doc->ocg_count > 0) { |
5 |
582 |
587 |
|
| 599 |
5 |
| ocerr = pdfmake_doc_write_ocproperties(doc); |
| 600 |
5 |
▶ | if (ocerr != PDFMAKE_OK) return ocerr; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ocerr != PDFMAKE_OK) return ocerr; |
0 |
5 |
5 |
|
| 601 |
|
| } |
| 602 |
|
| |
| 603 |
|
| /* Add /Names/EmbeddedFiles if document has attachments */ |
| 604 |
587 |
▶ | if (doc->attach_count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (doc->attach_count > 0) { |
3 |
584 |
587 |
|
| 605 |
3 |
| aterr = pdfmake_doc_write_attachments(doc); |
| 606 |
3 |
▶ | if (aterr != PDFMAKE_OK) return aterr; |
Branch: 1 branch
| Branch | True | False | Total |
| if (aterr != PDFMAKE_OK) return aterr; |
0 |
3 |
3 |
|
| 607 |
|
| } |
| 608 |
|
| |
| 609 |
|
| /* Add structure tree if tagged */ |
| 610 |
|
| { |
| 611 |
587 |
| tagerr = pdfmake_doc_write_struct_tree(doc); |
| 612 |
587 |
▶ | if (tagerr != PDFMAKE_OK) return tagerr; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tagerr != PDFMAKE_OK) return tagerr; |
0 |
587 |
587 |
|
| 613 |
|
| } |
| 614 |
|
| |
| 615 |
|
| /* Add /Annots arrays to pages that have annotations */ |
| 616 |
1257 |
▶ | for (i = 0; i < doc->page_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < doc->page_count; i++) { |
670 |
587 |
1257 |
|
| 617 |
670 |
| page = doc->pages[i]; |
| 618 |
670 |
▶ | if (page->annot_count > 0 && page->page_num > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (page->annot_count > 0 && page->page_num > 0) { |
26 |
644 |
670 |
| if (page->annot_count > 0 && page->page_num > 0) { |
26 |
0 |
26 |
|
| 619 |
26 |
| page_obj = pdfmake_doc_get(doc, page->page_num); |
| 620 |
26 |
▶ | if (page_obj && page_obj->kind == PDFMAKE_DICT) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (page_obj && page_obj->kind == PDFMAKE_DICT) { |
26 |
0 |
26 |
| if (page_obj && page_obj->kind == PDFMAKE_DICT) { |
26 |
0 |
26 |
|
| 621 |
26 |
| annots_arr = pdfmake_array_new(arena); |
| 622 |
96 |
▶ | for (j = 0; j < page->annot_count; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < page->annot_count; j++) { |
70 |
26 |
96 |
|
| 623 |
70 |
| pdfmake_array_push(arena, &annots_arr, |
| 624 |
70 |
| pdfmake_ref(page->annots[j], 0)); |
| 625 |
|
| } |
| 626 |
26 |
| annots_key = pdfmake_arena_intern_name(arena, "Annots", 6); |
| 627 |
26 |
| pdfmake_dict_set(arena, page_obj, annots_key, annots_arr); |
| 628 |
|
| } |
| 629 |
|
| } |
| 630 |
|
| } |
| 631 |
|
| |
| 632 |
587 |
| doc->finalized = 1; |
| 633 |
587 |
| return PDFMAKE_OK; |
| 634 |
|
| } |