| 1 |
|
| /* |
| 2 |
|
| * pdfmake_tag.c — Tagged PDF / Logical Structure |
| 3 |
|
| * |
| 4 |
|
| * §14.6 Marked Content, §14.7 Logical Structure, §14.8 Tagged PDF |
| 5 |
|
| */ |
| 6 |
|
| |
| 7 |
|
| #include "pdfmake_tag.h" |
| 8 |
|
| #include "pdfmake_page.h" |
| 9 |
|
| #include "pdfmake_arena.h" |
| 10 |
|
| #include <stdlib.h> |
| 11 |
|
| #include <string.h> |
| 12 |
|
| #include <stdio.h> |
| 13 |
|
| |
| 14 |
|
| /* ── Type names ────────────────────────────────────────── */ |
| 15 |
|
| |
| 16 |
|
| static const char *_struct_type_names[] = { |
| 17 |
|
| "Document", "Part", "Art", "Sect", "Div", |
| 18 |
|
| "BlockQuote", "Caption", "TOC", "TOCI", |
| 19 |
|
| "P", "H", "H1", "H2", "H3", "H4", "H5", "H6", |
| 20 |
|
| "L", "LI", "Lbl", "LBody", |
| 21 |
|
| "Table", "TR", "TH", "TD", "THead", "TBody", "TFoot", |
| 22 |
|
| "Span", "Quote", "Note", "Reference", "Code", "Link", "Annot", |
| 23 |
|
| "Figure", "Formula", "Form", |
| 24 |
|
| }; |
| 25 |
|
| |
| 26 |
11 |
| const char *pdfmake_struct_type_name(pdfmake_struct_type_t type) { |
| 27 |
11 |
▶ | if (type < 0 || type >= PDFMAKE_TAG_COUNT) return "Div"; |
Branch: 1 branch
| Branch | True | False | Total |
| if (type < 0 || type >= PDFMAKE_TAG_COUNT) return "Div"; |
0 |
11 |
11 |
|
| 28 |
11 |
| return _struct_type_names[type]; |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
5 |
| int pdfmake_struct_type_lookup(const char *name) { |
| 32 |
|
| int i; |
| 33 |
5 |
▶ | if (!name) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!name) return -1; |
0 |
5 |
5 |
|
| 34 |
80 |
▶ | for (i = 0; i < PDFMAKE_TAG_COUNT; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < PDFMAKE_TAG_COUNT; i++) { |
80 |
0 |
80 |
|
| 35 |
80 |
▶ | if (strcmp(name, _struct_type_names[i]) == 0) return i; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(name, _struct_type_names[i]) == 0) return i; |
5 |
75 |
80 |
|
| 36 |
|
| } |
| 37 |
0 |
| return -1; |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
|
| /* ── Document-level ────────────────────────────────────── */ |
| 41 |
|
| |
| 42 |
4 |
| pdfmake_struct_tree_t *pdfmake_doc_create_struct_tree(pdfmake_doc_t *doc) { |
| 43 |
|
| pdfmake_struct_tree_t *tree; |
| 44 |
|
| |
| 45 |
4 |
▶ | if (!doc) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!doc) return NULL; |
0 |
4 |
4 |
|
| 46 |
|
| |
| 47 |
|
| /* Reuse if already exists on this document */ |
| 48 |
4 |
▶ | if (doc->struct_tree) return (pdfmake_struct_tree_t *)doc->struct_tree; |
Branch: 1 branch
| Branch | True | False | Total |
| if (doc->struct_tree) return (pdfmake_struct_tree_t *)doc->struct_tree; |
0 |
4 |
4 |
|
| 49 |
|
| |
| 50 |
4 |
| tree = calloc(1, sizeof(*tree)); |
| 51 |
4 |
▶ | if (!tree) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tree) return NULL; |
0 |
4 |
4 |
|
| 52 |
|
| |
| 53 |
|
| /* Create root Document element */ |
| 54 |
4 |
| tree->root = calloc(1, sizeof(pdfmake_struct_elem_t)); |
| 55 |
4 |
▶ | if (!tree->root) { free(tree); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tree->root) { free(tree); return NULL; } |
0 |
4 |
4 |
|
| 56 |
4 |
| tree->root->type = PDFMAKE_TAG_DOCUMENT; |
| 57 |
|
| |
| 58 |
4 |
| doc->struct_tree = tree; |
| 59 |
4 |
| return tree; |
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
587 |
| pdfmake_struct_tree_t *pdfmake_doc_struct_tree(pdfmake_doc_t *doc) { |
| 63 |
587 |
▶ | return doc ? (pdfmake_struct_tree_t *)doc->struct_tree : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return doc ? (pdfmake_struct_tree_t *)doc->struct_tree : NULL; |
587 |
0 |
587 |
|
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
|
| /* The structure tree is the one part of a document allocated with plain |
| 67 |
|
| * calloc rather than from the arena - elements grow their child and MCR |
| 68 |
|
| * arrays with realloc, which an arena cannot do - so it has to be freed |
| 69 |
|
| * explicitly when the document goes. */ |
| 70 |
9 |
| static void struct_elem_free(pdfmake_struct_elem_t *elem) { |
| 71 |
|
| size_t i; |
| 72 |
9 |
▶ | if (!elem) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!elem) return; |
0 |
9 |
9 |
|
| 73 |
14 |
▶ | for (i = 0; i < elem->child_count; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < elem->child_count; i++) |
5 |
9 |
14 |
|
| 74 |
5 |
| struct_elem_free(elem->children[i]); |
| 75 |
9 |
| free(elem->children); |
| 76 |
9 |
| free(elem->content_refs); |
| 77 |
9 |
| free(elem); |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
810 |
| void pdfmake_doc_free_struct_tree(pdfmake_doc_t *doc) { |
| 81 |
|
| pdfmake_struct_tree_t *tree; |
| 82 |
810 |
▶ | if (!doc || !doc->struct_tree) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!doc || !doc->struct_tree) return; |
810 |
0 |
810 |
| if (!doc || !doc->struct_tree) return; |
806 |
4 |
810 |
|
| 83 |
4 |
| tree = (pdfmake_struct_tree_t *)doc->struct_tree; |
| 84 |
4 |
| struct_elem_free(tree->root); |
| 85 |
4 |
| free(tree->role_map); |
| 86 |
4 |
| free(tree); |
| 87 |
4 |
| doc->struct_tree = NULL; |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
|
| /* ── Structure elements ────────────────────────────────── */ |
| 91 |
|
| |
| 92 |
5 |
| pdfmake_struct_elem_t *pdfmake_struct_elem_create( |
| 93 |
|
| pdfmake_doc_t *doc, |
| 94 |
|
| pdfmake_struct_type_t type, |
| 95 |
|
| pdfmake_struct_elem_t *parent) |
| 96 |
|
| { |
| 97 |
|
| pdfmake_struct_elem_t *elem; |
| 98 |
|
| size_t new_cap; |
| 99 |
|
| pdfmake_struct_elem_t **new_arr; |
| 100 |
|
| |
| 101 |
|
| (void)doc; |
| 102 |
5 |
| elem = calloc(1, sizeof(*elem)); |
| 103 |
5 |
▶ | if (!elem) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!elem) return NULL; |
0 |
5 |
5 |
|
| 104 |
|
| |
| 105 |
5 |
| elem->type = type; |
| 106 |
5 |
| elem->parent = parent; |
| 107 |
|
| |
| 108 |
|
| /* Add to parent's children */ |
| 109 |
5 |
▶ | if (parent) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (parent) { |
5 |
0 |
5 |
|
| 110 |
5 |
▶ | if (parent->child_count >= parent->child_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (parent->child_count >= parent->child_cap) { |
2 |
3 |
5 |
|
| 111 |
2 |
▶ | new_cap = parent->child_cap == 0 ? 8 : parent->child_cap * 2; |
Branch: 1 branch
| Branch | True | False | Total |
| new_cap = parent->child_cap == 0 ? 8 : parent->child_cap * 2; |
0 |
2 |
2 |
|
| 112 |
2 |
| new_arr = realloc(parent->children, |
| 113 |
|
| new_cap * sizeof(pdfmake_struct_elem_t *)); |
| 114 |
2 |
▶ | if (!new_arr) { free(elem); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!new_arr) { free(elem); return NULL; } |
0 |
2 |
2 |
|
| 115 |
2 |
| parent->children = new_arr; |
| 116 |
2 |
| parent->child_cap = new_cap; |
| 117 |
|
| } |
| 118 |
5 |
| parent->children[parent->child_count++] = elem; |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
5 |
| return elem; |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
0 |
| pdfmake_struct_elem_t *pdfmake_struct_elem_create_custom( |
| 125 |
|
| pdfmake_doc_t *doc, |
| 126 |
|
| const char *custom_type, |
| 127 |
|
| pdfmake_struct_elem_t *parent) |
| 128 |
|
| { |
| 129 |
0 |
| pdfmake_struct_elem_t *elem = pdfmake_struct_elem_create(doc, PDFMAKE_TAG_DIV, parent); |
| 130 |
0 |
▶ | if (elem && custom_type) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (elem && custom_type) { |
0 |
0 |
0 |
| if (elem && custom_type) { |
0 |
0 |
0 |
|
| 131 |
0 |
| strncpy(elem->custom_type, custom_type, sizeof(elem->custom_type) - 1); |
| 132 |
|
| } |
| 133 |
0 |
| return elem; |
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
1 |
| pdfmake_err_t pdfmake_struct_elem_set_alt_text(pdfmake_struct_elem_t *elem, const char *alt) { |
| 137 |
1 |
▶ | if (!elem || !alt) return PDFMAKE_EINVAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!elem || !alt) return PDFMAKE_EINVAL; |
1 |
0 |
1 |
| if (!elem || !alt) return PDFMAKE_EINVAL; |
0 |
1 |
1 |
|
| 138 |
1 |
| strncpy(elem->alt_text, alt, sizeof(elem->alt_text) - 1); |
| 139 |
1 |
| return PDFMAKE_OK; |
| 140 |
|
| } |
| 141 |
|
| |
| 142 |
0 |
| pdfmake_err_t pdfmake_struct_elem_set_actual_text(pdfmake_struct_elem_t *elem, const char *text) { |
| 143 |
0 |
▶ | if (!elem || !text) return PDFMAKE_EINVAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!elem || !text) return PDFMAKE_EINVAL; |
0 |
0 |
0 |
| if (!elem || !text) return PDFMAKE_EINVAL; |
0 |
0 |
0 |
|
| 144 |
0 |
| strncpy(elem->actual_text, text, sizeof(elem->actual_text) - 1); |
| 145 |
0 |
| return PDFMAKE_OK; |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
1 |
| pdfmake_err_t pdfmake_struct_elem_set_lang(pdfmake_struct_elem_t *elem, const char *lang) { |
| 149 |
1 |
▶ | if (!elem || !lang) return PDFMAKE_EINVAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!elem || !lang) return PDFMAKE_EINVAL; |
1 |
0 |
1 |
| if (!elem || !lang) return PDFMAKE_EINVAL; |
0 |
1 |
1 |
|
| 150 |
1 |
| strncpy(elem->lang, lang, sizeof(elem->lang) - 1); |
| 151 |
1 |
| return PDFMAKE_OK; |
| 152 |
|
| } |
| 153 |
|
| |
| 154 |
0 |
| size_t pdfmake_struct_elem_child_count(pdfmake_struct_elem_t *elem) { |
| 155 |
0 |
▶ | return elem ? elem->child_count : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return elem ? elem->child_count : 0; |
0 |
0 |
0 |
|
| 156 |
|
| } |
| 157 |
|
| |
| 158 |
0 |
| pdfmake_struct_elem_t *pdfmake_struct_elem_child_at(pdfmake_struct_elem_t *elem, size_t idx) { |
| 159 |
0 |
▶ | if (!elem || idx >= elem->child_count) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!elem || idx >= elem->child_count) return NULL; |
0 |
0 |
0 |
| if (!elem || idx >= elem->child_count) return NULL; |
0 |
0 |
0 |
|
| 160 |
0 |
| return elem->children[idx]; |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
0 |
| pdfmake_err_t pdfmake_struct_elem_add_mcr( |
| 164 |
|
| pdfmake_struct_elem_t *elem, |
| 165 |
|
| uint32_t page_obj_num, |
| 166 |
|
| int mcid) |
| 167 |
|
| { |
| 168 |
|
| size_t new_cap; |
| 169 |
|
| pdfmake_mcr_t *new_arr; |
| 170 |
|
| |
| 171 |
0 |
▶ | if (!elem) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!elem) return PDFMAKE_EINVAL; |
0 |
0 |
0 |
|
| 172 |
0 |
▶ | if (elem->mcr_count >= elem->mcr_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (elem->mcr_count >= elem->mcr_cap) { |
0 |
0 |
0 |
|
| 173 |
0 |
▶ | new_cap = elem->mcr_cap == 0 ? 4 : elem->mcr_cap * 2; |
Branch: 1 branch
| Branch | True | False | Total |
| new_cap = elem->mcr_cap == 0 ? 4 : elem->mcr_cap * 2; |
0 |
0 |
0 |
|
| 174 |
0 |
| new_arr = realloc(elem->content_refs, new_cap * sizeof(pdfmake_mcr_t)); |
| 175 |
0 |
▶ | if (!new_arr) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!new_arr) return PDFMAKE_ENOMEM; |
0 |
0 |
0 |
|
| 176 |
0 |
| elem->content_refs = new_arr; |
| 177 |
0 |
| elem->mcr_cap = new_cap; |
| 178 |
|
| } |
| 179 |
0 |
| elem->content_refs[elem->mcr_count].page_obj_num = page_obj_num; |
| 180 |
0 |
| elem->content_refs[elem->mcr_count].mcid = mcid; |
| 181 |
0 |
| elem->mcr_count++; |
| 182 |
0 |
| return PDFMAKE_OK; |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
|
| /* ── Content stream ────────────────────────────────────── */ |
| 186 |
|
| |
| 187 |
0 |
| pdfmake_err_t pdfmake_content_begin_tag( |
| 188 |
|
| pdfmake_content_t *c, |
| 189 |
|
| pdfmake_struct_type_t type, |
| 190 |
|
| int mcid) |
| 191 |
|
| { |
| 192 |
|
| pdfmake_buf_t *buf; |
| 193 |
|
| const char *tag; |
| 194 |
|
| char props[64]; |
| 195 |
|
| |
| 196 |
0 |
▶ | if (!c) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c) return PDFMAKE_EINVAL; |
0 |
0 |
0 |
|
| 197 |
0 |
| buf = &c->buf; |
| 198 |
0 |
| tag = pdfmake_struct_type_name(type); |
| 199 |
|
| |
| 200 |
|
| /* Emit: /Tag <</MCID n>> BDC */ |
| 201 |
0 |
| snprintf(props, sizeof(props), "<</MCID %d>>", mcid); |
| 202 |
|
| |
| 203 |
0 |
| pdfmake_buf_append_byte(buf, '/'); |
| 204 |
0 |
| pdfmake_buf_append_cstr(buf, tag); |
| 205 |
0 |
| pdfmake_buf_append_byte(buf, ' '); |
| 206 |
0 |
| pdfmake_buf_append_cstr(buf, props); |
| 207 |
0 |
| pdfmake_buf_append_cstr(buf, " BDC\n"); |
| 208 |
|
| |
| 209 |
0 |
| return PDFMAKE_OK; |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
0 |
| pdfmake_err_t pdfmake_content_end_tag(pdfmake_content_t *c) { |
| 213 |
0 |
▶ | if (!c) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c) return PDFMAKE_EINVAL; |
0 |
0 |
0 |
|
| 214 |
0 |
| return pdfmake_mc_EMC(c); |
| 215 |
|
| } |
| 216 |
|
| |
| 217 |
|
| /* ── Role mapping ──────────────────────────────────────── */ |
| 218 |
|
| |
| 219 |
0 |
| pdfmake_err_t pdfmake_struct_tree_map_role( |
| 220 |
|
| pdfmake_struct_tree_t *tree, |
| 221 |
|
| const char *custom, |
| 222 |
|
| pdfmake_struct_type_t standard) |
| 223 |
|
| { |
| 224 |
|
| size_t new_cap; |
| 225 |
|
| void *new_arr; |
| 226 |
|
| |
| 227 |
0 |
▶ | if (!tree || !custom) return PDFMAKE_EINVAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!tree || !custom) return PDFMAKE_EINVAL; |
0 |
0 |
0 |
| if (!tree || !custom) return PDFMAKE_EINVAL; |
0 |
0 |
0 |
|
| 228 |
0 |
▶ | if (tree->role_map_count >= tree->role_map_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tree->role_map_count >= tree->role_map_cap) { |
0 |
0 |
0 |
|
| 229 |
0 |
▶ | new_cap = tree->role_map_cap == 0 ? 4 : tree->role_map_cap * 2; |
Branch: 1 branch
| Branch | True | False | Total |
| new_cap = tree->role_map_cap == 0 ? 4 : tree->role_map_cap * 2; |
0 |
0 |
0 |
|
| 230 |
0 |
| new_arr = realloc(tree->role_map, new_cap * sizeof(tree->role_map[0])); |
| 231 |
0 |
▶ | if (!new_arr) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!new_arr) return PDFMAKE_ENOMEM; |
0 |
0 |
0 |
|
| 232 |
0 |
| tree->role_map = new_arr; |
| 233 |
0 |
| tree->role_map_cap = new_cap; |
| 234 |
|
| } |
| 235 |
0 |
| strncpy(tree->role_map[tree->role_map_count].custom, custom, 63); |
| 236 |
0 |
| tree->role_map[tree->role_map_count].standard = standard; |
| 237 |
0 |
| tree->role_map_count++; |
| 238 |
0 |
| return PDFMAKE_OK; |
| 239 |
|
| } |
| 240 |
|
| |
| 241 |
|
| /* ── Writing ───────────────────────────────────────────── */ |
| 242 |
|
| |
| 243 |
|
| /* Write a single structure element recursively */ |
| 244 |
7 |
| static uint32_t _write_struct_elem(pdfmake_struct_elem_t *elem, |
| 245 |
|
| pdfmake_doc_t *doc, |
| 246 |
|
| uint32_t parent_num) |
| 247 |
|
| { |
| 248 |
|
| pdfmake_arena_t *arena; |
| 249 |
|
| uint32_t k; |
| 250 |
|
| pdfmake_obj_t dict; |
| 251 |
|
| const char *type_name; |
| 252 |
|
| pdfmake_obj_t kids; |
| 253 |
|
| size_t i_mcr; |
| 254 |
|
| size_t i_child; |
| 255 |
|
| pdfmake_obj_t mcr; |
| 256 |
|
| uint32_t mk; |
| 257 |
|
| uint32_t child_num; |
| 258 |
|
| pdfmake_obj_t *elem_obj; |
| 259 |
|
| |
| 260 |
7 |
▶ | if (!elem || !doc) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!elem || !doc) return 0; |
7 |
0 |
7 |
| if (!elem || !doc) return 0; |
0 |
7 |
7 |
|
| 261 |
7 |
| arena = pdfmake_doc_arena(doc); |
| 262 |
|
| |
| 263 |
7 |
| dict = pdfmake_dict_new(arena); |
| 264 |
7 |
| k = pdfmake_arena_intern_name(arena, "Type", 4); |
| 265 |
7 |
| pdfmake_dict_set(arena, &dict, k, pdfmake_name_cstr(arena, "StructElem")); |
| 266 |
|
| |
| 267 |
|
| /* /S — structure type */ |
| 268 |
7 |
| k = pdfmake_arena_intern_name(arena, "S", 1); |
| 269 |
14 |
| type_name = elem->custom_type[0] ? elem->custom_type |
| 270 |
7 |
▶ | : pdfmake_struct_type_name(elem->type); |
Branch: 1 branch
| Branch | True | False | Total |
| : pdfmake_struct_type_name(elem->type); |
0 |
7 |
7 |
|
| 271 |
7 |
| pdfmake_dict_set(arena, &dict, k, pdfmake_name_cstr(arena, type_name)); |
| 272 |
|
| |
| 273 |
|
| /* /P — parent reference */ |
| 274 |
7 |
▶ | if (parent_num > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (parent_num > 0) { |
5 |
2 |
7 |
|
| 275 |
5 |
| k = pdfmake_arena_intern_name(arena, "P", 1); |
| 276 |
5 |
| pdfmake_dict_set(arena, &dict, k, pdfmake_ref(parent_num, 0)); |
| 277 |
|
| } |
| 278 |
|
| |
| 279 |
|
| /* /Alt, /ActualText, /Lang */ |
| 280 |
7 |
▶ | if (elem->alt_text[0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (elem->alt_text[0]) { |
1 |
6 |
7 |
|
| 281 |
1 |
| k = pdfmake_arena_intern_name(arena, "Alt", 3); |
| 282 |
1 |
| pdfmake_dict_set(arena, &dict, k, pdfmake_str_cstr(arena, elem->alt_text)); |
| 283 |
|
| } |
| 284 |
7 |
▶ | if (elem->actual_text[0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (elem->actual_text[0]) { |
0 |
7 |
7 |
|
| 285 |
0 |
| k = pdfmake_arena_intern_name(arena, "ActualText", 10); |
| 286 |
0 |
| pdfmake_dict_set(arena, &dict, k, pdfmake_str_cstr(arena, elem->actual_text)); |
| 287 |
|
| } |
| 288 |
7 |
▶ | if (elem->lang[0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (elem->lang[0]) { |
1 |
6 |
7 |
|
| 289 |
1 |
| k = pdfmake_arena_intern_name(arena, "Lang", 4); |
| 290 |
1 |
| pdfmake_dict_set(arena, &dict, k, pdfmake_str_cstr(arena, elem->lang)); |
| 291 |
|
| } |
| 292 |
|
| |
| 293 |
|
| /* Add as indirect object to get our number */ |
| 294 |
7 |
| elem->obj_num = pdfmake_doc_add(doc, dict); |
| 295 |
7 |
▶ | if (elem->obj_num == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (elem->obj_num == 0) return 0; |
0 |
7 |
7 |
|
| 296 |
|
| |
| 297 |
|
| /* /K — children array (MCRs + child elements) */ |
| 298 |
7 |
▶ | if (elem->mcr_count > 0 || elem->child_count > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (elem->mcr_count > 0 || elem->child_count > 0) { |
7 |
0 |
7 |
| if (elem->mcr_count > 0 || elem->child_count > 0) { |
2 |
5 |
7 |
|
| 299 |
2 |
| kids = pdfmake_array_new(arena); |
| 300 |
|
| |
| 301 |
|
| /* Marked content references */ |
| 302 |
2 |
▶ | for (i_mcr = 0; i_mcr < elem->mcr_count; i_mcr++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i_mcr = 0; i_mcr < elem->mcr_count; i_mcr++) { |
0 |
2 |
2 |
|
| 303 |
0 |
| mcr = pdfmake_dict_new(arena); |
| 304 |
0 |
| mk = pdfmake_arena_intern_name(arena, "Type", 4); |
| 305 |
0 |
| pdfmake_dict_set(arena, &mcr, mk, pdfmake_name_cstr(arena, "MCR")); |
| 306 |
0 |
| mk = pdfmake_arena_intern_name(arena, "Pg", 2); |
| 307 |
0 |
| pdfmake_dict_set(arena, &mcr, mk, |
| 308 |
0 |
| pdfmake_ref(elem->content_refs[i_mcr].page_obj_num, 0)); |
| 309 |
0 |
| mk = pdfmake_arena_intern_name(arena, "MCID", 4); |
| 310 |
0 |
| pdfmake_dict_set(arena, &mcr, mk, |
| 311 |
0 |
| pdfmake_int(elem->content_refs[i_mcr].mcid)); |
| 312 |
0 |
| pdfmake_array_push(arena, &kids, mcr); |
| 313 |
|
| } |
| 314 |
|
| |
| 315 |
|
| /* Child elements */ |
| 316 |
7 |
▶ | for (i_child = 0; i_child < elem->child_count; i_child++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i_child = 0; i_child < elem->child_count; i_child++) { |
5 |
2 |
7 |
|
| 317 |
5 |
| child_num = _write_struct_elem(elem->children[i_child], doc, elem->obj_num); |
| 318 |
5 |
▶ | if (child_num > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (child_num > 0) { |
5 |
0 |
5 |
|
| 319 |
5 |
| pdfmake_array_push(arena, &kids, pdfmake_ref(child_num, 0)); |
| 320 |
|
| } |
| 321 |
|
| } |
| 322 |
|
| |
| 323 |
|
| /* Update the element dict with /K */ |
| 324 |
2 |
| elem_obj = pdfmake_doc_get(doc, elem->obj_num); |
| 325 |
2 |
▶ | if (elem_obj && elem_obj->kind == PDFMAKE_DICT) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (elem_obj && elem_obj->kind == PDFMAKE_DICT) { |
2 |
0 |
2 |
| if (elem_obj && elem_obj->kind == PDFMAKE_DICT) { |
2 |
0 |
2 |
|
| 326 |
2 |
| k = pdfmake_arena_intern_name(arena, "K", 1); |
| 327 |
2 |
| pdfmake_dict_set(arena, elem_obj, k, kids); |
| 328 |
|
| } |
| 329 |
|
| } |
| 330 |
|
| |
| 331 |
7 |
| return elem->obj_num; |
| 332 |
|
| } |
| 333 |
|
| |
| 334 |
587 |
| pdfmake_err_t pdfmake_doc_write_struct_tree(pdfmake_doc_t *doc) { |
| 335 |
|
| pdfmake_struct_tree_t *tree; |
| 336 |
|
| pdfmake_arena_t *arena; |
| 337 |
|
| uint32_t k; |
| 338 |
|
| uint32_t root_elem_num; |
| 339 |
|
| pdfmake_obj_t tree_dict; |
| 340 |
|
| pdfmake_obj_t rm; |
| 341 |
|
| size_t i; |
| 342 |
|
| uint32_t rk; |
| 343 |
|
| pdfmake_obj_t *root_obj; |
| 344 |
|
| pdfmake_obj_t *catalog; |
| 345 |
|
| pdfmake_obj_t mark_info; |
| 346 |
|
| uint32_t marked_key; |
| 347 |
|
| |
| 348 |
587 |
▶ | if (!doc) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!doc) return PDFMAKE_EINVAL; |
0 |
587 |
587 |
|
| 349 |
|
| |
| 350 |
|
| /* Query, never create. Creating one here meant every untagged document - |
| 351 |
|
| * which is most of them - allocated a tree and a root element only to |
| 352 |
|
| * discover on the next line that there was nothing to write, and then |
| 353 |
|
| * leaked both. */ |
| 354 |
587 |
| tree = pdfmake_doc_struct_tree(doc); |
| 355 |
587 |
▶ | if (!tree || !tree->root) return PDFMAKE_OK; /* No tree */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!tree || !tree->root) return PDFMAKE_OK; /* No tree */ |
2 |
585 |
587 |
| if (!tree || !tree->root) return PDFMAKE_OK; /* No tree */ |
0 |
2 |
2 |
|
| 356 |
2 |
▶ | if (tree->root->child_count == 0 && tree->root->mcr_count == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (tree->root->child_count == 0 && tree->root->mcr_count == 0) |
0 |
2 |
2 |
| if (tree->root->child_count == 0 && tree->root->mcr_count == 0) |
0 |
0 |
0 |
|
| 357 |
0 |
| return PDFMAKE_OK; /* Empty tree */ |
| 358 |
|
| |
| 359 |
2 |
| arena = pdfmake_doc_arena(doc); |
| 360 |
|
| |
| 361 |
|
| /* Write all elements recursively */ |
| 362 |
2 |
| root_elem_num = _write_struct_elem(tree->root, doc, 0); |
| 363 |
2 |
▶ | if (root_elem_num == 0) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (root_elem_num == 0) return PDFMAKE_ENOMEM; |
0 |
2 |
2 |
|
| 364 |
|
| |
| 365 |
|
| /* Build /StructTreeRoot */ |
| 366 |
2 |
| tree_dict = pdfmake_dict_new(arena); |
| 367 |
2 |
| k = pdfmake_arena_intern_name(arena, "Type", 4); |
| 368 |
2 |
| pdfmake_dict_set(arena, &tree_dict, k, pdfmake_name_cstr(arena, "StructTreeRoot")); |
| 369 |
|
| |
| 370 |
2 |
| k = pdfmake_arena_intern_name(arena, "K", 1); |
| 371 |
2 |
| pdfmake_dict_set(arena, &tree_dict, k, pdfmake_ref(root_elem_num, 0)); |
| 372 |
|
| |
| 373 |
|
| /* Role mapping */ |
| 374 |
2 |
▶ | if (tree->role_map_count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tree->role_map_count > 0) { |
0 |
2 |
2 |
|
| 375 |
0 |
| rm = pdfmake_dict_new(arena); |
| 376 |
0 |
▶ | for (i = 0; i < tree->role_map_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < tree->role_map_count; i++) { |
0 |
0 |
0 |
|
| 377 |
0 |
| rk = pdfmake_arena_intern_name(arena, |
| 378 |
0 |
| tree->role_map[i].custom, strlen(tree->role_map[i].custom)); |
| 379 |
0 |
| pdfmake_dict_set(arena, &rm, rk, |
| 380 |
0 |
| pdfmake_name_cstr(arena, pdfmake_struct_type_name(tree->role_map[i].standard))); |
| 381 |
|
| } |
| 382 |
0 |
| k = pdfmake_arena_intern_name(arena, "RoleMap", 7); |
| 383 |
0 |
| pdfmake_dict_set(arena, &tree_dict, k, rm); |
| 384 |
|
| } |
| 385 |
|
| |
| 386 |
2 |
| tree->obj_num = pdfmake_doc_add(doc, tree_dict); |
| 387 |
2 |
▶ | if (tree->obj_num == 0) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tree->obj_num == 0) return PDFMAKE_ENOMEM; |
0 |
2 |
2 |
|
| 388 |
|
| |
| 389 |
|
| /* Update root element's /P to point to tree root */ |
| 390 |
2 |
| root_obj = pdfmake_doc_get(doc, root_elem_num); |
| 391 |
2 |
▶ | if (root_obj && root_obj->kind == PDFMAKE_DICT) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (root_obj && root_obj->kind == PDFMAKE_DICT) { |
2 |
0 |
2 |
| if (root_obj && root_obj->kind == PDFMAKE_DICT) { |
2 |
0 |
2 |
|
| 392 |
2 |
| k = pdfmake_arena_intern_name(arena, "P", 1); |
| 393 |
2 |
| pdfmake_dict_set(arena, root_obj, k, pdfmake_ref(tree->obj_num, 0)); |
| 394 |
|
| } |
| 395 |
|
| |
| 396 |
|
| /* Add /StructTreeRoot to catalog */ |
| 397 |
2 |
| catalog = pdfmake_doc_get(doc, doc->root_num); |
| 398 |
2 |
▶ | if (catalog && catalog->kind == PDFMAKE_DICT) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (catalog && catalog->kind == PDFMAKE_DICT) { |
2 |
0 |
2 |
| if (catalog && catalog->kind == PDFMAKE_DICT) { |
2 |
0 |
2 |
|
| 399 |
2 |
| k = pdfmake_arena_intern_name(arena, "StructTreeRoot", 14); |
| 400 |
2 |
| pdfmake_dict_set(arena, catalog, k, pdfmake_ref(tree->obj_num, 0)); |
| 401 |
|
| |
| 402 |
|
| /* Mark as tagged: /MarkInfo << /Marked true >> */ |
| 403 |
2 |
| mark_info = pdfmake_dict_new(arena); |
| 404 |
2 |
| marked_key = pdfmake_arena_intern_name(arena, "Marked", 6); |
| 405 |
2 |
| pdfmake_dict_set(arena, &mark_info, marked_key, pdfmake_bool(1)); |
| 406 |
2 |
| k = pdfmake_arena_intern_name(arena, "MarkInfo", 8); |
| 407 |
2 |
| pdfmake_dict_set(arena, catalog, k, mark_info); |
| 408 |
|
| } |
| 409 |
|
| |
| 410 |
2 |
| return PDFMAKE_OK; |
| 411 |
|
| } |