| 1 |
/* Copyright (C) 2001-2025 Artifex Software, Inc. |
| 2 |
All Rights Reserved. |
| 3 |
|
| 4 |
This software is provided AS-IS with no warranty, either express or |
| 5 |
implied. |
| 6 |
|
| 7 |
This software is distributed under license and may not be copied, |
| 8 |
modified or distributed except as expressly authorized under the terms |
| 9 |
of the license contained in the file LICENSE in this distribution. |
| 10 |
|
| 11 |
Refer to licensing information at http://www.artifex.com or contact |
| 12 |
Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
| 13 |
CA 94129, USA, for further information. |
| 14 |
*/ |
| 15 |
|
| 16 |
/* pdfmark handling for the PDF interpreter */ |
| 17 |
|
| 18 |
#include "pdf_int.h" |
| 19 |
#include "pdf_stack.h" |
| 20 |
#include "pdf_file.h" |
| 21 |
#include "pdf_dict.h" |
| 22 |
#include "pdf_array.h" |
| 23 |
#include "pdf_loop_detect.h" |
| 24 |
#include "pdf_mark.h" |
| 25 |
#include "pdf_obj.h" |
| 26 |
#include "pdf_misc.h" |
| 27 |
#include "pdf_page.h" |
| 28 |
#include "pdf_deref.h" |
| 29 |
|
| 30 |
#include "gscoord.h" /* For gs_currentmatrix */ |
| 31 |
|
| 32 |
static int pdfi_get_named_dest(pdf_context *ctx, pdf_obj *Named, pdf_obj **Dest); |
| 33 |
|
| 34 |
static int pdfi_pdfmark_setparam_obj(pdf_context *ctx, pdf_obj *obj, gs_param_string *entry) |
| 35 |
{ |
| 36 |
int code = 0; |
| 37 |
byte *data = NULL; |
| 38 |
int size = 0; |
| 39 |
|
| 40 |
code = pdfi_obj_to_string(ctx, obj, &data, &size); |
| 41 |
if (code < 0) |
| 42 |
return code; |
| 43 |
entry->data = data; |
| 44 |
entry->size = size; |
| 45 |
entry->persistent = false; |
| 46 |
return 0; |
| 47 |
} |
| 48 |
|
| 49 |
static int pdfi_pdfmark_setparam_pair(pdf_context *ctx, pdf_name *Key, pdf_obj *Value, |
| 50 |
gs_param_string *entry) |
| 51 |
{ |
| 52 |
int code = 0; |
| 53 |
|
| 54 |
/* Handle the Key */ |
| 55 |
if (pdfi_type_of(Key) != PDF_NAME) { |
| 56 |
code = gs_note_error(gs_error_typecheck); |
| 57 |
goto exit; |
| 58 |
} |
| 59 |
|
| 60 |
code = pdfi_pdfmark_setparam_obj(ctx, (pdf_obj *)Key, entry); |
| 61 |
if (code < 0) |
| 62 |
goto exit; |
| 63 |
|
| 64 |
code = pdfi_pdfmark_setparam_obj(ctx, Value, entry+1); |
| 65 |
if (code < 0) |
| 66 |
goto exit; |
| 67 |
|
| 68 |
exit: |
| 69 |
return code; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
/* Note: this isn't part of the obj_to_string() stuff */ |
| 74 |
static int pdfi_pdfmark_ctm_str(pdf_context *ctx, gs_matrix *ctm, byte **data, int *len) |
| 75 |
{ |
| 76 |
int size = 100; |
| 77 |
char *buf; |
| 78 |
|
| 79 |
buf = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_pdfmark_ctm_str(data)"); |
| 80 |
if (buf == NULL) |
| 81 |
return_error(gs_error_VMerror); |
| 82 |
snprintf(buf, size, "[%.4f %.4f %.4f %.4f %.4f %.4f]", |
| 83 |
ctm->xx, ctm->xy, ctm->yx, ctm->yy, ctm->tx, ctm->ty); |
| 84 |
*data = (byte *)buf; |
| 85 |
*len = strlen(buf); |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
/* Write an string array command to the device (e.g. pdfmark) */ |
| 90 |
static int pdfi_pdfmark_write_array(pdf_context *ctx, gs_param_string_array *array_list, const char *command) |
| 91 |
{ |
| 92 |
gs_c_param_list list; |
| 93 |
int code; |
| 94 |
|
| 95 |
/* Set the list to writeable, and initialise it */ |
| 96 |
gs_c_param_list_write(&list, ctx->memory); |
| 97 |
/* We don't want keys to be persistent, as we are going to throw |
| 98 |
* away our array, force them to be copied |
| 99 |
*/ |
| 100 |
gs_param_list_set_persistent_keys((gs_param_list *) &list, false); |
| 101 |
|
| 102 |
/* Make really sure the list is writable, but don't initialise it */ |
| 103 |
gs_c_param_list_write_more(&list); |
| 104 |
|
| 105 |
/* Add the param string array to the list */ |
| 106 |
code = param_write_string_array((gs_param_list *)&list, command, array_list); |
| 107 |
if (code < 0) |
| 108 |
return code; |
| 109 |
|
| 110 |
/* Set the param list back to readable, so putceviceparams can readit (mad...) */ |
| 111 |
gs_c_param_list_read(&list); |
| 112 |
|
| 113 |
/* and set the actual device parameters */ |
| 114 |
code = gs_putdeviceparams(ctx->pgs->device, (gs_param_list *)&list); |
| 115 |
|
| 116 |
gs_c_param_list_release(&list); |
| 117 |
|
| 118 |
return code; |
| 119 |
} |
| 120 |
|
| 121 |
/* Write an string array command to the device (e.g. pdfmark) */ |
| 122 |
static int pdfi_pdfmark_write_string(pdf_context *ctx, gs_param_string *param_string, const char *command) |
| 123 |
{ |
| 124 |
gs_c_param_list list; |
| 125 |
int code; |
| 126 |
|
| 127 |
/* Set the list to writeable, and initialise it */ |
| 128 |
gs_c_param_list_write(&list, ctx->memory); |
| 129 |
/* We don't want keys to be persistent, as we are going to throw |
| 130 |
* away our array, force them to be copied |
| 131 |
*/ |
| 132 |
gs_param_list_set_persistent_keys((gs_param_list *) &list, false); |
| 133 |
|
| 134 |
/* Make really sure the list is writable, but don't initialise it */ |
| 135 |
gs_c_param_list_write_more(&list); |
| 136 |
|
| 137 |
/* Add the param string array to the list */ |
| 138 |
code = param_write_string((gs_param_list *)&list, command, param_string); |
| 139 |
if (code < 0) |
| 140 |
return code; |
| 141 |
|
| 142 |
/* Set the param list back to readable, so putceviceparams can readit (mad...) */ |
| 143 |
gs_c_param_list_read(&list); |
| 144 |
|
| 145 |
/* and set the actual device parameters */ |
| 146 |
code = gs_putdeviceparams(ctx->pgs->device, (gs_param_list *)&list); |
| 147 |
|
| 148 |
gs_c_param_list_release(&list); |
| 149 |
|
| 150 |
return code; |
| 151 |
} |
| 152 |
|
| 153 |
/* Apparently the strings to pass to the device are: |
| 154 |
key1 val1 .... keyN valN CTM "ANN" |
| 155 |
CTM is (for example) "[1.0 0 0 1.0 0 0]" |
| 156 |
|
| 157 |
This takes an optional 'label' argument which can be NULL. |
| 158 |
|
| 159 |
the /pdfmark command has this array of strings as a parameter, i.e. |
| 160 |
[ 'label' key1 val1 .... keyN valN CTM "ANN" ] /pdfmark |
| 161 |
|
| 162 |
Apparently the "type" doesn't have a leading "/" but the other names in the |
| 163 |
keys do need the "/" |
| 164 |
|
| 165 |
See plparams.c/process_pdfmark() |
| 166 |
*/ |
| 167 |
static int pdfi_pdfmark_from_dict_withlabel(pdf_context *ctx, pdf_indirect_ref *label, |
| 168 |
pdf_dict *dict, gs_matrix *ctm, const char *type) |
| 169 |
{ |
| 170 |
int code = 0; |
| 171 |
int size; |
| 172 |
uint64_t dictsize; |
| 173 |
uint64_t index; |
| 174 |
uint64_t keynum = 0; |
| 175 |
int i; |
| 176 |
pdf_name *Key = NULL; |
| 177 |
pdf_obj *Value = NULL; |
| 178 |
pdf_obj *tempobj = NULL; |
| 179 |
gs_param_string *parray = NULL; |
| 180 |
gs_param_string_array array_list; |
| 181 |
byte *ctm_data = NULL; |
| 182 |
int ctm_len; |
| 183 |
gs_matrix ctm_placeholder; |
| 184 |
int offset = 0; |
| 185 |
|
| 186 |
/* If ctm not provided, make a placeholder */ |
| 187 |
if (!ctm) { |
| 188 |
gs_currentmatrix(ctx->pgs, &ctm_placeholder); |
| 189 |
ctm = &ctm_placeholder; |
| 190 |
} |
| 191 |
|
| 192 |
dictsize = pdfi_dict_entries(dict); |
| 193 |
size = dictsize*2 + 2; /* pairs + CTM + type */ |
| 194 |
if (label) |
| 195 |
size += 1; |
| 196 |
|
| 197 |
parray = (gs_param_string *)gs_alloc_bytes(ctx->memory, (size_t)size*sizeof(gs_param_string), |
| 198 |
"pdfi_pdfmark_from_dict(parray)"); |
| 199 |
if (parray == NULL) { |
| 200 |
code = gs_note_error(gs_error_VMerror); |
| 201 |
goto exit; |
| 202 |
} |
| 203 |
memset(parray, 0, size*sizeof(gs_param_string)); |
| 204 |
|
| 205 |
if (label) { |
| 206 |
code = pdfi_pdfmark_setparam_obj(ctx, (pdf_obj *)label, parray+0); |
| 207 |
offset += 1; |
| 208 |
} |
| 209 |
|
| 210 |
/* Get each (key,val) pair from dict and setup param for it */ |
| 211 |
if (dictsize > 0) { |
| 212 |
code = pdfi_dict_key_first(ctx, dict, (pdf_obj **)&Key, &index); |
| 213 |
while (code >= 0) { |
| 214 |
code = pdfi_dict_get_no_deref(ctx, dict, Key, &Value); |
| 215 |
if (code < 0) goto exit; |
| 216 |
|
| 217 |
code = pdfi_pdfmark_setparam_pair(ctx, Key, Value, parray+offset+(keynum*2)); |
| 218 |
if (code < 0) goto exit; |
| 219 |
|
| 220 |
pdfi_countdown(Key); |
| 221 |
Key = NULL; |
| 222 |
pdfi_countdown(Value); |
| 223 |
Value = NULL; |
| 224 |
pdfi_countdown(tempobj); |
| 225 |
tempobj = NULL; |
| 226 |
|
| 227 |
code = pdfi_dict_key_next(ctx, dict, (pdf_obj **)&Key, &index); |
| 228 |
if (code == gs_error_undefined) { |
| 229 |
code = 0; |
| 230 |
break; |
| 231 |
} |
| 232 |
keynum ++; |
| 233 |
} |
| 234 |
} |
| 235 |
if (code < 0) goto exit; |
| 236 |
|
| 237 |
/* CTM */ |
| 238 |
code = pdfi_pdfmark_ctm_str(ctx, ctm, &ctm_data, &ctm_len); |
| 239 |
if (code < 0) goto exit; |
| 240 |
parray[size-2].data = ctm_data; |
| 241 |
parray[size-2].size = ctm_len; |
| 242 |
|
| 243 |
/* Type (e.g. ANN, DOCINFO) */ |
| 244 |
parray[size-1].data = (const byte *)type; |
| 245 |
parray[size-1].size = strlen(type); |
| 246 |
|
| 247 |
array_list.data = parray; |
| 248 |
array_list.persistent = false; |
| 249 |
array_list.size = size; |
| 250 |
|
| 251 |
code = pdfi_pdfmark_write_array(ctx, &array_list, "pdfmark"); |
| 252 |
|
| 253 |
exit: |
| 254 |
pdfi_countdown(Key); |
| 255 |
pdfi_countdown(Value); |
| 256 |
pdfi_countdown(tempobj); |
| 257 |
if (parray != NULL) { |
| 258 |
/* Free the param data except the last two which are handled separately */ |
| 259 |
for (i=0; i<size-2; i++) { |
| 260 |
if (parray[i].data) |
| 261 |
gs_free_object(ctx->memory, (byte *)parray[i].data, "pdfi_pdfmark_from_dict(parray)"); |
| 262 |
} |
| 263 |
} |
| 264 |
if (ctm_data) |
| 265 |
gs_free_object(ctx->memory, ctm_data, "pdfi_pdfmark_from_dict(ctm_data)"); |
| 266 |
gs_free_object(ctx->memory, parray, "pdfi_pdfmark_from_dict(parray)"); |
| 267 |
return code; |
| 268 |
} |
| 269 |
|
| 270 |
/* Do a pdfmark from a dictionary */ |
| 271 |
int pdfi_pdfmark_from_dict(pdf_context *ctx, pdf_dict *dict, gs_matrix *ctm, const char *type) |
| 272 |
{ |
| 273 |
return pdfi_pdfmark_from_dict_withlabel(ctx, NULL, dict, ctm, type); |
| 274 |
} |
| 275 |
|
| 276 |
/* Does a pdfmark, from a c-array of pdf_obj's |
| 277 |
* This will put in a dummy ctm if none provided |
| 278 |
*/ |
| 279 |
int pdfi_pdfmark_from_objarray(pdf_context *ctx, pdf_obj **objarray, int len, |
| 280 |
gs_matrix *ctm, const char *type) |
| 281 |
{ |
| 282 |
int code = 0; |
| 283 |
int size; |
| 284 |
gs_param_string *parray = NULL; |
| 285 |
gs_param_string_array array_list; |
| 286 |
byte *ctm_data = NULL; |
| 287 |
int ctm_len; |
| 288 |
gs_matrix ctm_placeholder; |
| 289 |
int i; |
| 290 |
|
| 291 |
/* If ctm not provided, make a placeholder */ |
| 292 |
if (!ctm) { |
| 293 |
gs_currentmatrix(ctx->pgs, &ctm_placeholder); |
| 294 |
ctm = &ctm_placeholder; |
| 295 |
} |
| 296 |
|
| 297 |
size = len + 2; /* data + CTM + type */ |
| 298 |
|
| 299 |
parray = (gs_param_string *)gs_alloc_bytes(ctx->memory, (size_t)size*sizeof(gs_param_string), |
| 300 |
"pdfi_pdfmark_from_objarray(parray)"); |
| 301 |
if (parray == NULL) { |
| 302 |
code = gs_note_error(gs_error_VMerror); |
| 303 |
goto exit; |
| 304 |
} |
| 305 |
memset(parray, 0, size *sizeof(gs_param_string)); |
| 306 |
|
| 307 |
for (i=0; i<len; i++) { |
| 308 |
code = pdfi_pdfmark_setparam_obj(ctx, objarray[i], parray+i); |
| 309 |
if (code < 0) goto exit; |
| 310 |
} |
| 311 |
|
| 312 |
/* CTM */ |
| 313 |
code = pdfi_pdfmark_ctm_str(ctx, ctm, &ctm_data, &ctm_len); |
| 314 |
if (code < 0) goto exit; |
| 315 |
parray[len].data = ctm_data; |
| 316 |
parray[len].size = ctm_len; |
| 317 |
|
| 318 |
/* Type (e.g. ANN, DOCINFO) */ |
| 319 |
parray[len+1].data = (const byte *)type; |
| 320 |
parray[len+1].size = strlen(type); |
| 321 |
|
| 322 |
array_list.data = parray; |
| 323 |
array_list.persistent = false; |
| 324 |
array_list.size = size; |
| 325 |
|
| 326 |
code = pdfi_pdfmark_write_array(ctx, &array_list, "pdfmark"); |
| 327 |
|
| 328 |
exit: |
| 329 |
if (parray != NULL) { |
| 330 |
for (i=0; i<len; i++) { |
| 331 |
gs_free_object(ctx->memory, (byte *)parray[i].data, "pdfi_pdfmark_from_objarray(parray)"); |
| 332 |
} |
| 333 |
} |
| 334 |
if (ctm_data) |
| 335 |
gs_free_object(ctx->memory, ctm_data, "pdfi_pdfmark_from_objarray(ctm_data)"); |
| 336 |
gs_free_object(ctx->memory, parray, "pdfi_pdfmark_from_objarray(parray)"); |
| 337 |
return code; |
| 338 |
} |
| 339 |
|
| 340 |
/* Send an arbitrary object as a string, with command 'cmd' |
| 341 |
* This is not a pdfmark, has no ctm. |
| 342 |
*/ |
| 343 |
int pdfi_pdfmark_object(pdf_context *ctx, pdf_obj *object, const char *cmd) |
| 344 |
{ |
| 345 |
gs_param_string param_string; |
| 346 |
int code = 0; |
| 347 |
|
| 348 |
param_string.data = NULL; |
| 349 |
|
| 350 |
code = pdfi_loop_detector_mark(ctx); |
| 351 |
if (code < 0) |
| 352 |
goto exit; |
| 353 |
|
| 354 |
code = pdfi_resolve_indirect_loop_detect(ctx, NULL, object, true); |
| 355 |
(void)pdfi_loop_detector_cleartomark(ctx); |
| 356 |
if (code < 0) |
| 357 |
goto exit; |
| 358 |
|
| 359 |
code = pdfi_pdfmark_setparam_obj(ctx, object, ¶m_string); |
| 360 |
if (code < 0) |
| 361 |
goto exit; |
| 362 |
|
| 363 |
code = pdfi_pdfmark_write_string(ctx, ¶m_string, cmd); |
| 364 |
|
| 365 |
exit: |
| 366 |
if (param_string.data != NULL) |
| 367 |
gs_free_object(ctx->memory, (byte *)param_string.data, "free data transferred to param_string in pdfi_pdfmark_object\n"); |
| 368 |
return code; |
| 369 |
} |
| 370 |
|
| 371 |
/* Convert a Dest array to the hacky Page and View keys that pdfwrite expects |
| 372 |
* dest_array: [<page_ref> <view_info>] |
| 373 |
* page_ref: indirect ref to a page dict |
| 374 |
* view_info: see table 8.2 in PDF 1.7, for example "/Fit" |
| 375 |
* |
| 376 |
* Removes /Dest and inserts two key pairs: /Page N and /View <view_info> |
| 377 |
* N is the page number, which starts at 1, not 0. |
| 378 |
*/ |
| 379 |
static int pdfi_pdfmark_add_Page_View(pdf_context *ctx, pdf_dict *link_dict, pdf_array *dest_array) |
| 380 |
{ |
| 381 |
int code = 0; |
| 382 |
int i; |
| 383 |
uint64_t page_num; |
| 384 |
pdf_dict *page_dict = NULL; |
| 385 |
pdf_array *view_array = NULL; |
| 386 |
uint64_t array_size; |
| 387 |
pdf_obj *temp_obj = NULL; |
| 388 |
|
| 389 |
/* Get the page_dict, without storing the deref in the array. |
| 390 |
* (This is needed because otherwise there could be a circular ref that will |
| 391 |
* lead to a memory leak) |
| 392 |
*/ |
| 393 |
code = pdfi_array_get_no_store_R(ctx, dest_array, 0, (pdf_obj **)&page_dict); |
| 394 |
if (code < 0) goto exit; |
| 395 |
|
| 396 |
if(pdfi_type_of(page_dict) == PDF_INT) { |
| 397 |
page_num = ((pdf_num *)page_dict)->value.i; |
| 398 |
} else { |
| 399 |
if (pdfi_type_of(page_dict) != PDF_DICT) { |
| 400 |
if (pdfi_type_of(page_dict) != PDF_NULL) { |
| 401 |
code = gs_note_error(gs_error_typecheck); |
| 402 |
goto exit; |
| 403 |
} |
| 404 |
page_num = 0; |
| 405 |
} else { |
| 406 |
/* Find out which page number this is */ |
| 407 |
code = pdfi_page_get_number(ctx, page_dict, &page_num); |
| 408 |
if (code < 0) goto exit; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
page_num += ctx->Pdfmark_InitialPage; |
| 413 |
|
| 414 |
if (pdfi_type_of(page_dict) != PDF_NULL) |
| 415 |
/* Add /Page key to the link_dict |
| 416 |
* Of course pdfwrite is numbering its pages starting at 1, because... of course :( |
| 417 |
*/ |
| 418 |
code = pdfi_dict_put_int(ctx, link_dict, "Page", page_num+1); |
| 419 |
else |
| 420 |
code = pdfi_dict_put_int(ctx, link_dict, "Page", 0); |
| 421 |
|
| 422 |
if (code < 0) |
| 423 |
goto exit; |
| 424 |
|
| 425 |
/* Get and check the destination type, again without storing the deref in the array. |
| 426 |
* In this case a memory leak can only happen if the destination |
| 427 |
* is an invalid type, but lets take no chances. |
| 428 |
*/ |
| 429 |
code = pdfi_array_get_no_store_R(ctx, dest_array, 1, &temp_obj); |
| 430 |
if (code < 0) goto exit; |
| 431 |
|
| 432 |
if (pdfi_type_of(temp_obj) != PDF_NAME) { |
| 433 |
pdfi_countdown(temp_obj); |
| 434 |
temp_obj = NULL; |
| 435 |
code = gs_note_error(gs_error_typecheck); |
| 436 |
goto exit; |
| 437 |
} |
| 438 |
pdfi_countdown(temp_obj); |
| 439 |
temp_obj = NULL; |
| 440 |
|
| 441 |
/* Build an array for /View, out of the remainder of the Dest entry */ |
| 442 |
array_size = pdfi_array_size(dest_array) - 1; |
| 443 |
code = pdfi_array_alloc(ctx, array_size, &view_array); |
| 444 |
if (code < 0) goto exit; |
| 445 |
pdfi_countup(view_array); |
| 446 |
for (i=0; i<array_size; i++) { |
| 447 |
code = pdfi_array_get(ctx, dest_array, i+1, &temp_obj); |
| 448 |
if (code < 0) goto exit; |
| 449 |
code = pdfi_array_put(ctx, view_array, i, temp_obj); |
| 450 |
if (code < 0) goto exit; |
| 451 |
|
| 452 |
pdfi_countdown(temp_obj); |
| 453 |
temp_obj = NULL; |
| 454 |
} |
| 455 |
/* Add /View key to the link_dict */ |
| 456 |
code = pdfi_dict_put(ctx, link_dict, "View", (pdf_obj *)view_array); |
| 457 |
if (code < 0) goto exit; |
| 458 |
|
| 459 |
exit: |
| 460 |
pdfi_countdown(temp_obj); |
| 461 |
pdfi_countdown(page_dict); |
| 462 |
pdfi_countdown(view_array); |
| 463 |
return code; |
| 464 |
} |
| 465 |
|
| 466 |
/* Lookup a Dest string(or name) in the Names array and try to resolve it */ |
| 467 |
static int pdfi_pdfmark_handle_dest_names(pdf_context *ctx, pdf_dict *link_dict, |
| 468 |
pdf_obj *dest, pdf_array *Names) |
| 469 |
{ |
| 470 |
int code = 0; |
| 471 |
int i; |
| 472 |
uint64_t array_size; |
| 473 |
pdf_obj *name = NULL; |
| 474 |
pdf_dict *D_dict = NULL; |
| 475 |
bool found = false; |
| 476 |
pdf_array *dest_array = NULL; |
| 477 |
|
| 478 |
array_size = pdfi_array_size(Names); |
| 479 |
|
| 480 |
/* Needs to be an even number - pairs of [name, obj] */ |
| 481 |
if (array_size % 2 != 0) { |
| 482 |
/* TODO: flag an error? */ |
| 483 |
/* Let's just ignore the last unpaired item for now */ |
| 484 |
array_size -= 1; |
| 485 |
} |
| 486 |
|
| 487 |
for (i=0; i<array_size; i+=2) { |
| 488 |
code = pdfi_array_get(ctx, Names, i, (pdf_obj **)&name); |
| 489 |
if (code < 0) goto exit; |
| 490 |
/* Note: in current implementation, PDF_STRING and PDF_NAME have all the same |
| 491 |
* fields, but just in case that changes I treat them separately here. |
| 492 |
*/ |
| 493 |
if (pdfi_type_of(name) == PDF_STRING && pdfi_type_of(dest) == PDF_STRING) { |
| 494 |
if (!pdfi_string_cmp((pdf_string *)name, (pdf_string *)dest)) { |
| 495 |
found = true; |
| 496 |
break; |
| 497 |
} |
| 498 |
} else if (pdfi_type_of(name) == PDF_NAME && pdfi_type_of(dest) == PDF_NAME) { |
| 499 |
if (!pdfi_name_cmp((pdf_name *)name, (pdf_name *)dest)) { |
| 500 |
found = true; |
| 501 |
break; |
| 502 |
} |
| 503 |
} |
| 504 |
pdfi_countdown(name); |
| 505 |
name = NULL; |
| 506 |
} |
| 507 |
|
| 508 |
if (!found) { |
| 509 |
/* TODO: flag a warning? */ |
| 510 |
code = 0; |
| 511 |
goto exit; |
| 512 |
} |
| 513 |
|
| 514 |
/* Next entry is either a dictionary (with a /D key) or an array */ |
| 515 |
code = pdfi_array_get(ctx, Names, i+1, (pdf_obj **)&D_dict); |
| 516 |
if (code < 0) goto exit; |
| 517 |
|
| 518 |
switch (pdfi_type_of(D_dict)) { |
| 519 |
case PDF_DICT: |
| 520 |
/* Dict is supposed to contain key "D" with Dest array */ |
| 521 |
code = pdfi_dict_knownget_type(ctx, D_dict, "D", PDF_ARRAY, (pdf_obj **)&dest_array); |
| 522 |
if (code <= 0) goto exit; |
| 523 |
break; |
| 524 |
case PDF_ARRAY: |
| 525 |
dest_array = (pdf_array *)D_dict; |
| 526 |
D_dict = NULL; |
| 527 |
break; |
| 528 |
default: |
| 529 |
code = gs_note_error(gs_error_typecheck); |
| 530 |
goto exit; |
| 531 |
} |
| 532 |
|
| 533 |
/* Process the dest_array to replace with /Page /View */ |
| 534 |
code = pdfi_pdfmark_add_Page_View(ctx, link_dict, dest_array); |
| 535 |
if (code < 0) goto exit; |
| 536 |
|
| 537 |
exit: |
| 538 |
pdfi_countdown(name); |
| 539 |
pdfi_countdown(D_dict); |
| 540 |
pdfi_countdown(dest_array); |
| 541 |
return code; |
| 542 |
} |
| 543 |
|
| 544 |
/* Special handling for "Dest" in Links |
| 545 |
* Will replace /Dest with /Page /View in link_dict (for pdfwrite) |
| 546 |
*/ |
| 547 |
int pdfi_pdfmark_modDest(pdf_context *ctx, pdf_dict *link_dict) |
| 548 |
{ |
| 549 |
int code = 0, code1 = 0; |
| 550 |
pdf_dict *Dests = NULL; |
| 551 |
pdf_obj *Dest = NULL; |
| 552 |
bool delete_Dest = true; |
| 553 |
pdf_array *dest_array = NULL; |
| 554 |
pdf_array *Names = NULL; |
| 555 |
pdf_dict *Names_dict = NULL; |
| 556 |
|
| 557 |
code = pdfi_dict_get(ctx, link_dict, "Dest", (pdf_obj **)&Dest); |
| 558 |
if (code < 0) goto exit; |
| 559 |
|
| 560 |
code = pdfi_dict_knownget_type(ctx, ctx->Root, "Dests", PDF_DICT, (pdf_obj **)&Dests); |
| 561 |
if (code < 0) goto exit; |
| 562 |
|
| 563 |
code = pdfi_dict_knownget_type(ctx, ctx->Root, "Names", PDF_DICT, (pdf_obj **)&Names_dict); |
| 564 |
if (code < 0) goto exit; |
| 565 |
|
| 566 |
switch (pdfi_type_of(Dest)) { |
| 567 |
case PDF_ARRAY: |
| 568 |
code = pdfi_pdfmark_add_Page_View(ctx, link_dict, (pdf_array *)Dest); |
| 569 |
if (code < 0) goto exit; |
| 570 |
break; |
| 571 |
case PDF_NAME: |
| 572 |
if (Dests != NULL) { |
| 573 |
/* Case where it's a name to look up in Contents(Root) /Dests */ |
| 574 |
code = pdfi_dict_get_by_key(ctx, Dests, (const pdf_name *)Dest, (pdf_obj **)&dest_array); |
| 575 |
if (code == gs_error_undefined) { |
| 576 |
/* TODO: Not found, should flag a warning */ |
| 577 |
code = 0; |
| 578 |
goto exit; |
| 579 |
} |
| 580 |
if (code < 0) goto exit; |
| 581 |
if (pdfi_type_of(dest_array) != PDF_ARRAY) { |
| 582 |
code = gs_note_error(gs_error_typecheck); |
| 583 |
goto exit; |
| 584 |
} |
| 585 |
code = pdfi_pdfmark_add_Page_View(ctx, link_dict, dest_array); |
| 586 |
if (code < 0) goto exit; |
| 587 |
break; |
| 588 |
} |
| 589 |
/* fallthrough */ |
| 590 |
case PDF_STRING: |
| 591 |
if (Names_dict != NULL) { |
| 592 |
/* Looking in Catalog(Root) for /Names<</Dests<</Names [name dict array]>>>> */ |
| 593 |
code = pdfi_dict_knownget_type(ctx, Names_dict, "Dests", PDF_DICT, (pdf_obj **)&Dests); |
| 594 |
if (code < 0) goto exit; |
| 595 |
if (code == 0) { |
| 596 |
/* TODO: Not found -- not sure if there is another case here or not */ |
| 597 |
code = gs_note_error(gs_error_undefined); |
| 598 |
goto exit; |
| 599 |
} |
| 600 |
|
| 601 |
code = pdfi_dict_knownget_type(ctx, Dests, "Names", PDF_ARRAY, (pdf_obj **)&Names); |
| 602 |
if (code < 0) goto exit; |
| 603 |
if (code == 0) { |
| 604 |
code = pdfi_get_named_dest(ctx, Dest, (pdf_obj **)&dest_array); |
| 605 |
if (code < 0) |
| 606 |
goto exit; |
| 607 |
if (pdfi_type_of(dest_array) == PDF_DICT) { |
| 608 |
pdf_obj *D = NULL; |
| 609 |
/* Dict is supposed to contain key "D" with Dest array */ |
| 610 |
code = pdfi_dict_knownget_type(ctx, (pdf_dict *)dest_array, "D", PDF_ARRAY, &D); |
| 611 |
if (code <= 0) goto exit; |
| 612 |
|
| 613 |
pdfi_countdown(dest_array); |
| 614 |
dest_array = (pdf_array *)D; |
| 615 |
} |
| 616 |
if (pdfi_type_of(dest_array) == PDF_ARRAY) { |
| 617 |
code = pdfi_pdfmark_add_Page_View(ctx, link_dict, dest_array); |
| 618 |
if (code < 0) |
| 619 |
goto exit; |
| 620 |
} else { |
| 621 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_undefined), NULL, E_PDF_BAD_NAMED_DEST, "pdfi_pdfmark_modDest", NULL)) < 0) |
| 622 |
goto exit; |
| 623 |
} |
| 624 |
break; |
| 625 |
} else |
| 626 |
code = pdfi_pdfmark_handle_dest_names(ctx, link_dict, Dest, Names); |
| 627 |
if (code < 0) goto exit; |
| 628 |
} else { |
| 629 |
/* TODO: Ignore it -- flag a warning? */ |
| 630 |
} |
| 631 |
break; |
| 632 |
default: |
| 633 |
/* TODO: Ignore it -- flag a warning? */ |
| 634 |
break; |
| 635 |
} |
| 636 |
|
| 637 |
exit: |
| 638 |
if (delete_Dest) { |
| 639 |
/* Delete the Dest key */ |
| 640 |
code1 = pdfi_dict_delete(ctx, link_dict, "Dest"); |
| 641 |
if (code1 < 0 && code >= 0) |
| 642 |
code = code1; |
| 643 |
} |
| 644 |
pdfi_countdown(Dest); |
| 645 |
pdfi_countdown(Dests); |
| 646 |
pdfi_countdown(Names); |
| 647 |
pdfi_countdown(Names_dict); |
| 648 |
pdfi_countdown(dest_array); |
| 649 |
return code; |
| 650 |
} |
| 651 |
|
| 652 |
static int pdfi_check_limits(pdf_context *ctx, pdf_dict *node, char *str, int len) |
| 653 |
{ |
| 654 |
int code = 0, min, i, len2 = 0; |
| 655 |
pdf_array *Limits = NULL; |
| 656 |
pdf_string *Str = NULL; |
| 657 |
char *str2 = NULL; |
| 658 |
|
| 659 |
code = pdfi_dict_get_type(ctx, node, "Limits", PDF_ARRAY, (pdf_obj **)&Limits); |
| 660 |
if (code < 0) |
| 661 |
goto error; |
| 662 |
|
| 663 |
if (pdfi_array_size(Limits) != 2) { |
| 664 |
/* Limits are not valid, just ignore them. The calling code will then check |
| 665 |
* the Names array. |
| 666 |
*/ |
| 667 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_TREE_LIMITS, "pdfi_check_limits", 0); |
| 668 |
goto error; |
| 669 |
} |
| 670 |
|
| 671 |
code = pdfi_array_get_type(ctx, Limits, 0, PDF_STRING, (pdf_obj **)&Str); |
| 672 |
if (code < 0) |
| 673 |
goto error; |
| 674 |
|
| 675 |
if (pdfi_type_of(Str) == PDF_NAME) { |
| 676 |
code = pdfi_string_from_name(ctx, (pdf_name *)Str, &str2, &len2); |
| 677 |
if (code < 0) |
| 678 |
return code; |
| 679 |
} else { |
| 680 |
len2 = ((pdf_string *)Str)->length; |
| 681 |
str2 = (char *)gs_alloc_bytes(ctx->memory, (size_t)len2 + 1, "pdfi_check_limits"); |
| 682 |
if (str2 == NULL) { |
| 683 |
code = gs_note_error(gs_error_VMerror); |
| 684 |
goto error; |
| 685 |
} |
| 686 |
memcpy(str2, ((pdf_string *)Str)->data, len2); |
| 687 |
str2[len2] = 0; |
| 688 |
} |
| 689 |
|
| 690 |
pdfi_countdown(Str); |
| 691 |
Str = NULL; |
| 692 |
|
| 693 |
min = len; |
| 694 |
if (len2 < min) |
| 695 |
min = len2; |
| 696 |
|
| 697 |
for (i=0;i< min;i++) { |
| 698 |
if (str[i] < str2[i]) { |
| 699 |
code = gs_note_error(gs_error_undefined); |
| 700 |
goto error; |
| 701 |
} |
| 702 |
if (str[i] != str2[i]) |
| 703 |
break; |
| 704 |
} |
| 705 |
if (i > min && len2 < Str->length) { |
| 706 |
code = gs_note_error(gs_error_undefined); |
| 707 |
goto error; |
| 708 |
} |
| 709 |
gs_free_object(ctx->memory, str2, "pdfi_check_limits"); |
| 710 |
str2 = NULL; |
| 711 |
|
| 712 |
code = pdfi_array_get_type(ctx, Limits, 1, PDF_STRING, (pdf_obj **)&Str); |
| 713 |
if (code < 0) |
| 714 |
goto error; |
| 715 |
|
| 716 |
if (pdfi_type_of(Str) == PDF_NAME) { |
| 717 |
code = pdfi_string_from_name(ctx, (pdf_name *)Str, &str2, &len2); |
| 718 |
if (code < 0) |
| 719 |
return code; |
| 720 |
} else { |
| 721 |
len2 = ((pdf_string *)Str)->length; |
| 722 |
str2 = (char *)gs_alloc_bytes(ctx->memory, (size_t)len2 + 1, "pdfi_check_limits"); |
| 723 |
if (str2 == NULL) { |
| 724 |
code = gs_note_error(gs_error_VMerror); |
| 725 |
goto error; |
| 726 |
} |
| 727 |
memcpy(str2, ((pdf_string *)Str)->data, len2); |
| 728 |
str2[len2] = 0; |
| 729 |
} |
| 730 |
|
| 731 |
pdfi_countdown(Str); |
| 732 |
Str = NULL; |
| 733 |
|
| 734 |
min = len; |
| 735 |
if (len2 < min) |
| 736 |
min = len2; |
| 737 |
|
| 738 |
for (i=0;i< min;i++) { |
| 739 |
if (str[i] > str2[i]) { |
| 740 |
code = gs_note_error(gs_error_undefined); |
| 741 |
goto error; |
| 742 |
} |
| 743 |
if (str[i] != str2[i]) |
| 744 |
break; |
| 745 |
} |
| 746 |
|
| 747 |
if (i > min && len > len2) |
| 748 |
code = gs_note_error(gs_error_undefined); |
| 749 |
|
| 750 |
error: |
| 751 |
gs_free_object(ctx->memory, str2, "pdfi_check_limits"); |
| 752 |
pdfi_countdown(Str); |
| 753 |
pdfi_countdown(Limits); |
| 754 |
return code; |
| 755 |
} |
| 756 |
|
| 757 |
static int pdfi_get_name_from_node(pdf_context *ctx, pdf_dict *node, char *str, int len, pdf_obj **Name, bool is_root) |
| 758 |
{ |
| 759 |
int i = 0, code = 0; |
| 760 |
pdf_string *StrKey = NULL; |
| 761 |
pdf_array *NamesArray = NULL; |
| 762 |
pdf_dict *Kid = NULL; |
| 763 |
bool known; |
| 764 |
|
| 765 |
code = pdfi_loop_detector_mark(ctx); |
| 766 |
if (code < 0) |
| 767 |
return code; |
| 768 |
|
| 769 |
code = pdfi_dict_known(ctx, node, "Names", &known); |
| 770 |
if (code < 0) |
| 771 |
goto error; |
| 772 |
|
| 773 |
if (known) { |
| 774 |
code = pdfi_dict_known(ctx, node, "Limits", &known); |
| 775 |
if (code < 0) |
| 776 |
goto error; |
| 777 |
|
| 778 |
if (!known) { |
| 779 |
if (!is_root) |
| 780 |
/* No Limits array (a required entry, except in the Root), so just assume that the |
| 781 |
* string is in this node and check all the Names anyway |
| 782 |
*/ |
| 783 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_NO_TREE_LIMITS, "pdfi_get_name_from_node", 0); |
| 784 |
} else { |
| 785 |
code = pdfi_check_limits(ctx, node, str, len); |
| 786 |
if (code < 0) |
| 787 |
goto error; |
| 788 |
} |
| 789 |
|
| 790 |
code = pdfi_dict_get_type(ctx, node, "Names", PDF_ARRAY, (pdf_obj **)&NamesArray); |
| 791 |
if (code < 0) |
| 792 |
goto error; |
| 793 |
|
| 794 |
if (pdfi_array_size(NamesArray) & 1) |
| 795 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_NAMES_ARRAY_SIZE, "pdfi_get_name_from_node", 0); |
| 796 |
|
| 797 |
for (i = 0;i < pdfi_array_size(NamesArray) / 2; i++) { |
| 798 |
code = pdfi_array_get_type(ctx, NamesArray, i * 2, PDF_STRING, (pdf_obj **)&StrKey); |
| 799 |
if (code < 0) |
| 800 |
goto error; |
| 801 |
|
| 802 |
if (StrKey->length == len && memcmp((const char *)StrKey->data, str, len) == 0) { |
| 803 |
code = pdfi_array_get(ctx, NamesArray, (i * 2) + 1, (pdf_obj **)Name); |
| 804 |
goto error; |
| 805 |
} |
| 806 |
pdfi_countdown(StrKey); |
| 807 |
StrKey = NULL; |
| 808 |
} |
| 809 |
pdfi_countdown(NamesArray); |
| 810 |
NamesArray = NULL; |
| 811 |
} |
| 812 |
|
| 813 |
/* Either no Names array (initial node) or not in array */ |
| 814 |
code = pdfi_dict_get_type(ctx, node, "Kids", PDF_ARRAY, (pdf_obj **)&NamesArray); |
| 815 |
if (code < 0) |
| 816 |
goto error; |
| 817 |
|
| 818 |
for (i = 0;i < pdfi_array_size(NamesArray); i++) { |
| 819 |
code = pdfi_array_get_type(ctx, NamesArray, i, PDF_DICT, (pdf_obj **)&Kid); |
| 820 |
if (code < 0) |
| 821 |
goto error; |
| 822 |
|
| 823 |
code = pdfi_get_name_from_node(ctx, Kid, str, len, Name, false); |
| 824 |
pdfi_countdown(Kid); |
| 825 |
Kid = NULL; |
| 826 |
if (code == 0) |
| 827 |
break; |
| 828 |
|
| 829 |
if (code < 0) { |
| 830 |
if (code == gs_error_undefined) |
| 831 |
continue; |
| 832 |
goto error; |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
error: |
| 837 |
pdfi_countdown(Kid); |
| 838 |
pdfi_countdown(StrKey); |
| 839 |
pdfi_countdown(NamesArray); |
| 840 |
(void)pdfi_loop_detector_cleartomark(ctx); |
| 841 |
return code; |
| 842 |
} |
| 843 |
|
| 844 |
static int pdfi_get_named_dest(pdf_context *ctx, pdf_obj *Named, pdf_obj **Dest) |
| 845 |
{ |
| 846 |
int code = 0, len = 0; |
| 847 |
pdf_dict *Names = NULL, *Dests = NULL; |
| 848 |
char *str = NULL; |
| 849 |
|
| 850 |
code = pdfi_loop_detector_mark(ctx); |
| 851 |
if (code < 0) |
| 852 |
return code; |
| 853 |
|
| 854 |
code = pdfi_dict_get_type(ctx, ctx->Root, "Names", PDF_DICT, (pdf_obj **)&Names); |
| 855 |
if (code < 0) |
| 856 |
goto error; |
| 857 |
|
| 858 |
code = pdfi_dict_get_type(ctx, Names, "Dests", PDF_DICT, (pdf_obj **)&Dests); |
| 859 |
if (code < 0) |
| 860 |
goto error; |
| 861 |
|
| 862 |
if (pdfi_type_of(Named) == PDF_NAME) { |
| 863 |
code = pdfi_string_from_name(ctx, (pdf_name *)Named, &str, &len); |
| 864 |
if (code < 0) |
| 865 |
return code; |
| 866 |
} else { |
| 867 |
len = ((pdf_string *)Named)->length; |
| 868 |
str = (char *)gs_alloc_bytes(ctx->memory, (size_t)len + 1, "pdfi_get_named_dest"); |
| 869 |
if (str == NULL) { |
| 870 |
code = gs_note_error(gs_error_VMerror); |
| 871 |
goto error; |
| 872 |
} |
| 873 |
memcpy(str, ((pdf_string *)Named)->data, len); |
| 874 |
str[len] = 0; |
| 875 |
} |
| 876 |
|
| 877 |
code = pdfi_get_name_from_node(ctx, Dests, str, len, Dest, true); |
| 878 |
|
| 879 |
error: |
| 880 |
if (pdfi_type_of(Named) == PDF_NAME) |
| 881 |
(void)pdfi_free_string_from_name(ctx, str); |
| 882 |
else |
| 883 |
gs_free_object(ctx->memory, str, "pdfi_get_named_dest"); |
| 884 |
pdfi_countdown(Names); |
| 885 |
pdfi_countdown(Dests); |
| 886 |
pdfi_loop_detector_cleartomark(ctx); |
| 887 |
return code; |
| 888 |
} |
| 889 |
|
| 890 |
/* Special handling for "A" in Link annotations and Outlines |
| 891 |
* Will delete A if handled and if A_key is provided. |
| 892 |
*/ |
| 893 |
int pdfi_pdfmark_modA(pdf_context *ctx, pdf_dict *dict) |
| 894 |
{ |
| 895 |
int code = 0; |
| 896 |
pdf_dict *A_dict = NULL; |
| 897 |
bool known; |
| 898 |
pdf_name *S_name = NULL; |
| 899 |
pdf_array *D_array = NULL; |
| 900 |
bool delete_A = false; |
| 901 |
bool deref_A = true; |
| 902 |
|
| 903 |
code = pdfi_dict_get_no_store_R(ctx, dict, "A", (pdf_obj **)&A_dict); |
| 904 |
if (code < 0) goto exit; |
| 905 |
|
| 906 |
if (pdfi_type_of(A_dict) != PDF_DICT) { |
| 907 |
/* Invalid AP, just delete it because I dunno what to do... |
| 908 |
* TODO: Should flag a warning here |
| 909 |
*/ |
| 910 |
delete_A = true; |
| 911 |
goto exit; |
| 912 |
} |
| 913 |
|
| 914 |
/* Handle URI */ |
| 915 |
code = pdfi_dict_known(ctx, A_dict, "URI", &known); |
| 916 |
if (code < 0) goto exit; |
| 917 |
if (known) { |
| 918 |
code = pdfi_resolve_indirect_loop_detect(ctx, (pdf_obj *)NULL, (pdf_obj *)A_dict, true); |
| 919 |
goto exit; |
| 920 |
} |
| 921 |
|
| 922 |
/* Handle S = GoTo */ |
| 923 |
/* TODO: this handles <</S /GoTo /D [dest array]>> |
| 924 |
* Not sure if there are other cases to handle? |
| 925 |
*/ |
| 926 |
code = pdfi_dict_knownget_type(ctx, A_dict, "S", PDF_NAME, (pdf_obj **)&S_name); |
| 927 |
if (code <= 0) goto exit; |
| 928 |
/* We only handle GoTo for now */ |
| 929 |
if (pdfi_name_is(S_name, "GoTo")) { |
| 930 |
code = pdfi_dict_knownget(ctx, A_dict, "D", (pdf_obj **)&D_array); |
| 931 |
if (code <= 0) |
| 932 |
goto exit; |
| 933 |
if (pdfi_type_of(D_array) == PDF_STRING || pdfi_type_of(D_array) == PDF_NAME) |
| 934 |
{ |
| 935 |
pdf_obj *Dest = NULL; |
| 936 |
|
| 937 |
code = pdfi_get_named_dest(ctx, (pdf_obj *)D_array, &Dest); |
| 938 |
if (code < 0) |
| 939 |
goto exit; |
| 940 |
pdfi_countdown(D_array); |
| 941 |
D_array = NULL; |
| 942 |
if (pdfi_type_of(Dest) != PDF_ARRAY) { |
| 943 |
if (pdfi_type_of(Dest) != PDF_DICT) { |
| 944 |
pdfi_countdown(Dest); |
| 945 |
code = gs_note_error(gs_error_typecheck); |
| 946 |
goto exit; |
| 947 |
} |
| 948 |
code = pdfi_dict_knownget(ctx, (pdf_dict *)Dest, "D", (pdf_obj **)&D_array); |
| 949 |
pdfi_countdown(Dest); |
| 950 |
if (code <= 0) |
| 951 |
goto exit; |
| 952 |
} else |
| 953 |
D_array = (pdf_array *)Dest; |
| 954 |
} |
| 955 |
if (pdfi_type_of(D_array) != PDF_ARRAY) { |
| 956 |
code = gs_note_error(gs_error_typecheck); |
| 957 |
goto exit; |
| 958 |
} |
| 959 |
|
| 960 |
/* Process the D array to replace with /Page /View */ |
| 961 |
code = pdfi_pdfmark_add_Page_View(ctx, dict, D_array); |
| 962 |
if (code < 0) goto exit; |
| 963 |
delete_A = true; |
| 964 |
} else if (pdfi_name_is(S_name, "GoToR") || pdfi_name_is(S_name, "Launch")) { |
| 965 |
/* These point out of the document. |
| 966 |
* Flatten out the reference, but otherwise leave it alone. |
| 967 |
* gs does some wack stuff here. |
| 968 |
* |
| 969 |
* Currently this is same behavior as gs, but it is not correct behavior. |
| 970 |
* In at least some cases we could do better, for example if the doc |
| 971 |
* pointed to happens to be the same file. |
| 972 |
* Sample: fts_28_2808.pdf |
| 973 |
* Sample: ~/work/samples/tests_private/pdf/sumatra/1874_-_clicking_ToC_link_crashes.pdf |
| 974 |
*/ |
| 975 |
code = pdfi_resolve_indirect_loop_detect(ctx, (pdf_obj *)dict, (pdf_obj *)A_dict, true); |
| 976 |
delete_A = false; |
| 977 |
code = 0; |
| 978 |
} else if (pdfi_name_is(S_name, "Named")) { |
| 979 |
/* We can just pass this through and it will work fine |
| 980 |
* This should be a name like "FirstPage" or "LastPage". |
| 981 |
* Note: gs implementation translates into page numbers and also has some bugs... |
| 982 |
* Sample: fts_33_3310.pdf |
| 983 |
*/ |
| 984 |
delete_A = false; |
| 985 |
code = 0; |
| 986 |
} else if (pdfi_name_is(S_name, "GoToE")) { |
| 987 |
/* TODO: ?? |
| 988 |
* File: fts_33_3303.pdf |
| 989 |
*/ |
| 990 |
} else if (pdfi_name_is(S_name, "Thread")) { |
| 991 |
/* TODO: For basically all of these below, I think just need to preserve |
| 992 |
* any references and streams and pass it all through. |
| 993 |
* File: fts_33_3305.pdf fts_33_3317.pdf |
| 994 |
*/ |
| 995 |
deref_A = false; |
| 996 |
} else if (pdfi_name_is(S_name, "Sound")) { |
| 997 |
/* TODO: ?? |
| 998 |
* File: fts_33_3307.pdf |
| 999 |
*/ |
| 1000 |
deref_A = false; |
| 1001 |
} else if (pdfi_name_is(S_name, "Movie")) { |
| 1002 |
/* TODO: ?? |
| 1003 |
* File: fts_33_3308.pdf |
| 1004 |
*/ |
| 1005 |
deref_A = false; |
| 1006 |
} else if (pdfi_name_is(S_name, "GoTo3DView")) { |
| 1007 |
/* TODO: ?? |
| 1008 |
* File: fts_33_3318.pdf |
| 1009 |
*/ |
| 1010 |
} else if (pdfi_name_is(S_name, "RichMediaExecute")) { |
| 1011 |
/* TODO: ?? |
| 1012 |
* File: fts_33_3319.pdf |
| 1013 |
*/ |
| 1014 |
} else if (pdfi_name_is(S_name, "Rendition")) { |
| 1015 |
/* TODO: make sure to pass through accurately? |
| 1016 |
* File: fts_07_0709.pdf fts_33_3316.pdf |
| 1017 |
*/ |
| 1018 |
} else { |
| 1019 |
/* TODO: flag warning? */ |
| 1020 |
} |
| 1021 |
|
| 1022 |
exit: |
| 1023 |
if (delete_A) { |
| 1024 |
code = pdfi_dict_delete(ctx, dict, "A"); |
| 1025 |
} else if (deref_A) { |
| 1026 |
pdfi_countdown(A_dict); |
| 1027 |
A_dict = NULL; |
| 1028 |
} |
| 1029 |
pdfi_countdown(A_dict); |
| 1030 |
pdfi_countdown(S_name); |
| 1031 |
pdfi_countdown(D_array); |
| 1032 |
return code; |
| 1033 |
} |
| 1034 |
|
| 1035 |
/* Begin defining an object |
| 1036 |
* Send an OBJ (_objdef) command |
| 1037 |
* (_objdef) (<label>) (/type) (/<type>) OBJ |
| 1038 |
*/ |
| 1039 |
static int pdfi_pdfmark_objdef_begin(pdf_context *ctx, pdf_indirect_ref *label, const char *type) |
| 1040 |
{ |
| 1041 |
int code; |
| 1042 |
pdf_obj *objarray[4]; |
| 1043 |
int num_objects = 4; |
| 1044 |
int i; |
| 1045 |
|
| 1046 |
memset(objarray, 0, sizeof(objarray)); |
| 1047 |
|
| 1048 |
code = pdfi_obj_charstr_to_name(ctx, "_objdef", (pdf_name **)&objarray[0]); |
| 1049 |
if (code < 0) goto exit; |
| 1050 |
|
| 1051 |
objarray[1] = (pdf_obj *)label; |
| 1052 |
pdfi_countup(label); |
| 1053 |
|
| 1054 |
code = pdfi_obj_charstr_to_name(ctx, "type", (pdf_name **)&objarray[2]); |
| 1055 |
if (code < 0) goto exit; |
| 1056 |
|
| 1057 |
code = pdfi_obj_charstr_to_name(ctx, type, (pdf_name **)&objarray[3]); |
| 1058 |
if (code < 0) goto exit; |
| 1059 |
|
| 1060 |
code = pdfi_pdfmark_from_objarray(ctx, objarray, num_objects, NULL, "OBJ"); |
| 1061 |
if (code < 0) goto exit; |
| 1062 |
|
| 1063 |
exit: |
| 1064 |
for (i=0; i<num_objects; i++) |
| 1065 |
pdfi_countdown(objarray[i]); |
| 1066 |
return code; |
| 1067 |
} |
| 1068 |
|
| 1069 |
/* Close an object |
| 1070 |
* Send a CLOSE command |
| 1071 |
* (<label>) CLOSE |
| 1072 |
*/ |
| 1073 |
static int pdfi_pdfmark_objdef_close(pdf_context *ctx, pdf_indirect_ref *label) |
| 1074 |
{ |
| 1075 |
int code; |
| 1076 |
pdf_obj *objarray[1]; |
| 1077 |
int num_objects = 1; |
| 1078 |
int i; |
| 1079 |
|
| 1080 |
memset(objarray, 0, sizeof(objarray)); |
| 1081 |
|
| 1082 |
objarray[0] = (pdf_obj *)label; |
| 1083 |
pdfi_countup(label); |
| 1084 |
|
| 1085 |
code = pdfi_pdfmark_from_objarray(ctx, objarray, num_objects, NULL, "CLOSE"); |
| 1086 |
if (code < 0) goto exit; |
| 1087 |
|
| 1088 |
exit: |
| 1089 |
for (i=0; i<num_objects; i++) |
| 1090 |
pdfi_countdown(objarray[i]); |
| 1091 |
return code; |
| 1092 |
} |
| 1093 |
|
| 1094 |
static int pdfi_pdfmark_stream_contents(pdf_context *ctx, pdf_indirect_ref *label, pdf_stream *stream) |
| 1095 |
{ |
| 1096 |
int code; |
| 1097 |
pdf_obj *objarray[2]; |
| 1098 |
int num_objects = 2; |
| 1099 |
int i; |
| 1100 |
|
| 1101 |
objarray[0] = (pdf_obj *)label; |
| 1102 |
pdfi_countup(label); |
| 1103 |
|
| 1104 |
objarray[1] = (pdf_obj *)stream; |
| 1105 |
pdfi_countup(stream); |
| 1106 |
stream->is_marking = true; |
| 1107 |
|
| 1108 |
code = pdfi_pdfmark_from_objarray(ctx, objarray, num_objects, NULL, ".PUTSTREAM"); |
| 1109 |
if (code < 0) goto exit; |
| 1110 |
|
| 1111 |
exit: |
| 1112 |
stream->is_marking = false; |
| 1113 |
for (i=0; i<num_objects; i++) |
| 1114 |
pdfi_countdown(objarray[i]); |
| 1115 |
return code; |
| 1116 |
} |
| 1117 |
|
| 1118 |
/* Mark a stream object */ |
| 1119 |
int pdfi_pdfmark_stream(pdf_context *ctx, pdf_stream *stream) |
| 1120 |
{ |
| 1121 |
int code; |
| 1122 |
pdf_dict *streamdict = NULL; |
| 1123 |
pdf_indirect_ref *streamref = NULL; |
| 1124 |
pdf_dict *tempdict = NULL; |
| 1125 |
uint64_t dictsize; |
| 1126 |
uint64_t index; |
| 1127 |
pdf_name *Key = NULL; |
| 1128 |
|
| 1129 |
if (stream->stream_written) |
| 1130 |
return 0; |
| 1131 |
|
| 1132 |
stream->stream_written = true; |
| 1133 |
|
| 1134 |
if (!ctx->device_state.writepdfmarks) |
| 1135 |
return 0; |
| 1136 |
|
| 1137 |
/* Create an indirect ref for the stream */ |
| 1138 |
code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&streamref); |
| 1139 |
if (code < 0) goto exit; |
| 1140 |
pdfi_countup(streamref); |
| 1141 |
streamref->ref_object_num = stream->object_num; |
| 1142 |
streamref->ref_generation_num = stream->generation_num; |
| 1143 |
streamref->is_marking = true; |
| 1144 |
|
| 1145 |
code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream, &streamdict); |
| 1146 |
if (code < 0) goto exit; |
| 1147 |
|
| 1148 |
/* Make a copy of the dict and remove Filter keyword */ |
| 1149 |
dictsize = pdfi_dict_entries(streamdict); |
| 1150 |
code = pdfi_dict_alloc(ctx, dictsize, &tempdict); |
| 1151 |
if (code < 0) goto exit; |
| 1152 |
pdfi_countup(tempdict); |
| 1153 |
code = pdfi_dict_copy(ctx, tempdict, streamdict); |
| 1154 |
if (code < 0) goto exit; |
| 1155 |
code = pdfi_dict_key_first(ctx, streamdict, (pdf_obj **)&Key, &index); |
| 1156 |
while (code >= 0) { |
| 1157 |
if (pdfi_name_is(Key, "Filter") || pdfi_name_is(Key, "Length")) { |
| 1158 |
code = pdfi_dict_delete_pair(ctx, tempdict, Key); |
| 1159 |
if (code < 0) goto exit; |
| 1160 |
} |
| 1161 |
pdfi_countdown(Key); |
| 1162 |
Key = NULL; |
| 1163 |
|
| 1164 |
code = pdfi_dict_key_next(ctx, streamdict, (pdf_obj **)&Key, &index); |
| 1165 |
if (code == gs_error_undefined) { |
| 1166 |
code = 0; |
| 1167 |
break; |
| 1168 |
} |
| 1169 |
} |
| 1170 |
if (code < 0) goto exit; |
| 1171 |
|
| 1172 |
code = pdfi_pdfmark_objdef_begin(ctx, streamref, "stream"); |
| 1173 |
if (code < 0) goto exit; |
| 1174 |
|
| 1175 |
code = pdfi_pdfmark_from_dict_withlabel(ctx, streamref, tempdict, NULL, ".PUTDICT"); |
| 1176 |
if (code < 0) goto exit; |
| 1177 |
|
| 1178 |
code = pdfi_pdfmark_stream_contents(ctx, streamref, stream); |
| 1179 |
if (code < 0) goto exit; |
| 1180 |
|
| 1181 |
code = pdfi_pdfmark_objdef_close(ctx, streamref); |
| 1182 |
if (code < 0) goto exit; |
| 1183 |
|
| 1184 |
exit: |
| 1185 |
pdfi_countdown(tempdict); |
| 1186 |
pdfi_countdown(streamref); |
| 1187 |
return code; |
| 1188 |
} |
| 1189 |
|
| 1190 |
/* Mark a dict object */ |
| 1191 |
int pdfi_pdfmark_dict(pdf_context *ctx, pdf_dict *dict) |
| 1192 |
{ |
| 1193 |
int code; |
| 1194 |
pdf_indirect_ref *dictref = NULL; |
| 1195 |
|
| 1196 |
if (dict->dict_written) |
| 1197 |
return 0; |
| 1198 |
|
| 1199 |
dict->dict_written = true; |
| 1200 |
|
| 1201 |
if (!ctx->device_state.writepdfmarks) |
| 1202 |
return 0; |
| 1203 |
|
| 1204 |
/* Create an indirect ref for the dict */ |
| 1205 |
code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&dictref); |
| 1206 |
if (code < 0) goto exit; |
| 1207 |
pdfi_countup(dictref); |
| 1208 |
dictref->ref_object_num = dict->object_num; |
| 1209 |
dictref->ref_generation_num = dict->generation_num; |
| 1210 |
dictref->is_marking = true; |
| 1211 |
|
| 1212 |
code = pdfi_pdfmark_objdef_begin(ctx, dictref, "dict"); |
| 1213 |
if (code < 0) goto exit; |
| 1214 |
|
| 1215 |
code = pdfi_pdfmark_from_dict_withlabel(ctx, dictref, dict, NULL, ".PUTDICT"); |
| 1216 |
if (code < 0) goto exit; |
| 1217 |
|
| 1218 |
exit: |
| 1219 |
pdfi_countdown(dictref); |
| 1220 |
return code; |
| 1221 |
} |
| 1222 |
|
| 1223 |
static int pdfi_pdfmark_filespec(pdf_context *ctx, pdf_string *name, pdf_dict *filespec) |
| 1224 |
{ |
| 1225 |
int code; |
| 1226 |
pdf_dict *tempdict = NULL; |
| 1227 |
|
| 1228 |
code = pdfi_dict_alloc(ctx, 40, &tempdict); |
| 1229 |
if (code < 0) goto exit; |
| 1230 |
pdfi_countup(tempdict); |
| 1231 |
|
| 1232 |
code = pdfi_dict_put(ctx, tempdict, "Name", (pdf_obj *)name); |
| 1233 |
if (code < 0) goto exit; |
| 1234 |
|
| 1235 |
/* Flatten the filespec */ |
| 1236 |
code = pdfi_resolve_indirect(ctx, (pdf_obj *)filespec, true); |
| 1237 |
if (code < 0) goto exit; |
| 1238 |
|
| 1239 |
code = pdfi_dict_put(ctx, tempdict, "FS", (pdf_obj *)filespec); |
| 1240 |
if (code < 0) goto exit; |
| 1241 |
|
| 1242 |
code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "EMBED"); |
| 1243 |
if (code < 0) goto exit; |
| 1244 |
|
| 1245 |
exit: |
| 1246 |
pdfi_countdown(tempdict); |
| 1247 |
return code; |
| 1248 |
} |
| 1249 |
|
| 1250 |
/* embed a file */ |
| 1251 |
int pdfi_pdfmark_embed_filespec(pdf_context *ctx, pdf_string *name, pdf_dict *filespec) |
| 1252 |
{ |
| 1253 |
int code; |
| 1254 |
|
| 1255 |
code = pdfi_pdfmark_filespec(ctx, name, filespec); |
| 1256 |
if (code < 0) goto exit; |
| 1257 |
|
| 1258 |
exit: |
| 1259 |
return code; |
| 1260 |
} |
| 1261 |
|
| 1262 |
/* |
| 1263 |
* Create and emit a /DOCINFO pdfmark for any and all of Title, |
| 1264 |
* Author, Subject, Keywords and Creator |
| 1265 |
*/ |
| 1266 |
void pdfi_pdfmark_write_docinfo(pdf_context *ctx, pdf_dict *info_dict) |
| 1267 |
{ |
| 1268 |
int i, code = 0; |
| 1269 |
pdf_dict *Info = NULL; |
| 1270 |
pdf_obj *o = NULL; |
| 1271 |
/* We don't preserve the Producer, we are the Producer */ |
| 1272 |
const char *KeyNames[] = { |
| 1273 |
"Title", "Author", "Subject", "Keywords", "Creator" |
| 1274 |
}; |
| 1275 |
|
| 1276 |
if (!ctx->device_state.writepdfmarks) |
| 1277 |
return; |
| 1278 |
|
| 1279 |
code = pdfi_dict_alloc(ctx, 5, &Info); |
| 1280 |
if (code < 0) |
| 1281 |
goto exit; |
| 1282 |
pdfi_countup(Info); |
| 1283 |
|
| 1284 |
for (i=0;i<5;i++) |
| 1285 |
{ |
| 1286 |
if (pdfi_dict_knownget(ctx, info_dict, KeyNames[i], &o) > 0) |
| 1287 |
{ |
| 1288 |
(void)pdfi_dict_put(ctx, Info, KeyNames[i], (pdf_obj *)o); |
| 1289 |
pdfi_countdown(o); |
| 1290 |
} |
| 1291 |
} |
| 1292 |
|
| 1293 |
code = pdfi_pdfmark_from_dict(ctx, Info, NULL, "DOCINFO"); |
| 1294 |
exit: |
| 1295 |
pdfi_countdown(Info); |
| 1296 |
return; |
| 1297 |
} |
| 1298 |
|
| 1299 |
/* |
| 1300 |
* Create and emit a /PAGE pdfmark for any and all of |
| 1301 |
* CropBox, TrimBox, Artbox and BleedBox. If the interpreter |
| 1302 |
* has used something other than the MediaBox as the media size, then |
| 1303 |
* we don't send these, they are almost certainly incorrect. |
| 1304 |
* |
| 1305 |
* Because we will have used the MediaBox to create the media size, and |
| 1306 |
* will have accounted for any rotation or scaling, we can use the CTM |
| 1307 |
* to adjust the various Box entries (note this routine must be called |
| 1308 |
* early!). |
| 1309 |
*/ |
| 1310 |
void pdfi_pdfmark_write_boxes(pdf_context *ctx, pdf_dict *page_dict) |
| 1311 |
{ |
| 1312 |
int i, code = 0; |
| 1313 |
pdf_dict *BoxDict = NULL; |
| 1314 |
pdf_obj *o = NULL; |
| 1315 |
gx_device *device = gs_currentdevice(ctx->pgs); |
| 1316 |
gs_matrix scale, m, ctm; |
| 1317 |
const char *BoxNames[] = { |
| 1318 |
"CropBox", "BleedBox", "TrimBox", "ArtBox" |
| 1319 |
}; |
| 1320 |
|
| 1321 |
/* If the device doesn't support pdfmark, or the device modifies the Page Size (making the other boxes unreliable) exit now */ |
| 1322 |
if (!ctx->device_state.writepdfmarks || ctx->device_state.ModifiesPageSize) |
| 1323 |
return; |
| 1324 |
|
| 1325 |
/* If we are using somethign other than the MediaBox, don't send these */ |
| 1326 |
if (ctx->args.usecropbox || ctx->args.usebleedbox || |
| 1327 |
ctx->args.usetrimbox || ctx->args.useartbox) |
| 1328 |
return; |
| 1329 |
|
| 1330 |
code = pdfi_dict_alloc(ctx, 4, &BoxDict); |
| 1331 |
if (code < 0) |
| 1332 |
goto exit; |
| 1333 |
|
| 1334 |
pdfi_countup(BoxDict); |
| 1335 |
|
| 1336 |
/* Undo the resolution scaling from the CTM, we don't want to apply that */ |
| 1337 |
scale.xx = 72.0 / device->HWResolution[0]; |
| 1338 |
scale.xy = 0; |
| 1339 |
scale.yx = 0; |
| 1340 |
scale.yy = 72.0 / device->HWResolution[1]; |
| 1341 |
scale.tx = 0; |
| 1342 |
scale.ty = 0; |
| 1343 |
|
| 1344 |
/* And multiply that by the CTM to get a matrix which represents the |
| 1345 |
* scaling/rotation used to set the conetnt to the media. |
| 1346 |
*/ |
| 1347 |
gs_currentmatrix(ctx->pgs, &ctm); |
| 1348 |
code = gs_matrix_multiply(&ctm, &scale, &m); |
| 1349 |
if (code < 0) goto exit; |
| 1350 |
|
| 1351 |
for (i=0;i<4;i++) |
| 1352 |
{ |
| 1353 |
/* Check each Bos name in turn */ |
| 1354 |
if (pdfi_dict_knownget(ctx, page_dict, BoxNames[i], &o) > 0){ |
| 1355 |
gs_rect box; |
| 1356 |
pdf_array *new_array = NULL; |
| 1357 |
|
| 1358 |
/* Box is present in page dicitonayr, check it's an array */ |
| 1359 |
if (pdfi_type_of(o) != PDF_ARRAY) { |
| 1360 |
pdfi_countdown(o); |
| 1361 |
continue; |
| 1362 |
} |
| 1363 |
|
| 1364 |
/* Turn the contents into a gs_rect */ |
| 1365 |
code = pdfi_array_to_gs_rect(ctx, (pdf_array *)o, &box); |
| 1366 |
pdfi_countdown(o); |
| 1367 |
if (code < 0) |
| 1368 |
continue; |
| 1369 |
|
| 1370 |
/* Rectangles in PDF need not be llx,lly,urx,ury, they can be any |
| 1371 |
* two opposite corners. Turn that into the usual format. |
| 1372 |
*/ |
| 1373 |
pdfi_normalize_rect(ctx, &box); |
| 1374 |
|
| 1375 |
/* Transform the resulting box by the calculated matrix */ |
| 1376 |
pdfi_bbox_transform(ctx, &box, &m); |
| 1377 |
|
| 1378 |
/* Get a new array created from the box values */ |
| 1379 |
code = pdfi_gs_rect_to_array(ctx, &box, &new_array); |
| 1380 |
if (code < 0) |
| 1381 |
continue; |
| 1382 |
|
| 1383 |
/* And store it in the working dictionary */ |
| 1384 |
(void)pdfi_dict_put(ctx, BoxDict, BoxNames[i], (pdf_obj *)new_array); |
| 1385 |
pdfi_countdown(new_array); |
| 1386 |
} |
| 1387 |
} |
| 1388 |
|
| 1389 |
/* Send all the Box entries to the device */ |
| 1390 |
(void)pdfi_pdfmark_from_dict(ctx, BoxDict, NULL, "PAGE"); |
| 1391 |
|
| 1392 |
exit: |
| 1393 |
pdfi_countdown(BoxDict); |
| 1394 |
return; |
| 1395 |
} |