Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/pdf/pdf-page.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 // Copyright (C) 2004-2025 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 | |
| 23 #include "mupdf/fitz.h" | |
| 24 #include "pdf-annot-imp.h" | |
| 25 | |
| 26 #include <stdlib.h> | |
| 27 #include <string.h> | |
| 28 #include <limits.h> | |
| 29 | |
| 30 static void pdf_adjust_page_labels(fz_context *ctx, pdf_document *doc, int index, int adjust); | |
| 31 | |
| 32 int | |
| 33 pdf_count_pages(fz_context *ctx, pdf_document *doc) | |
| 34 { | |
| 35 int pages; | |
| 36 if (doc->is_fdf) | |
| 37 return 0; | |
| 38 /* FIXME: We should reset linear_page_count to 0 when editing starts | |
| 39 * (or when linear loading ends) */ | |
| 40 if (doc->linear_page_count != 0) | |
| 41 pages = doc->linear_page_count; | |
| 42 else | |
| 43 pages = pdf_to_int(ctx, pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/Pages/Count")); | |
| 44 if (pages < 0) | |
| 45 fz_throw(ctx, FZ_ERROR_FORMAT, "Invalid number of pages"); | |
| 46 return pages; | |
| 47 } | |
| 48 | |
| 49 int pdf_count_pages_imp(fz_context *ctx, fz_document *doc, int chapter) | |
| 50 { | |
| 51 return pdf_count_pages(ctx, (pdf_document*)doc); | |
| 52 } | |
| 53 | |
| 54 static int | |
| 55 pdf_load_page_tree_imp(fz_context *ctx, pdf_document *doc, pdf_obj *node, int idx, pdf_cycle_list *cycle_up) | |
| 56 { | |
| 57 pdf_cycle_list cycle; | |
| 58 pdf_obj *type = pdf_dict_get(ctx, node, PDF_NAME(Type)); | |
| 59 if (pdf_name_eq(ctx, type, PDF_NAME(Pages))) | |
| 60 { | |
| 61 pdf_obj *kids = pdf_dict_get(ctx, node, PDF_NAME(Kids)); | |
| 62 int i, n = pdf_array_len(ctx, kids); | |
| 63 if (pdf_cycle(ctx, &cycle, cycle_up, node)) | |
| 64 fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in page tree"); | |
| 65 for (i = 0; i < n; ++i) | |
| 66 idx = pdf_load_page_tree_imp(ctx, doc, pdf_array_get(ctx, kids, i), idx, &cycle); | |
| 67 } | |
| 68 else if (pdf_name_eq(ctx, type, PDF_NAME(Page))) | |
| 69 { | |
| 70 if (idx >= doc->map_page_count) | |
| 71 fz_throw(ctx, FZ_ERROR_FORMAT, "too many kids in page tree"); | |
| 72 doc->rev_page_map[idx].page = idx; | |
| 73 doc->rev_page_map[idx].object = pdf_to_num(ctx, node); | |
| 74 doc->fwd_page_map[idx] = pdf_keep_obj(ctx, node); | |
| 75 ++idx; | |
| 76 } | |
| 77 else | |
| 78 { | |
| 79 fz_throw(ctx, FZ_ERROR_FORMAT, "non-page object in page tree"); | |
| 80 } | |
| 81 return idx; | |
| 82 } | |
| 83 | |
| 84 static int | |
| 85 cmp_rev_page_map(const void *va, const void *vb) | |
| 86 { | |
| 87 const pdf_rev_page_map *a = va; | |
| 88 const pdf_rev_page_map *b = vb; | |
| 89 return a->object - b->object; | |
| 90 } | |
| 91 | |
| 92 void | |
| 93 pdf_load_page_tree(fz_context *ctx, pdf_document *doc) | |
| 94 { | |
| 95 /* Noop now. */ | |
| 96 } | |
| 97 | |
| 98 void | |
| 99 pdf_drop_page_tree_internal(fz_context *ctx, pdf_document *doc) | |
| 100 { | |
| 101 int i; | |
| 102 fz_free(ctx, doc->rev_page_map); | |
| 103 doc->rev_page_map = NULL; | |
| 104 if (doc->fwd_page_map) | |
| 105 for (i = 0; i < doc->map_page_count; i++) | |
| 106 pdf_drop_obj(ctx, doc->fwd_page_map[i]); | |
| 107 fz_free(ctx, doc->fwd_page_map); | |
| 108 doc->fwd_page_map = NULL; | |
| 109 doc->map_page_count = 0; | |
| 110 } | |
| 111 | |
| 112 static void | |
| 113 pdf_load_page_tree_internal(fz_context *ctx, pdf_document *doc) | |
| 114 { | |
| 115 /* Check we're not already loaded. */ | |
| 116 if (doc->fwd_page_map != NULL) | |
| 117 return; | |
| 118 | |
| 119 /* At this point we're trusting that only 1 thread should be doing | |
| 120 * stuff that hits the document at a time. */ | |
| 121 fz_try(ctx) | |
| 122 { | |
| 123 int idx; | |
| 124 | |
| 125 doc->map_page_count = pdf_count_pages(ctx, doc); | |
| 126 while (1) | |
| 127 { | |
| 128 doc->rev_page_map = Memento_label(fz_calloc(ctx, doc->map_page_count, sizeof(pdf_rev_page_map)), "pdf_rev_page_map"); | |
| 129 doc->fwd_page_map = Memento_label(fz_calloc(ctx, doc->map_page_count, sizeof(pdf_obj *)), "pdf_fwd_page_map"); | |
| 130 idx = pdf_load_page_tree_imp(ctx, doc, pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/Pages"), 0, NULL); | |
| 131 if (idx < doc->map_page_count) | |
| 132 { | |
| 133 /* The document claims more pages that it has. Fix that. */ | |
| 134 fz_warn(ctx, "Document claims to have %d pages, but only has %d.", doc->map_page_count, idx); | |
| 135 /* This put drops the page tree! */ | |
| 136 pdf_dict_putp_drop(ctx, pdf_trailer(ctx, doc), "Root/Pages/Count", pdf_new_int(ctx, idx)); | |
| 137 doc->map_page_count = idx; | |
| 138 continue; | |
| 139 } | |
| 140 break; | |
| 141 } | |
| 142 qsort(doc->rev_page_map, doc->map_page_count, sizeof *doc->rev_page_map, cmp_rev_page_map); | |
| 143 } | |
| 144 fz_catch(ctx) | |
| 145 { | |
| 146 pdf_drop_page_tree_internal(ctx, doc); | |
| 147 fz_rethrow(ctx); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void | |
| 152 pdf_drop_page_tree(fz_context *ctx, pdf_document *doc) | |
| 153 { | |
| 154 /* Historical entry point. Now does nothing. We drop 'just in time'. */ | |
| 155 } | |
| 156 | |
| 157 static pdf_obj * | |
| 158 pdf_lookup_page_loc_imp(fz_context *ctx, pdf_document *doc, pdf_obj *node, int *skip, pdf_obj **parentp, int *indexp) | |
| 159 { | |
| 160 pdf_mark_list mark_list; | |
| 161 pdf_obj *kids; | |
| 162 pdf_obj *hit = NULL; | |
| 163 int i, len; | |
| 164 | |
| 165 pdf_mark_list_init(ctx, &mark_list); | |
| 166 | |
| 167 fz_try(ctx) | |
| 168 { | |
| 169 do | |
| 170 { | |
| 171 kids = pdf_dict_get(ctx, node, PDF_NAME(Kids)); | |
| 172 len = pdf_array_len(ctx, kids); | |
| 173 | |
| 174 if (len == 0) | |
| 175 fz_throw(ctx, FZ_ERROR_FORMAT, "malformed page tree"); | |
| 176 | |
| 177 if (pdf_mark_list_push(ctx, &mark_list, node)) | |
| 178 fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in page tree"); | |
| 179 | |
| 180 for (i = 0; i < len; i++) | |
| 181 { | |
| 182 pdf_obj *kid = pdf_array_get(ctx, kids, i); | |
| 183 pdf_obj *type = pdf_dict_get(ctx, kid, PDF_NAME(Type)); | |
| 184 if (type ? pdf_name_eq(ctx, type, PDF_NAME(Pages)) : pdf_dict_get(ctx, kid, PDF_NAME(Kids)) && !pdf_dict_get(ctx, kid, PDF_NAME(MediaBox))) | |
| 185 { | |
| 186 int count = pdf_dict_get_int(ctx, kid, PDF_NAME(Count)); | |
| 187 if (*skip < count) | |
| 188 { | |
| 189 node = kid; | |
| 190 break; | |
| 191 } | |
| 192 else | |
| 193 { | |
| 194 *skip -= count; | |
| 195 } | |
| 196 } | |
| 197 else | |
| 198 { | |
| 199 if (type ? !pdf_name_eq(ctx, type, PDF_NAME(Page)) : !pdf_dict_get(ctx, kid, PDF_NAME(MediaBox))) | |
| 200 fz_warn(ctx, "non-page object in page tree (%s)", pdf_to_name(ctx, type)); | |
| 201 if (*skip == 0) | |
| 202 { | |
| 203 if (parentp) *parentp = node; | |
| 204 if (indexp) *indexp = i; | |
| 205 hit = kid; | |
| 206 break; | |
| 207 } | |
| 208 else | |
| 209 { | |
| 210 (*skip)--; | |
| 211 } | |
| 212 } | |
| 213 } | |
| 214 } | |
| 215 /* If i < len && hit != NULL the desired page was found in the | |
| 216 Kids array, done. If i < len && hit == NULL the found page tree | |
| 217 node contains a Kids array that contains the desired page, loop | |
| 218 back to top to extract it. When i == len the Kids array has been | |
| 219 exhausted without finding the desired page, give up. | |
| 220 */ | |
| 221 while (hit == NULL && i < len); | |
| 222 } | |
| 223 fz_always(ctx) | |
| 224 { | |
| 225 pdf_mark_list_free(ctx, &mark_list); | |
| 226 } | |
| 227 fz_catch(ctx) | |
| 228 { | |
| 229 fz_rethrow(ctx); | |
| 230 } | |
| 231 | |
| 232 return hit; | |
| 233 } | |
| 234 | |
| 235 pdf_obj * | |
| 236 pdf_lookup_page_loc(fz_context *ctx, pdf_document *doc, int needle, pdf_obj **parentp, int *indexp) | |
| 237 { | |
| 238 pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)); | |
| 239 pdf_obj *node = pdf_dict_get(ctx, root, PDF_NAME(Pages)); | |
| 240 int skip = needle; | |
| 241 pdf_obj *hit; | |
| 242 | |
| 243 if (!node) | |
| 244 fz_throw(ctx, FZ_ERROR_FORMAT, "cannot find page tree"); | |
| 245 | |
| 246 hit = pdf_lookup_page_loc_imp(ctx, doc, node, &skip, parentp, indexp); | |
| 247 if (!hit) | |
| 248 fz_throw(ctx, FZ_ERROR_FORMAT, "cannot find page %d in page tree", needle+1); | |
| 249 return hit; | |
| 250 } | |
| 251 | |
| 252 pdf_obj * | |
| 253 pdf_lookup_page_obj(fz_context *ctx, pdf_document *doc, int needle) | |
| 254 { | |
| 255 if (doc->fwd_page_map == NULL && !doc->page_tree_broken) | |
| 256 { | |
| 257 fz_try(ctx) | |
| 258 pdf_load_page_tree_internal(ctx, doc); | |
| 259 fz_catch(ctx) | |
| 260 { | |
| 261 doc->page_tree_broken = 1; | |
| 262 fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); | |
| 263 fz_report_error(ctx); | |
| 264 fz_warn(ctx, "Page tree load failed. Falling back to slow lookup"); | |
| 265 } | |
| 266 } | |
| 267 | |
| 268 if (doc->fwd_page_map) | |
| 269 { | |
| 270 if (needle < 0 || needle >= doc->map_page_count) | |
| 271 fz_throw(ctx, FZ_ERROR_FORMAT, "cannot find page %d in page tree", needle+1); | |
| 272 if (doc->fwd_page_map[needle] != NULL) | |
| 273 return doc->fwd_page_map[needle]; | |
| 274 } | |
| 275 | |
| 276 return pdf_lookup_page_loc(ctx, doc, needle, NULL, NULL); | |
| 277 } | |
| 278 | |
| 279 static int | |
| 280 pdf_count_pages_before_kid(fz_context *ctx, pdf_document *doc, pdf_obj *parent, int kid_num) | |
| 281 { | |
| 282 pdf_obj *kids = pdf_dict_get(ctx, parent, PDF_NAME(Kids)); | |
| 283 int i, total = 0, len = pdf_array_len(ctx, kids); | |
| 284 for (i = 0; i < len; i++) | |
| 285 { | |
| 286 pdf_obj *kid = pdf_array_get(ctx, kids, i); | |
| 287 if (pdf_to_num(ctx, kid) == kid_num) | |
| 288 return total; | |
| 289 if (pdf_name_eq(ctx, pdf_dict_get(ctx, kid, PDF_NAME(Type)), PDF_NAME(Pages))) | |
| 290 { | |
| 291 pdf_obj *count = pdf_dict_get(ctx, kid, PDF_NAME(Count)); | |
| 292 int n = pdf_to_int(ctx, count); | |
| 293 if (!pdf_is_int(ctx, count) || n < 0 || INT_MAX - total <= n) | |
| 294 fz_throw(ctx, FZ_ERROR_FORMAT, "illegal or missing count in pages tree"); | |
| 295 total += n; | |
| 296 } | |
| 297 else | |
| 298 total++; | |
| 299 } | |
| 300 return -1; // the page we're looking for is not in the page tree (it has been deleted) | |
| 301 } | |
| 302 | |
| 303 static int | |
| 304 pdf_lookup_page_number_slow(fz_context *ctx, pdf_document *doc, pdf_obj *node) | |
| 305 { | |
| 306 pdf_mark_list mark_list; | |
| 307 int needle = pdf_to_num(ctx, node); | |
| 308 int total = 0; | |
| 309 int n; | |
| 310 pdf_obj *parent; | |
| 311 | |
| 312 if (!pdf_name_eq(ctx, pdf_dict_get(ctx, node, PDF_NAME(Type)), PDF_NAME(Page))) | |
| 313 { | |
| 314 fz_warn(ctx, "invalid page object"); | |
| 315 return -1; | |
| 316 } | |
| 317 | |
| 318 pdf_mark_list_init(ctx, &mark_list); | |
| 319 parent = pdf_dict_get(ctx, node, PDF_NAME(Parent)); | |
| 320 fz_try(ctx) | |
| 321 { | |
| 322 while (pdf_is_dict(ctx, parent)) | |
| 323 { | |
| 324 if (pdf_mark_list_push(ctx, &mark_list, parent)) | |
| 325 fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in page tree (parents)"); | |
| 326 n = pdf_count_pages_before_kid(ctx, doc, parent, needle); | |
| 327 | |
| 328 // Page was not found in page tree! | |
| 329 if (n < 0) | |
| 330 { | |
| 331 total = -1; | |
| 332 break; | |
| 333 } | |
| 334 if (INT_MAX - total <= n) | |
| 335 fz_throw(ctx, FZ_ERROR_FORMAT, "illegal or missing count in pages tree"); | |
| 336 | |
| 337 total += n; | |
| 338 needle = pdf_to_num(ctx, parent); | |
| 339 parent = pdf_dict_get(ctx, parent, PDF_NAME(Parent)); | |
| 340 } | |
| 341 } | |
| 342 fz_always(ctx) | |
| 343 pdf_mark_list_free(ctx, &mark_list); | |
| 344 fz_catch(ctx) | |
| 345 fz_rethrow(ctx); | |
| 346 | |
| 347 return total; | |
| 348 } | |
| 349 | |
| 350 static int | |
| 351 pdf_lookup_page_number_fast(fz_context *ctx, pdf_document *doc, int needle) | |
| 352 { | |
| 353 int l = 0; | |
| 354 int r = doc->map_page_count - 1; | |
| 355 while (l <= r) | |
| 356 { | |
| 357 int m = (l + r) >> 1; | |
| 358 int c = needle - doc->rev_page_map[m].object; | |
| 359 if (c < 0) | |
| 360 r = m - 1; | |
| 361 else if (c > 0) | |
| 362 l = m + 1; | |
| 363 else | |
| 364 return doc->rev_page_map[m].page; | |
| 365 } | |
| 366 return -1; | |
| 367 } | |
| 368 | |
| 369 int | |
| 370 pdf_lookup_page_number(fz_context *ctx, pdf_document *doc, pdf_obj *page) | |
| 371 { | |
| 372 if (doc->rev_page_map == NULL && !doc->page_tree_broken) | |
| 373 { | |
| 374 fz_try(ctx) | |
| 375 pdf_load_page_tree_internal(ctx, doc); | |
| 376 fz_catch(ctx) | |
| 377 { | |
| 378 doc->page_tree_broken = 1; | |
| 379 fz_report_error(ctx); | |
| 380 fz_warn(ctx, "Page tree load failed. Falling back to slow lookup."); | |
| 381 } | |
| 382 } | |
| 383 | |
| 384 if (doc->rev_page_map) | |
| 385 return pdf_lookup_page_number_fast(ctx, doc, pdf_to_num(ctx, page)); | |
| 386 else | |
| 387 return pdf_lookup_page_number_slow(ctx, doc, page); | |
| 388 } | |
| 389 | |
| 390 static void | |
| 391 pdf_flatten_inheritable_page_item(fz_context *ctx, pdf_obj *page, pdf_obj *key) | |
| 392 { | |
| 393 pdf_obj *val = pdf_dict_get_inheritable(ctx, page, key); | |
| 394 if (val) | |
| 395 pdf_dict_put(ctx, page, key, val); | |
| 396 } | |
| 397 | |
| 398 void | |
| 399 pdf_flatten_inheritable_page_items(fz_context *ctx, pdf_obj *page) | |
| 400 { | |
| 401 pdf_flatten_inheritable_page_item(ctx, page, PDF_NAME(MediaBox)); | |
| 402 pdf_flatten_inheritable_page_item(ctx, page, PDF_NAME(CropBox)); | |
| 403 pdf_flatten_inheritable_page_item(ctx, page, PDF_NAME(Rotate)); | |
| 404 pdf_flatten_inheritable_page_item(ctx, page, PDF_NAME(Resources)); | |
| 405 } | |
| 406 | |
| 407 /* We need to know whether to install a page-level transparency group */ | |
| 408 | |
| 409 /* | |
| 410 * Object memo flags - allows us to secretly remember "a memo" (a bool) in an | |
| 411 * object, and to read back whether there was a memo, and if so, what it was. | |
| 412 */ | |
| 413 enum | |
| 414 { | |
| 415 PDF_FLAGS_MEMO_BM = 0, | |
| 416 PDF_FLAGS_MEMO_OP = 1 | |
| 417 }; | |
| 418 | |
| 419 static int pdf_resources_use_blending(fz_context *ctx, pdf_obj *rdb, pdf_cycle_list *cycle_up); | |
| 420 | |
| 421 static int | |
| 422 pdf_extgstate_uses_blending(fz_context *ctx, pdf_obj *dict) | |
| 423 { | |
| 424 pdf_obj *obj = pdf_dict_get(ctx, dict, PDF_NAME(BM)); | |
| 425 if (obj && !pdf_name_eq(ctx, obj, PDF_NAME(Normal))) | |
| 426 return 1; | |
| 427 return 0; | |
| 428 } | |
| 429 | |
| 430 static int | |
| 431 pdf_pattern_uses_blending(fz_context *ctx, pdf_obj *dict, pdf_cycle_list *cycle_up) | |
| 432 { | |
| 433 pdf_obj *obj; | |
| 434 pdf_cycle_list cycle; | |
| 435 if (pdf_cycle(ctx, &cycle, cycle_up, dict)) | |
| 436 return 0; | |
| 437 obj = pdf_dict_get(ctx, dict, PDF_NAME(Resources)); | |
| 438 if (pdf_resources_use_blending(ctx, obj, &cycle)) | |
| 439 return 1; | |
| 440 obj = pdf_dict_get(ctx, dict, PDF_NAME(ExtGState)); | |
| 441 return pdf_extgstate_uses_blending(ctx, obj); | |
| 442 } | |
| 443 | |
| 444 static int | |
| 445 pdf_xobject_uses_blending(fz_context *ctx, pdf_obj *dict, pdf_cycle_list *cycle_up) | |
| 446 { | |
| 447 pdf_obj *obj = pdf_dict_get(ctx, dict, PDF_NAME(Resources)); | |
| 448 pdf_cycle_list cycle; | |
| 449 if (pdf_cycle(ctx, &cycle, cycle_up, dict)) | |
| 450 return 0; | |
| 451 if (pdf_name_eq(ctx, pdf_dict_getp(ctx, dict, "Group/S"), PDF_NAME(Transparency))) | |
| 452 return 1; | |
| 453 if (pdf_name_eq(ctx, pdf_dict_get(ctx, dict, PDF_NAME(Subtype)), PDF_NAME(Image)) && | |
| 454 pdf_dict_get(ctx, dict, PDF_NAME(SMask)) != NULL) | |
| 455 return 1; | |
| 456 return pdf_resources_use_blending(ctx, obj, &cycle); | |
| 457 } | |
| 458 | |
| 459 static int | |
| 460 pdf_resources_use_blending(fz_context *ctx, pdf_obj *rdb, pdf_cycle_list *cycle_up) | |
| 461 { | |
| 462 pdf_cycle_list cycle; | |
| 463 pdf_obj *obj; | |
| 464 int i, n, useBM = 0; | |
| 465 | |
| 466 if (!rdb) | |
| 467 return 0; | |
| 468 | |
| 469 /* Have we been here before and remembered an answer? */ | |
| 470 if (pdf_obj_memo(ctx, rdb, PDF_FLAGS_MEMO_BM, &useBM)) | |
| 471 return useBM; | |
| 472 | |
| 473 /* stop on cyclic resource dependencies */ | |
| 474 if (pdf_cycle(ctx, &cycle, cycle_up, rdb)) | |
| 475 return 0; | |
| 476 | |
| 477 obj = pdf_dict_get(ctx, rdb, PDF_NAME(ExtGState)); | |
| 478 n = pdf_dict_len(ctx, obj); | |
| 479 for (i = 0; i < n; i++) | |
| 480 if (pdf_extgstate_uses_blending(ctx, pdf_dict_get_val(ctx, obj, i))) | |
| 481 goto found; | |
| 482 | |
| 483 obj = pdf_dict_get(ctx, rdb, PDF_NAME(Pattern)); | |
| 484 n = pdf_dict_len(ctx, obj); | |
| 485 for (i = 0; i < n; i++) | |
| 486 if (pdf_pattern_uses_blending(ctx, pdf_dict_get_val(ctx, obj, i), &cycle)) | |
| 487 goto found; | |
| 488 | |
| 489 obj = pdf_dict_get(ctx, rdb, PDF_NAME(XObject)); | |
| 490 n = pdf_dict_len(ctx, obj); | |
| 491 for (i = 0; i < n; i++) | |
| 492 if (pdf_xobject_uses_blending(ctx, pdf_dict_get_val(ctx, obj, i), &cycle)) | |
| 493 goto found; | |
| 494 if (0) | |
| 495 { | |
| 496 found: | |
| 497 useBM = 1; | |
| 498 } | |
| 499 | |
| 500 pdf_set_obj_memo(ctx, rdb, PDF_FLAGS_MEMO_BM, useBM); | |
| 501 return useBM; | |
| 502 } | |
| 503 | |
| 504 static int pdf_resources_use_overprint(fz_context *ctx, pdf_obj *rdb, pdf_cycle_list *cycle_up); | |
| 505 | |
| 506 static int | |
| 507 pdf_extgstate_uses_overprint(fz_context *ctx, pdf_obj *dict) | |
| 508 { | |
| 509 pdf_obj *obj = pdf_dict_get(ctx, dict, PDF_NAME(OP)); | |
| 510 if (obj && pdf_to_bool(ctx, obj)) | |
| 511 return 1; | |
| 512 return 0; | |
| 513 } | |
| 514 | |
| 515 static int | |
| 516 pdf_pattern_uses_overprint(fz_context *ctx, pdf_obj *dict, pdf_cycle_list *cycle_up) | |
| 517 { | |
| 518 pdf_obj *obj; | |
| 519 pdf_cycle_list cycle; | |
| 520 if (pdf_cycle(ctx, &cycle, cycle_up, dict)) | |
| 521 return 0; | |
| 522 obj = pdf_dict_get(ctx, dict, PDF_NAME(Resources)); | |
| 523 if (pdf_resources_use_overprint(ctx, obj, &cycle)) | |
| 524 return 1; | |
| 525 obj = pdf_dict_get(ctx, dict, PDF_NAME(ExtGState)); | |
| 526 return pdf_extgstate_uses_overprint(ctx, obj); | |
| 527 } | |
| 528 | |
| 529 static int | |
| 530 pdf_xobject_uses_overprint(fz_context *ctx, pdf_obj *dict, pdf_cycle_list *cycle_up) | |
| 531 { | |
| 532 pdf_obj *obj = pdf_dict_get(ctx, dict, PDF_NAME(Resources)); | |
| 533 pdf_cycle_list cycle; | |
| 534 if (pdf_cycle(ctx, &cycle, cycle_up, dict)) | |
| 535 return 0; | |
| 536 return pdf_resources_use_overprint(ctx, obj, &cycle); | |
| 537 } | |
| 538 | |
| 539 static int | |
| 540 pdf_resources_use_overprint(fz_context *ctx, pdf_obj *rdb, pdf_cycle_list *cycle_up) | |
| 541 { | |
| 542 pdf_cycle_list cycle; | |
| 543 pdf_obj *obj; | |
| 544 int i, n, useOP = 0; | |
| 545 | |
| 546 if (!rdb) | |
| 547 return 0; | |
| 548 | |
| 549 /* Have we been here before and remembered an answer? */ | |
| 550 if (pdf_obj_memo(ctx, rdb, PDF_FLAGS_MEMO_OP, &useOP)) | |
| 551 return useOP; | |
| 552 | |
| 553 /* stop on cyclic resource dependencies */ | |
| 554 if (pdf_cycle(ctx, &cycle, cycle_up, rdb)) | |
| 555 return 0; | |
| 556 | |
| 557 obj = pdf_dict_get(ctx, rdb, PDF_NAME(ExtGState)); | |
| 558 n = pdf_dict_len(ctx, obj); | |
| 559 for (i = 0; i < n; i++) | |
| 560 if (pdf_extgstate_uses_overprint(ctx, pdf_dict_get_val(ctx, obj, i))) | |
| 561 goto found; | |
| 562 | |
| 563 obj = pdf_dict_get(ctx, rdb, PDF_NAME(Pattern)); | |
| 564 n = pdf_dict_len(ctx, obj); | |
| 565 for (i = 0; i < n; i++) | |
| 566 if (pdf_pattern_uses_overprint(ctx, pdf_dict_get_val(ctx, obj, i), &cycle)) | |
| 567 goto found; | |
| 568 | |
| 569 obj = pdf_dict_get(ctx, rdb, PDF_NAME(XObject)); | |
| 570 n = pdf_dict_len(ctx, obj); | |
| 571 for (i = 0; i < n; i++) | |
| 572 if (pdf_xobject_uses_overprint(ctx, pdf_dict_get_val(ctx, obj, i), &cycle)) | |
| 573 goto found; | |
| 574 if (0) | |
| 575 { | |
| 576 found: | |
| 577 useOP = 1; | |
| 578 } | |
| 579 | |
| 580 pdf_set_obj_memo(ctx, rdb, PDF_FLAGS_MEMO_OP, useOP); | |
| 581 return useOP; | |
| 582 } | |
| 583 | |
| 584 fz_transition * | |
| 585 pdf_page_presentation(fz_context *ctx, pdf_page *page, fz_transition *transition, float *duration) | |
| 586 { | |
| 587 pdf_obj *obj, *transdict; | |
| 588 | |
| 589 *duration = pdf_dict_get_real(ctx, page->obj, PDF_NAME(Dur)); | |
| 590 | |
| 591 transdict = pdf_dict_get(ctx, page->obj, PDF_NAME(Trans)); | |
| 592 if (!transdict) | |
| 593 return NULL; | |
| 594 | |
| 595 obj = pdf_dict_get(ctx, transdict, PDF_NAME(D)); | |
| 596 | |
| 597 transition->duration = pdf_to_real_default(ctx, obj, 1); | |
| 598 | |
| 599 transition->vertical = !pdf_name_eq(ctx, pdf_dict_get(ctx, transdict, PDF_NAME(Dm)), PDF_NAME(H)); | |
| 600 transition->outwards = !pdf_name_eq(ctx, pdf_dict_get(ctx, transdict, PDF_NAME(M)), PDF_NAME(I)); | |
| 601 /* FIXME: If 'Di' is None, it should be handled differently, but | |
| 602 * this only affects Fly, and we don't implement that currently. */ | |
| 603 transition->direction = (pdf_dict_get_int(ctx, transdict, PDF_NAME(Di))); | |
| 604 /* FIXME: Read SS for Fly when we implement it */ | |
| 605 /* FIXME: Read B for Fly when we implement it */ | |
| 606 | |
| 607 obj = pdf_dict_get(ctx, transdict, PDF_NAME(S)); | |
| 608 if (pdf_name_eq(ctx, obj, PDF_NAME(Split))) | |
| 609 transition->type = FZ_TRANSITION_SPLIT; | |
| 610 else if (pdf_name_eq(ctx, obj, PDF_NAME(Blinds))) | |
| 611 transition->type = FZ_TRANSITION_BLINDS; | |
| 612 else if (pdf_name_eq(ctx, obj, PDF_NAME(Box))) | |
| 613 transition->type = FZ_TRANSITION_BOX; | |
| 614 else if (pdf_name_eq(ctx, obj, PDF_NAME(Wipe))) | |
| 615 transition->type = FZ_TRANSITION_WIPE; | |
| 616 else if (pdf_name_eq(ctx, obj, PDF_NAME(Dissolve))) | |
| 617 transition->type = FZ_TRANSITION_DISSOLVE; | |
| 618 else if (pdf_name_eq(ctx, obj, PDF_NAME(Glitter))) | |
| 619 transition->type = FZ_TRANSITION_GLITTER; | |
| 620 else if (pdf_name_eq(ctx, obj, PDF_NAME(Fly))) | |
| 621 transition->type = FZ_TRANSITION_FLY; | |
| 622 else if (pdf_name_eq(ctx, obj, PDF_NAME(Push))) | |
| 623 transition->type = FZ_TRANSITION_PUSH; | |
| 624 else if (pdf_name_eq(ctx, obj, PDF_NAME(Cover))) | |
| 625 transition->type = FZ_TRANSITION_COVER; | |
| 626 else if (pdf_name_eq(ctx, obj, PDF_NAME(Uncover))) | |
| 627 transition->type = FZ_TRANSITION_UNCOVER; | |
| 628 else if (pdf_name_eq(ctx, obj, PDF_NAME(Fade))) | |
| 629 transition->type = FZ_TRANSITION_FADE; | |
| 630 else | |
| 631 transition->type = FZ_TRANSITION_NONE; | |
| 632 | |
| 633 return transition; | |
| 634 } | |
| 635 | |
| 636 fz_rect | |
| 637 pdf_bound_page(fz_context *ctx, pdf_page *page, fz_box_type box) | |
| 638 { | |
| 639 fz_matrix page_ctm; | |
| 640 fz_rect rect; | |
| 641 pdf_page_transform_box(ctx, page, &rect, &page_ctm, box); | |
| 642 return fz_transform_rect(rect, page_ctm); | |
| 643 } | |
| 644 | |
| 645 static fz_rect | |
| 646 pdf_bound_page_imp(fz_context *ctx, fz_page *page, fz_box_type box) | |
| 647 { | |
| 648 return pdf_bound_page(ctx, (pdf_page*)page, box); | |
| 649 } | |
| 650 | |
| 651 void | |
| 652 pdf_set_page_box(fz_context *ctx, pdf_page *page, fz_box_type box, fz_rect rect) | |
| 653 { | |
| 654 fz_matrix page_ctm, inv_page_ctm; | |
| 655 fz_rect page_rect; | |
| 656 pdf_page_transform_box(ctx, page, NULL, &page_ctm, box); | |
| 657 inv_page_ctm = fz_invert_matrix(page_ctm); | |
| 658 page_rect = fz_transform_rect(rect, inv_page_ctm); | |
| 659 | |
| 660 switch (box) | |
| 661 { | |
| 662 case FZ_MEDIA_BOX: | |
| 663 pdf_dict_put_rect(ctx, page->obj, PDF_NAME(MediaBox), page_rect); | |
| 664 break; | |
| 665 case FZ_CROP_BOX: | |
| 666 pdf_dict_put_rect(ctx, page->obj, PDF_NAME(CropBox), page_rect); | |
| 667 break; | |
| 668 case FZ_BLEED_BOX: | |
| 669 pdf_dict_put_rect(ctx, page->obj, PDF_NAME(BleedBox), page_rect); | |
| 670 break; | |
| 671 case FZ_TRIM_BOX: | |
| 672 pdf_dict_put_rect(ctx, page->obj, PDF_NAME(TrimBox), page_rect); | |
| 673 break; | |
| 674 case FZ_ART_BOX: | |
| 675 pdf_dict_put_rect(ctx, page->obj, PDF_NAME(ArtBox), page_rect); | |
| 676 break; | |
| 677 case FZ_UNKNOWN_BOX: | |
| 678 fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "unknown page box type: %d", box); | |
| 679 } | |
| 680 } | |
| 681 | |
| 682 fz_link * | |
| 683 pdf_load_links(fz_context *ctx, pdf_page *page) | |
| 684 { | |
| 685 return fz_keep_link(ctx, page->links); | |
| 686 } | |
| 687 | |
| 688 static fz_link * | |
| 689 pdf_load_links_imp(fz_context *ctx, fz_page *page) | |
| 690 { | |
| 691 return pdf_load_links(ctx, (pdf_page*)page); | |
| 692 } | |
| 693 | |
| 694 pdf_obj * | |
| 695 pdf_page_resources(fz_context *ctx, pdf_page *page) | |
| 696 { | |
| 697 return pdf_dict_get_inheritable(ctx, page->obj, PDF_NAME(Resources)); | |
| 698 } | |
| 699 | |
| 700 pdf_obj * | |
| 701 pdf_page_contents(fz_context *ctx, pdf_page *page) | |
| 702 { | |
| 703 return pdf_dict_get(ctx, page->obj, PDF_NAME(Contents)); | |
| 704 } | |
| 705 | |
| 706 pdf_obj * | |
| 707 pdf_page_group(fz_context *ctx, pdf_page *page) | |
| 708 { | |
| 709 return pdf_dict_get(ctx, page->obj, PDF_NAME(Group)); | |
| 710 } | |
| 711 | |
| 712 void | |
| 713 pdf_page_obj_transform_box(fz_context *ctx, pdf_obj *pageobj, fz_rect *outbox, fz_matrix *page_ctm, fz_box_type box) | |
| 714 { | |
| 715 pdf_obj *obj; | |
| 716 fz_rect usedbox, tempbox, cropbox, mediabox; | |
| 717 float userunit = 1; | |
| 718 int rotate; | |
| 719 | |
| 720 if (!outbox) | |
| 721 outbox = &tempbox; | |
| 722 | |
| 723 userunit = pdf_dict_get_real_default(ctx, pageobj, PDF_NAME(UserUnit), 1); | |
| 724 | |
| 725 obj = pdf_dict_get_inheritable(ctx, pageobj, PDF_NAME(MediaBox)); | |
| 726 mediabox = pdf_to_rect(ctx, obj); | |
| 727 | |
| 728 obj = NULL; | |
| 729 if (box == FZ_ART_BOX) | |
| 730 obj = pdf_dict_get_inheritable(ctx, pageobj, PDF_NAME(ArtBox)); | |
| 731 if (box == FZ_TRIM_BOX) | |
| 732 obj = pdf_dict_get_inheritable(ctx, pageobj, PDF_NAME(TrimBox)); | |
| 733 if (box == FZ_BLEED_BOX) | |
| 734 obj = pdf_dict_get_inheritable(ctx, pageobj, PDF_NAME(BleedBox)); | |
| 735 if (box == FZ_CROP_BOX || !obj) | |
| 736 obj = pdf_dict_get_inheritable(ctx, pageobj, PDF_NAME(CropBox)); | |
| 737 if (box == FZ_MEDIA_BOX || !obj) | |
| 738 usedbox = mediabox; | |
| 739 else | |
| 740 { | |
| 741 // never use a box larger than fits the paper (mediabox) | |
| 742 usedbox = fz_intersect_rect(mediabox, pdf_to_rect(ctx, obj)); | |
| 743 } | |
| 744 | |
| 745 if (fz_is_empty_rect(usedbox)) | |
| 746 usedbox = fz_make_rect(0, 0, 612, 792); | |
| 747 usedbox.x0 = fz_min(usedbox.x0, usedbox.x1); | |
| 748 usedbox.y0 = fz_min(usedbox.y0, usedbox.y1); | |
| 749 usedbox.x1 = fz_max(usedbox.x0, usedbox.x1); | |
| 750 usedbox.y1 = fz_max(usedbox.y0, usedbox.y1); | |
| 751 if (usedbox.x1 - usedbox.x0 < 1 || usedbox.y1 - usedbox.y0 < 1) | |
| 752 usedbox = fz_unit_rect; | |
| 753 | |
| 754 *outbox = usedbox; | |
| 755 | |
| 756 /* Snap page rotation to 0, 90, 180 or 270 */ | |
| 757 rotate = pdf_dict_get_inheritable_int(ctx, pageobj, PDF_NAME(Rotate)); | |
| 758 if (rotate < 0) | |
| 759 rotate = 360 - ((-rotate) % 360); | |
| 760 if (rotate >= 360) | |
| 761 rotate = rotate % 360; | |
| 762 rotate = 90*((rotate + 45)/90); | |
| 763 if (rotate >= 360) | |
| 764 rotate = 0; | |
| 765 | |
| 766 /* Compute transform from fitz' page space (upper left page origin, y descending, 72 dpi) | |
| 767 * to PDF user space (arbitrary page origin, y ascending, UserUnit dpi). */ | |
| 768 | |
| 769 /* Make left-handed and scale by UserUnit */ | |
| 770 *page_ctm = fz_scale(userunit, -userunit); | |
| 771 | |
| 772 /* Rotate */ | |
| 773 *page_ctm = fz_pre_rotate(*page_ctm, -rotate); | |
| 774 | |
| 775 /* Always use CropBox to set origin to top left */ | |
| 776 obj = pdf_dict_get_inheritable(ctx, pageobj, PDF_NAME(CropBox)); | |
| 777 if (!pdf_is_array(ctx, obj)) | |
| 778 obj = pdf_dict_get_inheritable(ctx, pageobj, PDF_NAME(MediaBox)); | |
| 779 cropbox = pdf_to_rect(ctx, obj); | |
| 780 cropbox = fz_intersect_rect(cropbox, mediabox); | |
| 781 if (fz_is_empty_rect(cropbox)) | |
| 782 cropbox = fz_make_rect(0, 0, 612, 792); | |
| 783 cropbox.x0 = fz_min(cropbox.x0, cropbox.x1); | |
| 784 cropbox.y0 = fz_min(cropbox.y0, cropbox.y1); | |
| 785 cropbox.x1 = fz_max(cropbox.x0, cropbox.x1); | |
| 786 cropbox.y1 = fz_max(cropbox.y0, cropbox.y1); | |
| 787 if (cropbox.x1 - cropbox.x0 < 1 || cropbox.y1 - cropbox.y0 < 1) | |
| 788 cropbox = fz_unit_rect; | |
| 789 | |
| 790 /* Translate page origin of CropBox to 0,0 */ | |
| 791 cropbox = fz_transform_rect(cropbox, *page_ctm); | |
| 792 *page_ctm = fz_concat(*page_ctm, fz_translate(-cropbox.x0, -cropbox.y0)); | |
| 793 } | |
| 794 | |
| 795 void | |
| 796 pdf_page_obj_transform(fz_context *ctx, pdf_obj *pageobj, fz_rect *page_cropbox, fz_matrix *page_ctm) | |
| 797 { | |
| 798 pdf_page_obj_transform_box(ctx, pageobj, page_cropbox, page_ctm, FZ_CROP_BOX); | |
| 799 } | |
| 800 | |
| 801 void | |
| 802 pdf_page_transform_box(fz_context *ctx, pdf_page *page, fz_rect *page_cropbox, fz_matrix *page_ctm, fz_box_type box) | |
| 803 { | |
| 804 pdf_page_obj_transform_box(ctx, page->obj, page_cropbox, page_ctm, box); | |
| 805 } | |
| 806 | |
| 807 void | |
| 808 pdf_page_transform(fz_context *ctx, pdf_page *page, fz_rect *cropbox, fz_matrix *ctm) | |
| 809 { | |
| 810 pdf_page_transform_box(ctx, page, cropbox, ctm, FZ_CROP_BOX); | |
| 811 } | |
| 812 | |
| 813 static void | |
| 814 find_seps(fz_context *ctx, fz_separations **seps, pdf_obj *obj, pdf_mark_list *clearme) | |
| 815 { | |
| 816 int i, n; | |
| 817 pdf_obj *nameobj, *cols; | |
| 818 | |
| 819 if (!obj) | |
| 820 return; | |
| 821 | |
| 822 // Already seen this ColorSpace... | |
| 823 if (pdf_mark_list_push(ctx, clearme, obj)) | |
| 824 return; | |
| 825 | |
| 826 nameobj = pdf_array_get(ctx, obj, 0); | |
| 827 if (pdf_name_eq(ctx, nameobj, PDF_NAME(Separation))) | |
| 828 { | |
| 829 fz_colorspace *cs; | |
| 830 const char *name = pdf_array_get_name(ctx, obj, 1); | |
| 831 | |
| 832 /* Skip 'special' colorants. */ | |
| 833 if (!strcmp(name, "Black") || | |
| 834 !strcmp(name, "Cyan") || | |
| 835 !strcmp(name, "Magenta") || | |
| 836 !strcmp(name, "Yellow") || | |
| 837 !strcmp(name, "All") || | |
| 838 !strcmp(name, "None")) | |
| 839 return; | |
| 840 | |
| 841 n = fz_count_separations(ctx, *seps); | |
| 842 for (i = 0; i < n; i++) | |
| 843 { | |
| 844 if (!strcmp(name, fz_separation_name(ctx, *seps, i))) | |
| 845 return; /* Got that one already */ | |
| 846 } | |
| 847 | |
| 848 fz_try(ctx) | |
| 849 cs = pdf_load_colorspace(ctx, obj); | |
| 850 fz_catch(ctx) | |
| 851 { | |
| 852 fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); | |
| 853 fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); | |
| 854 fz_report_error(ctx); | |
| 855 return; /* ignore broken colorspace */ | |
| 856 } | |
| 857 fz_try(ctx) | |
| 858 { | |
| 859 if (!*seps) | |
| 860 *seps = fz_new_separations(ctx, 0); | |
| 861 fz_add_separation(ctx, *seps, name, cs, 0); | |
| 862 } | |
| 863 fz_always(ctx) | |
| 864 fz_drop_colorspace(ctx, cs); | |
| 865 fz_catch(ctx) | |
| 866 fz_rethrow(ctx); | |
| 867 } | |
| 868 else if (pdf_name_eq(ctx, nameobj, PDF_NAME(Indexed))) | |
| 869 { | |
| 870 find_seps(ctx, seps, pdf_array_get(ctx, obj, 1), clearme); | |
| 871 } | |
| 872 else if (pdf_name_eq(ctx, nameobj, PDF_NAME(DeviceN))) | |
| 873 { | |
| 874 /* If the separation colorants exists for this DeviceN color space | |
| 875 * add those prior to our search for DeviceN color */ | |
| 876 cols = pdf_dict_get(ctx, pdf_array_get(ctx, obj, 4), PDF_NAME(Colorants)); | |
| 877 n = pdf_dict_len(ctx, cols); | |
| 878 for (i = 0; i < n; i++) | |
| 879 find_seps(ctx, seps, pdf_dict_get_val(ctx, cols, i), clearme); | |
| 880 } | |
| 881 } | |
| 882 | |
| 883 static void | |
| 884 find_devn(fz_context *ctx, fz_separations **seps, pdf_obj *obj, pdf_mark_list *clearme) | |
| 885 { | |
| 886 int i, j, n, m; | |
| 887 pdf_obj *arr; | |
| 888 pdf_obj *nameobj = pdf_array_get(ctx, obj, 0); | |
| 889 | |
| 890 if (!obj) | |
| 891 return; | |
| 892 | |
| 893 // Already seen this ColorSpace... | |
| 894 if (pdf_mark_list_push(ctx, clearme, obj)) | |
| 895 return; | |
| 896 | |
| 897 if (!pdf_name_eq(ctx, nameobj, PDF_NAME(DeviceN))) | |
| 898 return; | |
| 899 | |
| 900 arr = pdf_array_get(ctx, obj, 1); | |
| 901 m = pdf_array_len(ctx, arr); | |
| 902 for (j = 0; j < m; j++) | |
| 903 { | |
| 904 fz_colorspace *cs; | |
| 905 const char *name = pdf_array_get_name(ctx, arr, j); | |
| 906 | |
| 907 /* Skip 'special' colorants. */ | |
| 908 if (!strcmp(name, "Black") || | |
| 909 !strcmp(name, "Cyan") || | |
| 910 !strcmp(name, "Magenta") || | |
| 911 !strcmp(name, "Yellow") || | |
| 912 !strcmp(name, "All") || | |
| 913 !strcmp(name, "None")) | |
| 914 continue; | |
| 915 | |
| 916 n = fz_count_separations(ctx, *seps); | |
| 917 for (i = 0; i < n; i++) | |
| 918 { | |
| 919 if (!strcmp(name, fz_separation_name(ctx, *seps, i))) | |
| 920 break; /* Got that one already */ | |
| 921 } | |
| 922 | |
| 923 if (i == n) | |
| 924 { | |
| 925 fz_try(ctx) | |
| 926 cs = pdf_load_colorspace(ctx, obj); | |
| 927 fz_catch(ctx) | |
| 928 { | |
| 929 fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); | |
| 930 fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); | |
| 931 fz_report_error(ctx); | |
| 932 continue; /* ignore broken colorspace */ | |
| 933 } | |
| 934 fz_try(ctx) | |
| 935 { | |
| 936 if (!*seps) | |
| 937 *seps = fz_new_separations(ctx, 0); | |
| 938 fz_add_separation(ctx, *seps, name, cs, j); | |
| 939 } | |
| 940 fz_always(ctx) | |
| 941 fz_drop_colorspace(ctx, cs); | |
| 942 fz_catch(ctx) | |
| 943 fz_rethrow(ctx); | |
| 944 } | |
| 945 } | |
| 946 } | |
| 947 | |
| 948 typedef void (res_finder_fn)(fz_context *ctx, fz_separations **seps, pdf_obj *obj, pdf_mark_list *clearme); | |
| 949 | |
| 950 static void | |
| 951 scan_page_seps(fz_context *ctx, pdf_obj *res, fz_separations **seps, res_finder_fn *fn, pdf_mark_list *clearme) | |
| 952 { | |
| 953 pdf_obj *dict; | |
| 954 pdf_obj *obj; | |
| 955 int i, n; | |
| 956 | |
| 957 if (!res) | |
| 958 return; | |
| 959 | |
| 960 // Already seen this Resources... | |
| 961 if (pdf_mark_list_push(ctx, clearme, res)) | |
| 962 return; | |
| 963 | |
| 964 dict = pdf_dict_get(ctx, res, PDF_NAME(ColorSpace)); | |
| 965 n = pdf_dict_len(ctx, dict); | |
| 966 for (i = 0; i < n; i++) | |
| 967 { | |
| 968 obj = pdf_dict_get_val(ctx, dict, i); | |
| 969 fn(ctx, seps, obj, clearme); | |
| 970 } | |
| 971 | |
| 972 dict = pdf_dict_get(ctx, res, PDF_NAME(Shading)); | |
| 973 n = pdf_dict_len(ctx, dict); | |
| 974 for (i = 0; i < n; i++) | |
| 975 { | |
| 976 obj = pdf_dict_get_val(ctx, dict, i); | |
| 977 fn(ctx, seps, pdf_dict_get(ctx, obj, PDF_NAME(ColorSpace)), clearme); | |
| 978 } | |
| 979 | |
| 980 dict = pdf_dict_get(ctx, res, PDF_NAME(Pattern)); | |
| 981 n = pdf_dict_len(ctx, dict); | |
| 982 for (i = 0; i < n; i++) | |
| 983 { | |
| 984 pdf_obj *obj2; | |
| 985 obj = pdf_dict_get_val(ctx, dict, i); | |
| 986 obj2 = pdf_dict_get(ctx, obj, PDF_NAME(Shading)); | |
| 987 fn(ctx, seps, pdf_dict_get(ctx, obj2, PDF_NAME(ColorSpace)), clearme); | |
| 988 } | |
| 989 | |
| 990 dict = pdf_dict_get(ctx, res, PDF_NAME(XObject)); | |
| 991 n = pdf_dict_len(ctx, dict); | |
| 992 for (i = 0; i < n; i++) | |
| 993 { | |
| 994 obj = pdf_dict_get_val(ctx, dict, i); | |
| 995 // Already seen this XObject... | |
| 996 if (!pdf_mark_list_push(ctx, clearme, obj)) | |
| 997 { | |
| 998 fn(ctx, seps, pdf_dict_get(ctx, obj, PDF_NAME(ColorSpace)), clearme); | |
| 999 /* Recurse on XObject forms. */ | |
| 1000 scan_page_seps(ctx, pdf_dict_get(ctx, obj, PDF_NAME(Resources)), seps, fn, clearme); | |
| 1001 } | |
| 1002 } | |
| 1003 } | |
| 1004 | |
| 1005 fz_separations * | |
| 1006 pdf_page_separations(fz_context *ctx, pdf_page *page) | |
| 1007 { | |
| 1008 pdf_obj *res = pdf_page_resources(ctx, page); | |
| 1009 pdf_mark_list clearme; | |
| 1010 fz_separations *seps = NULL; | |
| 1011 | |
| 1012 pdf_mark_list_init(ctx, &clearme); | |
| 1013 fz_try(ctx) | |
| 1014 { | |
| 1015 /* Run through and look for separations first. This is | |
| 1016 * because separations are simplest to deal with, and | |
| 1017 * because DeviceN may be implemented on top of separations. | |
| 1018 */ | |
| 1019 scan_page_seps(ctx, res, &seps, find_seps, &clearme); | |
| 1020 } | |
| 1021 fz_always(ctx) | |
| 1022 pdf_mark_list_free(ctx, &clearme); | |
| 1023 fz_catch(ctx) | |
| 1024 { | |
| 1025 fz_drop_separations(ctx, seps); | |
| 1026 fz_rethrow(ctx); | |
| 1027 } | |
| 1028 | |
| 1029 pdf_mark_list_init(ctx, &clearme); | |
| 1030 fz_try(ctx) | |
| 1031 { | |
| 1032 /* Now run through again, and look for DeviceNs. These may | |
| 1033 * have spot colors in that aren't defined in terms of | |
| 1034 * separations. */ | |
| 1035 scan_page_seps(ctx, res, &seps, find_devn, &clearme); | |
| 1036 } | |
| 1037 fz_always(ctx) | |
| 1038 pdf_mark_list_free(ctx, &clearme); | |
| 1039 fz_catch(ctx) | |
| 1040 { | |
| 1041 fz_drop_separations(ctx, seps); | |
| 1042 fz_rethrow(ctx); | |
| 1043 } | |
| 1044 | |
| 1045 return seps; | |
| 1046 } | |
| 1047 | |
| 1048 int | |
| 1049 pdf_page_uses_overprint(fz_context *ctx, pdf_page *page) | |
| 1050 { | |
| 1051 return page ? page->overprint : 0; | |
| 1052 } | |
| 1053 | |
| 1054 static void | |
| 1055 pdf_drop_page_imp(fz_context *ctx, fz_page *page_) | |
| 1056 { | |
| 1057 pdf_page *page = (pdf_page*)page_; | |
| 1058 pdf_annot *widget; | |
| 1059 pdf_annot *annot; | |
| 1060 pdf_link *link; | |
| 1061 | |
| 1062 link = (pdf_link *) page->links; | |
| 1063 while (link) | |
| 1064 { | |
| 1065 link->page = NULL; | |
| 1066 link = (pdf_link *) link->super.next; | |
| 1067 } | |
| 1068 fz_drop_link(ctx, page->links); | |
| 1069 page->links = NULL; | |
| 1070 | |
| 1071 annot = page->annots; | |
| 1072 while (annot) | |
| 1073 { | |
| 1074 annot->page = NULL; | |
| 1075 annot = annot->next; | |
| 1076 } | |
| 1077 pdf_drop_annots(ctx, page->annots); | |
| 1078 page->annots = NULL; | |
| 1079 | |
| 1080 widget = page->widgets; | |
| 1081 while (widget) | |
| 1082 { | |
| 1083 widget->page = NULL; | |
| 1084 widget = widget->next; | |
| 1085 } | |
| 1086 pdf_drop_widgets(ctx, page->widgets); | |
| 1087 page->widgets = NULL; | |
| 1088 pdf_drop_obj(ctx, page->obj); | |
| 1089 page->obj = NULL; | |
| 1090 page->doc = NULL; | |
| 1091 } | |
| 1092 | |
| 1093 static void pdf_run_page_contents_imp(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix ctm, fz_cookie *cookie) | |
| 1094 { | |
| 1095 pdf_run_page_contents(ctx, (pdf_page*)page, dev, ctm, cookie); | |
| 1096 } | |
| 1097 | |
| 1098 static void pdf_run_page_annots_imp(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix ctm, fz_cookie *cookie) | |
| 1099 { | |
| 1100 pdf_run_page_annots(ctx, (pdf_page*)page, dev, ctm, cookie); | |
| 1101 } | |
| 1102 | |
| 1103 static void pdf_run_page_widgets_imp(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix ctm, fz_cookie *cookie) | |
| 1104 { | |
| 1105 pdf_run_page_widgets(ctx, (pdf_page*)page, dev, ctm, cookie); | |
| 1106 } | |
| 1107 | |
| 1108 static fz_transition * pdf_page_presentation_imp(fz_context *ctx, fz_page *page, fz_transition *transition, float *duration) | |
| 1109 { | |
| 1110 return pdf_page_presentation(ctx, (pdf_page*)page, transition, duration); | |
| 1111 } | |
| 1112 | |
| 1113 static fz_separations * pdf_page_separations_imp(fz_context *ctx, fz_page *page) | |
| 1114 { | |
| 1115 return pdf_page_separations(ctx, (pdf_page*)page); | |
| 1116 } | |
| 1117 | |
| 1118 static int pdf_page_uses_overprint_imp(fz_context *ctx, fz_page *page) | |
| 1119 { | |
| 1120 return pdf_page_uses_overprint(ctx, (pdf_page*)page); | |
| 1121 } | |
| 1122 | |
| 1123 static fz_link * pdf_create_link_imp(fz_context *ctx, fz_page *page, fz_rect bbox, const char *uri) | |
| 1124 { | |
| 1125 return pdf_create_link(ctx, (pdf_page*)page, bbox, uri); | |
| 1126 } | |
| 1127 | |
| 1128 static void pdf_delete_link_imp(fz_context *ctx, fz_page *page, fz_link *link) | |
| 1129 { | |
| 1130 pdf_delete_link(ctx, (pdf_page*)page, link); | |
| 1131 } | |
| 1132 | |
| 1133 static pdf_page * | |
| 1134 pdf_new_page(fz_context *ctx, pdf_document *doc) | |
| 1135 { | |
| 1136 pdf_page *page = fz_new_derived_page(ctx, pdf_page, (fz_document*) doc); | |
| 1137 | |
| 1138 page->doc = doc; /* typecast alias for page->super.doc */ | |
| 1139 | |
| 1140 page->super.drop_page = pdf_drop_page_imp; | |
| 1141 page->super.load_links = pdf_load_links_imp; | |
| 1142 page->super.bound_page = pdf_bound_page_imp; | |
| 1143 page->super.run_page_contents = pdf_run_page_contents_imp; | |
| 1144 page->super.run_page_annots = pdf_run_page_annots_imp; | |
| 1145 page->super.run_page_widgets = pdf_run_page_widgets_imp; | |
| 1146 page->super.page_presentation = pdf_page_presentation_imp; | |
| 1147 page->super.separations = pdf_page_separations_imp; | |
| 1148 page->super.overprint = pdf_page_uses_overprint_imp; | |
| 1149 page->super.create_link = pdf_create_link_imp; | |
| 1150 page->super.delete_link = pdf_delete_link_imp; | |
| 1151 | |
| 1152 page->obj = NULL; | |
| 1153 | |
| 1154 page->transparency = 0; | |
| 1155 page->links = NULL; | |
| 1156 page->annots = NULL; | |
| 1157 page->annot_tailp = &page->annots; | |
| 1158 page->widgets = NULL; | |
| 1159 page->widget_tailp = &page->widgets; | |
| 1160 | |
| 1161 return page; | |
| 1162 } | |
| 1163 | |
| 1164 static void | |
| 1165 pdf_load_default_colorspaces_imp(fz_context *ctx, fz_default_colorspaces *default_cs, pdf_obj *obj) | |
| 1166 { | |
| 1167 pdf_obj *cs_obj; | |
| 1168 | |
| 1169 /* The spec says to ignore any colors we can't understand */ | |
| 1170 | |
| 1171 cs_obj = pdf_dict_get(ctx, obj, PDF_NAME(DefaultGray)); | |
| 1172 if (cs_obj) | |
| 1173 { | |
| 1174 fz_try(ctx) | |
| 1175 { | |
| 1176 fz_colorspace *cs = pdf_load_colorspace(ctx, cs_obj); | |
| 1177 fz_set_default_gray(ctx, default_cs, cs); | |
| 1178 fz_drop_colorspace(ctx, cs); | |
| 1179 } | |
| 1180 fz_catch(ctx) | |
| 1181 { | |
| 1182 fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); | |
| 1183 fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); | |
| 1184 fz_report_error(ctx); | |
| 1185 } | |
| 1186 } | |
| 1187 | |
| 1188 cs_obj = pdf_dict_get(ctx, obj, PDF_NAME(DefaultRGB)); | |
| 1189 if (cs_obj) | |
| 1190 { | |
| 1191 fz_try(ctx) | |
| 1192 { | |
| 1193 fz_colorspace *cs = pdf_load_colorspace(ctx, cs_obj); | |
| 1194 fz_set_default_rgb(ctx, default_cs, cs); | |
| 1195 fz_drop_colorspace(ctx, cs); | |
| 1196 } | |
| 1197 fz_catch(ctx) | |
| 1198 { | |
| 1199 fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); | |
| 1200 fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); | |
| 1201 fz_report_error(ctx); | |
| 1202 } | |
| 1203 } | |
| 1204 | |
| 1205 cs_obj = pdf_dict_get(ctx, obj, PDF_NAME(DefaultCMYK)); | |
| 1206 if (cs_obj) | |
| 1207 { | |
| 1208 fz_try(ctx) | |
| 1209 { | |
| 1210 fz_colorspace *cs = pdf_load_colorspace(ctx, cs_obj); | |
| 1211 fz_set_default_cmyk(ctx, default_cs, cs); | |
| 1212 fz_drop_colorspace(ctx, cs); | |
| 1213 } | |
| 1214 fz_catch(ctx) | |
| 1215 { | |
| 1216 fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); | |
| 1217 fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); | |
| 1218 fz_report_error(ctx); | |
| 1219 } | |
| 1220 } | |
| 1221 } | |
| 1222 | |
| 1223 fz_default_colorspaces * | |
| 1224 pdf_load_default_colorspaces(fz_context *ctx, pdf_document *doc, pdf_page *page) | |
| 1225 { | |
| 1226 pdf_obj *res; | |
| 1227 pdf_obj *obj; | |
| 1228 fz_default_colorspaces *default_cs; | |
| 1229 fz_colorspace *oi; | |
| 1230 | |
| 1231 default_cs = fz_new_default_colorspaces(ctx); | |
| 1232 | |
| 1233 fz_try(ctx) | |
| 1234 { | |
| 1235 res = pdf_page_resources(ctx, page); | |
| 1236 obj = pdf_dict_get(ctx, res, PDF_NAME(ColorSpace)); | |
| 1237 if (obj) | |
| 1238 pdf_load_default_colorspaces_imp(ctx, default_cs, obj); | |
| 1239 | |
| 1240 oi = pdf_document_output_intent(ctx, doc); | |
| 1241 if (oi) | |
| 1242 fz_set_default_output_intent(ctx, default_cs, oi); | |
| 1243 } | |
| 1244 fz_catch(ctx) | |
| 1245 { | |
| 1246 if (fz_caught(ctx) != FZ_ERROR_TRYLATER) | |
| 1247 { | |
| 1248 fz_drop_default_colorspaces(ctx, default_cs); | |
| 1249 fz_rethrow(ctx); | |
| 1250 } | |
| 1251 fz_ignore_error(ctx); | |
| 1252 page->super.incomplete = 1; | |
| 1253 } | |
| 1254 | |
| 1255 return default_cs; | |
| 1256 } | |
| 1257 | |
| 1258 fz_default_colorspaces * | |
| 1259 pdf_update_default_colorspaces(fz_context *ctx, fz_default_colorspaces *old_cs, pdf_obj *res) | |
| 1260 { | |
| 1261 pdf_obj *obj; | |
| 1262 fz_default_colorspaces *new_cs; | |
| 1263 | |
| 1264 obj = pdf_dict_get(ctx, res, PDF_NAME(ColorSpace)); | |
| 1265 if (!obj) | |
| 1266 return fz_keep_default_colorspaces(ctx, old_cs); | |
| 1267 | |
| 1268 new_cs = fz_clone_default_colorspaces(ctx, old_cs); | |
| 1269 fz_try(ctx) | |
| 1270 pdf_load_default_colorspaces_imp(ctx, new_cs, obj); | |
| 1271 fz_catch(ctx) | |
| 1272 { | |
| 1273 fz_drop_default_colorspaces(ctx, new_cs); | |
| 1274 fz_rethrow(ctx); | |
| 1275 } | |
| 1276 | |
| 1277 return new_cs; | |
| 1278 } | |
| 1279 | |
| 1280 void pdf_nuke_page(fz_context *ctx, pdf_page *page) | |
| 1281 { | |
| 1282 pdf_nuke_links(ctx, page); | |
| 1283 pdf_nuke_annots(ctx, page); | |
| 1284 pdf_drop_obj(ctx, page->obj); | |
| 1285 page->obj = NULL; | |
| 1286 page->super.in_doc = 0; | |
| 1287 } | |
| 1288 | |
| 1289 void pdf_sync_page(fz_context *ctx, pdf_page *page) | |
| 1290 { | |
| 1291 pdf_sync_links(ctx, page); | |
| 1292 pdf_sync_annots(ctx, page); | |
| 1293 } | |
| 1294 | |
| 1295 void pdf_sync_open_pages(fz_context *ctx, pdf_document *doc) | |
| 1296 { | |
| 1297 fz_page *page, *next; | |
| 1298 pdf_page *ppage; | |
| 1299 int number; | |
| 1300 | |
| 1301 for (page = doc->super.open; page != NULL; page = next) | |
| 1302 { | |
| 1303 next = page->next; | |
| 1304 if (page->doc == NULL) | |
| 1305 continue; | |
| 1306 ppage = (pdf_page*)page; | |
| 1307 number = pdf_lookup_page_number(ctx, doc, ppage->obj); | |
| 1308 if (number < 0) | |
| 1309 { | |
| 1310 pdf_nuke_page(ctx, ppage); | |
| 1311 if (next) | |
| 1312 next->prev = page->prev; | |
| 1313 if (page->prev) | |
| 1314 *page->prev = page->next; | |
| 1315 } | |
| 1316 else | |
| 1317 { | |
| 1318 pdf_sync_page(ctx, ppage); | |
| 1319 page->number = number; | |
| 1320 } | |
| 1321 } | |
| 1322 } | |
| 1323 | |
| 1324 pdf_page * | |
| 1325 pdf_load_page(fz_context *ctx, pdf_document *doc, int number) | |
| 1326 { | |
| 1327 return (pdf_page*)fz_load_page(ctx, (fz_document*)doc, number); | |
| 1328 } | |
| 1329 | |
| 1330 int | |
| 1331 pdf_page_has_transparency(fz_context *ctx, pdf_page *page) | |
| 1332 { | |
| 1333 return page->transparency; | |
| 1334 } | |
| 1335 | |
| 1336 fz_page * | |
| 1337 pdf_load_page_imp(fz_context *ctx, fz_document *doc_, int chapter, int number) | |
| 1338 { | |
| 1339 pdf_document *doc = (pdf_document*)doc_; | |
| 1340 pdf_page *page; | |
| 1341 pdf_annot *annot; | |
| 1342 pdf_obj *pageobj, *obj; | |
| 1343 | |
| 1344 if (doc->is_fdf) | |
| 1345 fz_throw(ctx, FZ_ERROR_FORMAT, "FDF documents have no pages"); | |
| 1346 | |
| 1347 if (chapter != 0) | |
| 1348 fz_throw(ctx, FZ_ERROR_ARGUMENT, "invalid chapter number: %d", chapter); | |
| 1349 | |
| 1350 if (number < 0 || number >= pdf_count_pages(ctx, doc)) | |
| 1351 fz_throw(ctx, FZ_ERROR_ARGUMENT, "invalid page number: %d", number); | |
| 1352 | |
| 1353 if (doc->file_reading_linearly) | |
| 1354 { | |
| 1355 pageobj = pdf_progressive_advance(ctx, doc, number); | |
| 1356 if (pageobj == NULL) | |
| 1357 fz_throw(ctx, FZ_ERROR_TRYLATER, "page %d not available yet", number); | |
| 1358 } | |
| 1359 else | |
| 1360 pageobj = pdf_lookup_page_obj(ctx, doc, number); | |
| 1361 | |
| 1362 page = pdf_new_page(ctx, doc); | |
| 1363 page->obj = pdf_keep_obj(ctx, pageobj); | |
| 1364 | |
| 1365 /* Pre-load annotations and links */ | |
| 1366 fz_try(ctx) | |
| 1367 { | |
| 1368 obj = pdf_dict_get(ctx, pageobj, PDF_NAME(Annots)); | |
| 1369 if (obj) | |
| 1370 { | |
| 1371 fz_rect page_cropbox; | |
| 1372 fz_matrix page_ctm; | |
| 1373 pdf_page_transform(ctx, page, &page_cropbox, &page_ctm); | |
| 1374 page->links = pdf_load_link_annots(ctx, doc, page, obj, number, page_ctm); | |
| 1375 pdf_load_annots(ctx, page); | |
| 1376 } | |
| 1377 } | |
| 1378 fz_catch(ctx) | |
| 1379 { | |
| 1380 if (fz_caught(ctx) != FZ_ERROR_TRYLATER) | |
| 1381 { | |
| 1382 fz_drop_page(ctx, &page->super); | |
| 1383 fz_rethrow(ctx); | |
| 1384 } | |
| 1385 fz_ignore_error(ctx); | |
| 1386 page->super.incomplete = 1; | |
| 1387 fz_drop_link(ctx, page->links); | |
| 1388 page->links = NULL; | |
| 1389 } | |
| 1390 | |
| 1391 /* Scan for transparency and overprint */ | |
| 1392 fz_try(ctx) | |
| 1393 { | |
| 1394 pdf_obj *resources = pdf_page_resources(ctx, page); | |
| 1395 if (pdf_name_eq(ctx, pdf_dict_getp(ctx, pageobj, "Group/S"), PDF_NAME(Transparency))) | |
| 1396 page->transparency = 1; | |
| 1397 else if (pdf_resources_use_blending(ctx, resources, NULL)) | |
| 1398 page->transparency = 1; | |
| 1399 if (pdf_resources_use_overprint(ctx, resources, NULL)) | |
| 1400 page->overprint = 1; | |
| 1401 for (annot = page->annots; annot && !page->transparency; annot = annot->next) | |
| 1402 { | |
| 1403 fz_try(ctx) | |
| 1404 { | |
| 1405 pdf_obj *ap; | |
| 1406 pdf_obj *res; | |
| 1407 pdf_annot_push_local_xref(ctx, annot); | |
| 1408 ap = pdf_annot_ap(ctx, annot); | |
| 1409 if (!ap) | |
| 1410 break; | |
| 1411 res = pdf_xobject_resources(ctx, ap); | |
| 1412 if (pdf_resources_use_blending(ctx, res, NULL)) | |
| 1413 page->transparency = 1; | |
| 1414 if (pdf_resources_use_overprint(ctx, pdf_xobject_resources(ctx, res), NULL)) | |
| 1415 page->overprint = 1; | |
| 1416 } | |
| 1417 fz_always(ctx) | |
| 1418 pdf_annot_pop_local_xref(ctx, annot); | |
| 1419 fz_catch(ctx) | |
| 1420 fz_rethrow(ctx); | |
| 1421 } | |
| 1422 for (annot = page->widgets; annot && !page->transparency; annot = annot->next) | |
| 1423 { | |
| 1424 fz_try(ctx) | |
| 1425 { | |
| 1426 pdf_obj *ap; | |
| 1427 pdf_obj *res; | |
| 1428 pdf_annot_push_local_xref(ctx, annot); | |
| 1429 ap = pdf_annot_ap(ctx, annot); | |
| 1430 if (!ap) | |
| 1431 break; | |
| 1432 res = pdf_xobject_resources(ctx, ap); | |
| 1433 if (pdf_resources_use_blending(ctx, res, NULL)) | |
| 1434 page->transparency = 1; | |
| 1435 if (pdf_resources_use_overprint(ctx, pdf_xobject_resources(ctx, res), NULL)) | |
| 1436 page->overprint = 1; | |
| 1437 } | |
| 1438 fz_always(ctx) | |
| 1439 pdf_annot_pop_local_xref(ctx, annot); | |
| 1440 fz_catch(ctx) | |
| 1441 fz_rethrow(ctx); | |
| 1442 } | |
| 1443 } | |
| 1444 fz_catch(ctx) | |
| 1445 { | |
| 1446 if (fz_caught(ctx) != FZ_ERROR_TRYLATER) | |
| 1447 { | |
| 1448 fz_drop_page(ctx, &page->super); | |
| 1449 fz_rethrow(ctx); | |
| 1450 } | |
| 1451 fz_ignore_error(ctx); | |
| 1452 page->super.incomplete = 1; | |
| 1453 } | |
| 1454 | |
| 1455 return (fz_page*)page; | |
| 1456 } | |
| 1457 | |
| 1458 void | |
| 1459 pdf_delete_page(fz_context *ctx, pdf_document *doc, int at) | |
| 1460 { | |
| 1461 pdf_obj *parent, *kids; | |
| 1462 int i; | |
| 1463 | |
| 1464 pdf_begin_operation(ctx, doc, "Delete page"); | |
| 1465 fz_try(ctx) | |
| 1466 { | |
| 1467 pdf_lookup_page_loc(ctx, doc, at, &parent, &i); | |
| 1468 kids = pdf_dict_get(ctx, parent, PDF_NAME(Kids)); | |
| 1469 pdf_array_delete(ctx, kids, i); | |
| 1470 | |
| 1471 while (parent) | |
| 1472 { | |
| 1473 int count = pdf_dict_get_int(ctx, parent, PDF_NAME(Count)); | |
| 1474 pdf_dict_put_int(ctx, parent, PDF_NAME(Count), count - 1); | |
| 1475 parent = pdf_dict_get(ctx, parent, PDF_NAME(Parent)); | |
| 1476 } | |
| 1477 | |
| 1478 /* Adjust page labels */ | |
| 1479 pdf_adjust_page_labels(ctx, doc, at, -1); | |
| 1480 pdf_end_operation(ctx, doc); | |
| 1481 } | |
| 1482 fz_catch(ctx) | |
| 1483 { | |
| 1484 pdf_abandon_operation(ctx, doc); | |
| 1485 pdf_sync_open_pages(ctx, doc); | |
| 1486 fz_rethrow(ctx); | |
| 1487 } | |
| 1488 | |
| 1489 pdf_sync_open_pages(ctx, doc); | |
| 1490 } | |
| 1491 | |
| 1492 void | |
| 1493 pdf_delete_page_range(fz_context *ctx, pdf_document *doc, int start, int end) | |
| 1494 { | |
| 1495 int count = pdf_count_pages(ctx, doc); | |
| 1496 if (end < 0) | |
| 1497 end = count; | |
| 1498 start = fz_clampi(start, 0, count); | |
| 1499 end = fz_clampi(end, 0, count); | |
| 1500 while (start < end) | |
| 1501 { | |
| 1502 pdf_delete_page(ctx, doc, start); | |
| 1503 end--; | |
| 1504 } | |
| 1505 } | |
| 1506 | |
| 1507 pdf_obj * | |
| 1508 pdf_add_page(fz_context *ctx, pdf_document *doc, fz_rect mediabox, int rotate, pdf_obj *resources, fz_buffer *contents) | |
| 1509 { | |
| 1510 pdf_obj *page_obj = NULL; | |
| 1511 pdf_obj *page_ref = NULL; | |
| 1512 | |
| 1513 fz_var(page_obj); | |
| 1514 fz_var(page_ref); | |
| 1515 | |
| 1516 pdf_begin_operation(ctx, doc, "Add page"); | |
| 1517 | |
| 1518 fz_try(ctx) | |
| 1519 { | |
| 1520 page_obj = pdf_new_dict(ctx, doc, 5); | |
| 1521 | |
| 1522 pdf_dict_put(ctx, page_obj, PDF_NAME(Type), PDF_NAME(Page)); | |
| 1523 pdf_dict_put_rect(ctx, page_obj, PDF_NAME(MediaBox), mediabox); | |
| 1524 pdf_dict_put_int(ctx, page_obj, PDF_NAME(Rotate), rotate); | |
| 1525 | |
| 1526 if (pdf_is_indirect(ctx, resources)) | |
| 1527 pdf_dict_put(ctx, page_obj, PDF_NAME(Resources), resources); | |
| 1528 else if (pdf_is_dict(ctx, resources)) | |
| 1529 pdf_dict_put_drop(ctx, page_obj, PDF_NAME(Resources), pdf_add_object(ctx, doc, resources)); | |
| 1530 else | |
| 1531 pdf_dict_put_dict(ctx, page_obj, PDF_NAME(Resources), 1); | |
| 1532 | |
| 1533 if (contents && contents->len > 0) | |
| 1534 pdf_dict_put_drop(ctx, page_obj, PDF_NAME(Contents), pdf_add_stream(ctx, doc, contents, NULL, 0)); | |
| 1535 page_ref = pdf_add_object_drop(ctx, doc, page_obj); | |
| 1536 pdf_end_operation(ctx, doc); | |
| 1537 } | |
| 1538 fz_catch(ctx) | |
| 1539 { | |
| 1540 pdf_drop_obj(ctx, page_obj); | |
| 1541 pdf_abandon_operation(ctx, doc); | |
| 1542 fz_rethrow(ctx); | |
| 1543 } | |
| 1544 return page_ref; | |
| 1545 } | |
| 1546 | |
| 1547 void | |
| 1548 pdf_insert_page(fz_context *ctx, pdf_document *doc, int at, pdf_obj *page_ref) | |
| 1549 { | |
| 1550 int count = pdf_count_pages(ctx, doc); | |
| 1551 pdf_obj *parent, *kids; | |
| 1552 int i; | |
| 1553 | |
| 1554 if (at < 0) | |
| 1555 at = count; | |
| 1556 if (at == INT_MAX) | |
| 1557 at = count; | |
| 1558 if (at > count) | |
| 1559 fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot insert page beyond end of page tree"); | |
| 1560 | |
| 1561 pdf_begin_operation(ctx, doc, "Insert page"); | |
| 1562 | |
| 1563 fz_try(ctx) | |
| 1564 { | |
| 1565 if (count == 0) | |
| 1566 { | |
| 1567 pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)); | |
| 1568 parent = pdf_dict_get(ctx, root, PDF_NAME(Pages)); | |
| 1569 if (!parent) | |
| 1570 fz_throw(ctx, FZ_ERROR_FORMAT, "cannot find page tree"); | |
| 1571 kids = pdf_dict_get(ctx, parent, PDF_NAME(Kids)); | |
| 1572 if (!kids) | |
| 1573 fz_throw(ctx, FZ_ERROR_FORMAT, "malformed page tree"); | |
| 1574 pdf_array_insert(ctx, kids, page_ref, 0); | |
| 1575 } | |
| 1576 else if (at == count) | |
| 1577 { | |
| 1578 /* append after last page */ | |
| 1579 pdf_lookup_page_loc(ctx, doc, count - 1, &parent, &i); | |
| 1580 kids = pdf_dict_get(ctx, parent, PDF_NAME(Kids)); | |
| 1581 pdf_array_insert(ctx, kids, page_ref, i + 1); | |
| 1582 } | |
| 1583 else | |
| 1584 { | |
| 1585 /* insert before found page */ | |
| 1586 pdf_lookup_page_loc(ctx, doc, at, &parent, &i); | |
| 1587 kids = pdf_dict_get(ctx, parent, PDF_NAME(Kids)); | |
| 1588 pdf_array_insert(ctx, kids, page_ref, i); | |
| 1589 } | |
| 1590 | |
| 1591 pdf_dict_put(ctx, page_ref, PDF_NAME(Parent), parent); | |
| 1592 | |
| 1593 /* Adjust page counts */ | |
| 1594 while (parent) | |
| 1595 { | |
| 1596 count = pdf_dict_get_int(ctx, parent, PDF_NAME(Count)); | |
| 1597 pdf_dict_put_int(ctx, parent, PDF_NAME(Count), count + 1); | |
| 1598 parent = pdf_dict_get(ctx, parent, PDF_NAME(Parent)); | |
| 1599 } | |
| 1600 | |
| 1601 /* Adjust page labels */ | |
| 1602 pdf_adjust_page_labels(ctx, doc, at, 1); | |
| 1603 pdf_end_operation(ctx, doc); | |
| 1604 } | |
| 1605 fz_catch(ctx) | |
| 1606 { | |
| 1607 pdf_abandon_operation(ctx, doc); | |
| 1608 pdf_sync_open_pages(ctx, doc); | |
| 1609 fz_rethrow(ctx); | |
| 1610 } | |
| 1611 pdf_sync_open_pages(ctx, doc); | |
| 1612 } | |
| 1613 | |
| 1614 /* | |
| 1615 * Page Labels | |
| 1616 */ | |
| 1617 | |
| 1618 struct page_label_range { | |
| 1619 int offset; | |
| 1620 pdf_obj *label; | |
| 1621 int nums_ix; | |
| 1622 pdf_obj *nums; | |
| 1623 }; | |
| 1624 | |
| 1625 static void | |
| 1626 pdf_lookup_page_label_imp(fz_context *ctx, pdf_obj *node, int index, struct page_label_range *range) | |
| 1627 { | |
| 1628 pdf_obj *kids = pdf_dict_get(ctx, node, PDF_NAME(Kids)); | |
| 1629 pdf_obj *nums = pdf_dict_get(ctx, node, PDF_NAME(Nums)); | |
| 1630 int i; | |
| 1631 | |
| 1632 if (pdf_is_array(ctx, kids)) | |
| 1633 { | |
| 1634 for (i = 0; i < pdf_array_len(ctx, kids); ++i) | |
| 1635 { | |
| 1636 pdf_obj *kid = pdf_array_get(ctx, kids, i); | |
| 1637 pdf_lookup_page_label_imp(ctx, kid, index, range); | |
| 1638 } | |
| 1639 } | |
| 1640 | |
| 1641 if (pdf_is_array(ctx, nums)) | |
| 1642 { | |
| 1643 for (i = 0; i < pdf_array_len(ctx, nums); i += 2) | |
| 1644 { | |
| 1645 int k = pdf_array_get_int(ctx, nums, i); | |
| 1646 if (k <= index) | |
| 1647 { | |
| 1648 range->offset = k; | |
| 1649 range->label = pdf_array_get(ctx, nums, i + 1); | |
| 1650 range->nums_ix = i; | |
| 1651 range->nums = nums; | |
| 1652 } | |
| 1653 else | |
| 1654 { | |
| 1655 /* stop looking if we've already passed the index */ | |
| 1656 return; | |
| 1657 } | |
| 1658 } | |
| 1659 } | |
| 1660 } | |
| 1661 | |
| 1662 static struct page_label_range | |
| 1663 pdf_lookup_page_label(fz_context *ctx, pdf_document *doc, int index) | |
| 1664 { | |
| 1665 struct page_label_range range = { 0, NULL }; | |
| 1666 pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)); | |
| 1667 pdf_obj *labels = pdf_dict_get(ctx, root, PDF_NAME(PageLabels)); | |
| 1668 pdf_lookup_page_label_imp(ctx, labels, index, &range); | |
| 1669 return range; | |
| 1670 } | |
| 1671 | |
| 1672 static void | |
| 1673 pdf_flatten_page_label_tree_imp(fz_context *ctx, pdf_obj *node, pdf_obj *new_nums) | |
| 1674 { | |
| 1675 pdf_obj *kids = pdf_dict_get(ctx, node, PDF_NAME(Kids)); | |
| 1676 pdf_obj *nums = pdf_dict_get(ctx, node, PDF_NAME(Nums)); | |
| 1677 int i; | |
| 1678 | |
| 1679 if (pdf_is_array(ctx, kids)) | |
| 1680 { | |
| 1681 for (i = 0; i < pdf_array_len(ctx, kids); ++i) | |
| 1682 { | |
| 1683 pdf_obj *kid = pdf_array_get(ctx, kids, i); | |
| 1684 pdf_flatten_page_label_tree_imp(ctx, kid, new_nums); | |
| 1685 } | |
| 1686 } | |
| 1687 | |
| 1688 if (pdf_is_array(ctx, nums)) | |
| 1689 { | |
| 1690 for (i = 0; i < pdf_array_len(ctx, nums); i += 2) | |
| 1691 { | |
| 1692 pdf_array_push(ctx, new_nums, pdf_array_get(ctx, nums, i)); | |
| 1693 pdf_array_push(ctx, new_nums, pdf_array_get(ctx, nums, i + 1)); | |
| 1694 } | |
| 1695 } | |
| 1696 } | |
| 1697 | |
| 1698 static void | |
| 1699 pdf_flatten_page_label_tree(fz_context *ctx, pdf_document *doc) | |
| 1700 { | |
| 1701 pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)); | |
| 1702 pdf_obj *labels = pdf_dict_get(ctx, root, PDF_NAME(PageLabels)); | |
| 1703 pdf_obj *nums = pdf_dict_get(ctx, labels, PDF_NAME(Nums)); | |
| 1704 | |
| 1705 // Already flat... | |
| 1706 if (pdf_is_array(ctx, nums) && pdf_array_len(ctx, nums) >= 2) | |
| 1707 return; | |
| 1708 | |
| 1709 nums = pdf_new_array(ctx, doc, 8); | |
| 1710 fz_try(ctx) | |
| 1711 { | |
| 1712 if (!labels) | |
| 1713 labels = pdf_dict_put_dict(ctx, root, PDF_NAME(PageLabels), 1); | |
| 1714 | |
| 1715 pdf_flatten_page_label_tree_imp(ctx, labels, nums); | |
| 1716 | |
| 1717 pdf_dict_del(ctx, labels, PDF_NAME(Kids)); | |
| 1718 pdf_dict_del(ctx, labels, PDF_NAME(Limits)); | |
| 1719 pdf_dict_put(ctx, labels, PDF_NAME(Nums), nums); | |
| 1720 | |
| 1721 /* No Page Label tree found - insert one with default values */ | |
| 1722 if (pdf_array_len(ctx, nums) == 0) | |
| 1723 { | |
| 1724 pdf_obj *obj; | |
| 1725 pdf_array_push_int(ctx, nums, 0); | |
| 1726 obj = pdf_array_push_dict(ctx, nums, 1); | |
| 1727 pdf_dict_put(ctx, obj, PDF_NAME(S), PDF_NAME(D)); | |
| 1728 } | |
| 1729 } | |
| 1730 fz_always(ctx) | |
| 1731 pdf_drop_obj(ctx, nums); | |
| 1732 fz_catch(ctx) | |
| 1733 fz_rethrow(ctx); | |
| 1734 } | |
| 1735 | |
| 1736 static pdf_obj * | |
| 1737 pdf_create_page_label(fz_context *ctx, pdf_document *doc, pdf_page_label_style style, const char *prefix, int start) | |
| 1738 { | |
| 1739 pdf_obj *obj = pdf_new_dict(ctx, doc, 3); | |
| 1740 fz_try(ctx) | |
| 1741 { | |
| 1742 switch (style) | |
| 1743 { | |
| 1744 default: | |
| 1745 case PDF_PAGE_LABEL_NONE: | |
| 1746 break; | |
| 1747 case PDF_PAGE_LABEL_DECIMAL: | |
| 1748 pdf_dict_put(ctx, obj, PDF_NAME(S), PDF_NAME(D)); | |
| 1749 break; | |
| 1750 case PDF_PAGE_LABEL_ROMAN_UC: | |
| 1751 pdf_dict_put(ctx, obj, PDF_NAME(S), PDF_NAME(R)); | |
| 1752 break; | |
| 1753 case PDF_PAGE_LABEL_ROMAN_LC: | |
| 1754 pdf_dict_put(ctx, obj, PDF_NAME(S), PDF_NAME(r)); | |
| 1755 break; | |
| 1756 case PDF_PAGE_LABEL_ALPHA_UC: | |
| 1757 pdf_dict_put(ctx, obj, PDF_NAME(S), PDF_NAME(A)); | |
| 1758 break; | |
| 1759 case PDF_PAGE_LABEL_ALPHA_LC: | |
| 1760 pdf_dict_put(ctx, obj, PDF_NAME(S), PDF_NAME(a)); | |
| 1761 break; | |
| 1762 } | |
| 1763 if (prefix && strlen(prefix) > 0) | |
| 1764 pdf_dict_put_text_string(ctx, obj, PDF_NAME(P), prefix); | |
| 1765 if (start > 1) | |
| 1766 pdf_dict_put_int(ctx, obj, PDF_NAME(St), start); | |
| 1767 } | |
| 1768 fz_catch(ctx) | |
| 1769 { | |
| 1770 pdf_drop_obj(ctx, obj); | |
| 1771 fz_rethrow(ctx); | |
| 1772 } | |
| 1773 return obj; | |
| 1774 } | |
| 1775 | |
| 1776 static void | |
| 1777 pdf_adjust_page_labels(fz_context *ctx, pdf_document *doc, int index, int adjust) | |
| 1778 { | |
| 1779 pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)); | |
| 1780 pdf_obj *labels = pdf_dict_get(ctx, root, PDF_NAME(PageLabels)); | |
| 1781 | |
| 1782 // Skip the adjustment step if there are no page labels. | |
| 1783 // Exception: If we would adjust the label for page 0, we must create one! | |
| 1784 // Exception: If the document only has one page! | |
| 1785 if (labels || (adjust > 0 && index == 0 && pdf_count_pages(ctx, doc) > 1)) | |
| 1786 { | |
| 1787 struct page_label_range range; | |
| 1788 int i; | |
| 1789 | |
| 1790 // Ensure we have a flat page label tree with at least one entry. | |
| 1791 pdf_flatten_page_label_tree(ctx, doc); | |
| 1792 | |
| 1793 // Find page label affecting the page that triggered adjustment | |
| 1794 range = pdf_lookup_page_label(ctx, doc, index); | |
| 1795 | |
| 1796 // Shift all page labels on and after the inserted index | |
| 1797 if (adjust > 0) | |
| 1798 { | |
| 1799 if (range.offset == index) | |
| 1800 i = range.nums_ix; | |
| 1801 else | |
| 1802 i = range.nums_ix + 2; | |
| 1803 } | |
| 1804 | |
| 1805 // Shift all page labels after the removed index | |
| 1806 else | |
| 1807 { | |
| 1808 i = range.nums_ix + 2; | |
| 1809 } | |
| 1810 | |
| 1811 | |
| 1812 // Increase/decrease the indices in the name tree | |
| 1813 for (; i < pdf_array_len(ctx, range.nums); i += 2) | |
| 1814 pdf_array_put_int(ctx, range.nums, i, pdf_array_get_int(ctx, range.nums, i) + adjust); | |
| 1815 | |
| 1816 // TODO: delete page labels that have no effect (zero range) | |
| 1817 | |
| 1818 // Make sure the number tree always has an entry for page 0 | |
| 1819 if (adjust > 0 && index == 0) | |
| 1820 { | |
| 1821 pdf_array_insert_drop(ctx, range.nums, pdf_new_int(ctx, index), 0); | |
| 1822 pdf_array_insert_drop(ctx, range.nums, pdf_create_page_label(ctx, doc, PDF_PAGE_LABEL_DECIMAL, NULL, 1), 1); | |
| 1823 } | |
| 1824 } | |
| 1825 } | |
| 1826 | |
| 1827 void | |
| 1828 pdf_set_page_labels(fz_context *ctx, pdf_document *doc, | |
| 1829 int index, | |
| 1830 pdf_page_label_style style, const char *prefix, int start) | |
| 1831 { | |
| 1832 struct page_label_range range; | |
| 1833 | |
| 1834 pdf_begin_operation(ctx, doc, "Set page label"); | |
| 1835 fz_try(ctx) | |
| 1836 { | |
| 1837 // Ensure we have a flat page label tree with at least one entry. | |
| 1838 pdf_flatten_page_label_tree(ctx, doc); | |
| 1839 | |
| 1840 range = pdf_lookup_page_label(ctx, doc, index); | |
| 1841 | |
| 1842 if (range.offset == index) | |
| 1843 { | |
| 1844 // Replace label | |
| 1845 pdf_array_put_drop(ctx, range.nums, | |
| 1846 range.nums_ix + 1, | |
| 1847 pdf_create_page_label(ctx, doc, style, prefix, start)); | |
| 1848 } | |
| 1849 else | |
| 1850 { | |
| 1851 // Insert new label | |
| 1852 pdf_array_insert_drop(ctx, range.nums, | |
| 1853 pdf_new_int(ctx, index), | |
| 1854 range.nums_ix + 2); | |
| 1855 pdf_array_insert_drop(ctx, range.nums, | |
| 1856 pdf_create_page_label(ctx, doc, style, prefix, start), | |
| 1857 range.nums_ix + 3); | |
| 1858 } | |
| 1859 pdf_end_operation(ctx, doc); | |
| 1860 } | |
| 1861 fz_catch(ctx) | |
| 1862 { | |
| 1863 pdf_abandon_operation(ctx, doc); | |
| 1864 fz_rethrow(ctx); | |
| 1865 } | |
| 1866 } | |
| 1867 | |
| 1868 void | |
| 1869 pdf_delete_page_labels(fz_context *ctx, pdf_document *doc, int index) | |
| 1870 { | |
| 1871 struct page_label_range range; | |
| 1872 | |
| 1873 if (index == 0) | |
| 1874 { | |
| 1875 pdf_set_page_labels(ctx, doc, 0, PDF_PAGE_LABEL_DECIMAL, NULL, 1); | |
| 1876 return; | |
| 1877 } | |
| 1878 | |
| 1879 pdf_begin_operation(ctx, doc, "Delete page label"); | |
| 1880 fz_try(ctx) | |
| 1881 { | |
| 1882 // Ensure we have a flat page label tree with at least one entry. | |
| 1883 pdf_flatten_page_label_tree(ctx, doc); | |
| 1884 | |
| 1885 range = pdf_lookup_page_label(ctx, doc, index); | |
| 1886 | |
| 1887 if (range.offset == index) | |
| 1888 { | |
| 1889 // Delete label | |
| 1890 pdf_array_delete(ctx, range.nums, range.nums_ix); | |
| 1891 pdf_array_delete(ctx, range.nums, range.nums_ix); | |
| 1892 } | |
| 1893 pdf_end_operation(ctx, doc); | |
| 1894 } | |
| 1895 fz_catch(ctx) | |
| 1896 { | |
| 1897 pdf_abandon_operation(ctx, doc); | |
| 1898 fz_rethrow(ctx); | |
| 1899 } | |
| 1900 } | |
| 1901 | |
| 1902 static const char *roman_uc[3][10] = { | |
| 1903 { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }, | |
| 1904 { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" }, | |
| 1905 { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" }, | |
| 1906 }; | |
| 1907 | |
| 1908 static const char *roman_lc[3][10] = { | |
| 1909 { "", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix" }, | |
| 1910 { "", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc" }, | |
| 1911 { "", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm" }, | |
| 1912 }; | |
| 1913 | |
| 1914 static void pdf_format_roman_page_label(char *buf, int size, int n, const char *sym[3][10], const char *sym_m) | |
| 1915 { | |
| 1916 int I = n % 10; | |
| 1917 int X = (n / 10) % 10; | |
| 1918 int C = (n / 100) % 10; | |
| 1919 int M = (n / 1000); | |
| 1920 | |
| 1921 fz_strlcpy(buf, "", size); | |
| 1922 while (M--) | |
| 1923 fz_strlcat(buf, sym_m, size); | |
| 1924 fz_strlcat(buf, sym[2][C], size); | |
| 1925 fz_strlcat(buf, sym[1][X], size); | |
| 1926 fz_strlcat(buf, sym[0][I], size); | |
| 1927 } | |
| 1928 | |
| 1929 static void pdf_format_alpha_page_label(char *buf, int size, int n, int alpha) | |
| 1930 { | |
| 1931 int reps = (n - 1) / 26 + 1; | |
| 1932 if (reps > size - 1) | |
| 1933 reps = size - 1; | |
| 1934 memset(buf, (n - 1) % 26 + alpha, reps); | |
| 1935 buf[reps] = '\0'; | |
| 1936 } | |
| 1937 | |
| 1938 static void | |
| 1939 pdf_format_page_label(fz_context *ctx, int index, pdf_obj *dict, char *buf, size_t size) | |
| 1940 { | |
| 1941 pdf_obj *style = pdf_dict_get(ctx, dict, PDF_NAME(S)); | |
| 1942 const char *prefix = pdf_dict_get_text_string(ctx, dict, PDF_NAME(P)); | |
| 1943 int start = pdf_dict_get_int(ctx, dict, PDF_NAME(St)); | |
| 1944 size_t n; | |
| 1945 | |
| 1946 // St must be >= 1; default is 1. | |
| 1947 if (start < 1) | |
| 1948 start = 1; | |
| 1949 | |
| 1950 // Add prefix (optional; may be empty) | |
| 1951 fz_strlcpy(buf, prefix, size); | |
| 1952 n = strlen(buf); | |
| 1953 buf += n; | |
| 1954 size -= n; | |
| 1955 | |
| 1956 // Append number using style (optional) | |
| 1957 if (style == PDF_NAME(D)) | |
| 1958 fz_snprintf(buf, size, "%d", index + start); | |
| 1959 else if (style == PDF_NAME(R)) | |
| 1960 pdf_format_roman_page_label(buf, (int)size, index + start, roman_uc, "M"); | |
| 1961 else if (style == PDF_NAME(r)) | |
| 1962 pdf_format_roman_page_label(buf, (int)size, index + start, roman_lc, "m"); | |
| 1963 else if (style == PDF_NAME(A)) | |
| 1964 pdf_format_alpha_page_label(buf, (int)size, index + start, 'A'); | |
| 1965 else if (style == PDF_NAME(a)) | |
| 1966 pdf_format_alpha_page_label(buf, (int)size, index + start, 'a'); | |
| 1967 } | |
| 1968 | |
| 1969 void | |
| 1970 pdf_page_label(fz_context *ctx, pdf_document *doc, int index, char *buf, size_t size) | |
| 1971 { | |
| 1972 struct page_label_range range = pdf_lookup_page_label(ctx, doc, index); | |
| 1973 if (range.label) | |
| 1974 pdf_format_page_label(ctx, index - range.offset, range.label, buf, size); | |
| 1975 else | |
| 1976 fz_snprintf(buf, size, "%z", index + 1); | |
| 1977 } | |
| 1978 | |
| 1979 void | |
| 1980 pdf_page_label_imp(fz_context *ctx, fz_document *doc, int chapter, int page, char *buf, size_t size) | |
| 1981 { | |
| 1982 pdf_page_label(ctx, pdf_document_from_fz_document(ctx, doc), page, buf, size); | |
| 1983 } | |
| 1984 | |
| 1985 pdf_page * | |
| 1986 pdf_keep_page(fz_context *ctx, pdf_page *page) | |
| 1987 { | |
| 1988 return (pdf_page *) fz_keep_page(ctx, &page->super); | |
| 1989 } | |
| 1990 | |
| 1991 void | |
| 1992 pdf_drop_page(fz_context *ctx, pdf_page *page) | |
| 1993 { | |
| 1994 fz_drop_page(ctx, &page->super); | |
| 1995 } |
