1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
/* Copyright (C) 2018-2025 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
CA 94129, USA, for further information.
*/
/* array handling for the PDF interpreter */
#include "ghostpdf.h"
#include "pdf_types.h"
#include "pdf_stack.h"
#include "pdf_deref.h"
#include "pdf_array.h"
#include "pdf_loop_detect.h"
/* NOTE: I think this should take a pdf_context param, but it's not available where it's
* called, would require some surgery.
*/
void pdfi_free_array(pdf_obj *o)
{
pdf_array *a = (pdf_array *)o;
int i;
for (i=0;i < a->size;i++) {
if (a->values[i] != NULL)
pdfi_countdown(a->values[i]);
}
gs_free_object(OBJ_MEMORY(a), a->values, "pdf interpreter free array contents");
gs_free_object(OBJ_MEMORY(a), a, "pdf interpreter free array");
}
int pdfi_array_alloc(pdf_context *ctx, uint64_t size, pdf_array **a)
{
int code, i;
*a = NULL;
code = pdfi_object_alloc(ctx, PDF_ARRAY, size, (pdf_obj **)a);
if (code < 0)
return code;
(*a)->size = size;
if (size > 0) {
/* Start all the array entries pointing to null.
* array_put will replace tehm. This ensures we always have a valid
* object for every entry. pdfi_array_from_stack() doesn't do this
* initialisation because we know how many obejcts there are in the array
* and we have valid objects for each entry on the stack already created.
*/
for (i=0;i<size;i++){
(*a)->values[i] = PDF_NULL_OBJ;
}
}
return 0;
}
/* This was defined in pdf_int.c until we moved the equivalent pdfi_dict_from_stack() into
* pdf_dict.c, because we needed to be able to create dictionaries for images. We don't have
* that need, but its less confusing to have the array_from_stack function defined in
* here, similarly to the dictionary routine.
*/
int pdfi_array_from_stack(pdf_context *ctx, uint32_t indirect_num, uint32_t indirect_gen)
{
uint64_t index = 0;
pdf_array *a = NULL;
pdf_obj *o;
int code;
code = pdfi_count_to_mark(ctx, &index);
if (code < 0)
return code;
code = pdfi_array_alloc(ctx, index, &a);
if (code < 0)
return code;
while (index) {
o = ctx->stack_top[-1];
code = pdfi_array_put(ctx, a, --index, o);
if (code < 0) {
(void)pdfi_clear_to_mark(ctx);
return code;
}
pdfi_pop(ctx, 1);
}
code = pdfi_clear_to_mark(ctx);
if (code < 0)
return code;
if (ctx->args.pdfdebug)
outprintf (ctx->memory, " ]\n");
a->indirect_num = indirect_num;
a->indirect_gen = indirect_gen;
code = pdfi_push(ctx, (pdf_obj *)a);
if (code < 0)
pdfi_free_array((pdf_obj *)a);
return code;
}
int pdfi_array_fetch_recursing(pdf_context *ctx, pdf_array *a, uint64_t index, pdf_obj **o, bool setref, bool cache)
{
int code;
pdf_obj *obj;
*o = NULL;
if (pdfi_type_of(a) != PDF_ARRAY)
return_error(gs_error_typecheck);
if (index >= a->size)
return_error(gs_error_rangecheck);
obj = a->values[index];
if (pdfi_type_of(obj) == PDF_INDIRECT) {
pdf_obj *o1 = NULL;
pdf_indirect_ref *r = (pdf_indirect_ref *)obj;
if (r->ref_object_num == a->object_num)
return_error(gs_error_circular_reference);
if (cache)
code = pdfi_deref_loop_detect(ctx, r->ref_object_num, r->ref_generation_num, &o1);
else
code = pdfi_deref_loop_detect_nocache(ctx, r->ref_object_num, r->ref_generation_num, &o1);
if (code < 0)
return code;
if (setref)
(void)pdfi_array_put(ctx, a, index, o1);
obj = o1;
} else {
if (ctx->loop_detection != NULL && (uintptr_t)obj > TOKEN__LAST_KEY && obj->object_num != 0)
if (pdfi_loop_detector_check_object(ctx, obj->object_num))
return gs_note_error(gs_error_circular_reference);
pdfi_countup(obj);
}
*o = obj;
return 0;
}
/* Fetch object from array, resolving indirect reference if needed
* setref -- indicates whether to replace indirect ref with the object
*/
int pdfi_array_fetch(pdf_context *ctx, pdf_array *a, uint64_t index, pdf_obj **o, bool setref, bool cache)
{
int code;
pdf_obj *obj;
*o = NULL;
if (pdfi_type_of(a) != PDF_ARRAY)
return_error(gs_error_typecheck);
if (index >= a->size)
return_error(gs_error_rangecheck);
obj = a->values[index];
if (pdfi_type_of(obj) == PDF_INDIRECT) {
pdf_obj *o1 = NULL;
pdf_indirect_ref *r = (pdf_indirect_ref *)obj;
if (r->ref_object_num == a->object_num)
return_error(gs_error_circular_reference);
if (cache)
code = pdfi_deref_loop_detect(ctx, r->ref_object_num, r->ref_generation_num, &o1);
else
code = pdfi_deref_loop_detect_nocache(ctx, r->ref_object_num, r->ref_generation_num, &o1);
if (code < 0)
return code;
if (setref)
(void)pdfi_array_put(ctx, a, index, o1);
obj = o1;
} else {
pdfi_countup(obj);
}
*o = obj;
return 0;
}
/* Get element from array without resolving PDF_INDIRECT dereferences.
* It looks to me like some usages need to do the checking themselves to
* avoid circular references? Can remove this if not really needed.
*/
int pdfi_array_get_no_deref(pdf_context *ctx, pdf_array *a, uint64_t index, pdf_obj **o)
{
if (pdfi_type_of(a) != PDF_ARRAY)
return_error(gs_error_typecheck);
if (index >= a->size)
return_error(gs_error_rangecheck);
*o = a->values[index];
pdfi_countup(*o);
return 0;
}
/* Same as pdfi_array_get() but doesn't replace indirect ref with a new object.
*/
int pdfi_array_get_no_store_R(pdf_context *ctx, pdf_array *a, uint64_t index, pdf_obj **o)
{
int code;
code = pdfi_array_fetch(ctx, a, index, o, false, false);
if (code < 0) return code;
return 0;
}
/* Get value from pdfi_array.
* Handles type-checking and resolving indirect references.
*/
int pdfi_array_get_type(pdf_context *ctx, pdf_array *a, uint64_t index,
pdf_obj_type type, pdf_obj **o)
{
int code;
code = pdfi_array_get(ctx, a, index, o);
if (code < 0)
return code;
if (pdfi_type_of(*o) != type) {
pdfi_countdown(*o);
*o = NULL;
return_error(gs_error_typecheck);
}
return 0;
}
int pdfi_array_get_int(pdf_context *ctx, pdf_array *a, uint64_t index, int64_t *i)
{
int code;
pdf_obj *n;
code = pdfi_array_get(ctx, a, index, &n);
if (code < 0)
return code;
code = pdfi_obj_to_int(ctx, n, i);
pdfi_countdown(n);
return code;
}
int pdfi_array_get_number(pdf_context *ctx, pdf_array *a, uint64_t index, double *d)
{
int code;
pdf_obj *n;
code = pdfi_array_get(ctx, a, index, &n);
if (code < 0)
return code;
code = pdfi_obj_to_real(ctx, n, d);
pdfi_countdown(n);
return code;
}
/* Check whether a particular object is in an array.
* If index is not NULL, fill it in with the index of the object.
* Note that this will resolve indirect references if needed.
*/
bool pdfi_array_known(pdf_context *ctx, pdf_array *a, pdf_obj *o, int *index)
{
int i;
if (pdfi_type_of(a) != PDF_ARRAY)
return_error(gs_error_typecheck);
for (i=0; i < a->size; i++) {
pdf_obj *val;
int code;
code = pdfi_array_fetch(ctx, a, i, &val, true, true);
if (code < 0)
continue;
if (pdf_object_num(val) == pdf_object_num(o)) {
if (index != NULL) *index = i;
pdfi_countdown(val);
return true;
}
pdfi_countdown(val);
}
return false;
}
int pdfi_array_put(pdf_context *ctx, pdf_array *a, uint64_t index, pdf_obj *o)
{
if (pdfi_type_of(a) != PDF_ARRAY)
return_error(gs_error_typecheck);
if (index >= a->size)
return_error(gs_error_rangecheck);
pdfi_countdown(a->values[index]);
a->values[index] = o;
pdfi_countup(o);
return 0;
}
int pdfi_array_put_int(pdf_context *ctx, pdf_array *a, uint64_t index, int64_t val)
{
int code;
pdf_num *obj;
if (pdfi_type_of(a) != PDF_ARRAY)
return_error(gs_error_typecheck);
code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&obj);
if (code < 0)
return code;
obj->value.i = val;
return pdfi_array_put(ctx, a, index, (pdf_obj *)obj);
}
int pdfi_array_put_real(pdf_context *ctx, pdf_array *a, uint64_t index, double val)
{
int code;
pdf_num *obj;
if (pdfi_type_of(a) != PDF_ARRAY)
return_error(gs_error_typecheck);
code = pdfi_object_alloc(ctx, PDF_REAL, 0, (pdf_obj **)&obj);
if (code < 0)
return code;
obj->value.d = val;
return pdfi_array_put(ctx, a, index, (pdf_obj *)obj);
}
/* Strictly speaking the normalize_rect isn't really part of the PDF array
* processing, but its very likely that any time we want to use it, the
* rectangle will have come from a PDF array in a PDF file so it makes
* sense to have it here.
*/
/* Normalize rectangle */
void pdfi_normalize_rect(pdf_context *ctx, gs_rect *rect)
{
double temp;
/* Normalize the rectangle */
if (rect->p.x > rect->q.x) {
temp = rect->p.x;
rect->p.x = rect->q.x;
rect->q.x = temp;
}
if (rect->p.y > rect->q.y) {
temp = rect->p.y;
rect->p.y = rect->q.y;
rect->q.y = temp;
}
}
/*
* Turn an Array into a gs_rect. If Array is NULL, makes a tiny rect
*/
int pdfi_array_to_gs_rect(pdf_context *ctx, pdf_array *array, gs_rect *rect)
{
double number;
int code = 0;
/* Init to tiny rect to allow sane continuation on errors */
rect->p.x = 0.0;
rect->p.y = 0.0;
rect->q.x = 1.0;
rect->q.y = 1.0;
/* Identity matrix if no array */
if (array == NULL || pdfi_type_of(array) != PDF_ARRAY) {
return 0;
}
if (pdfi_array_size(array) != 4) {
return_error(gs_error_rangecheck);
}
code = pdfi_array_get_number(ctx, array, 0, &number);
if (code < 0) goto errorExit;
rect->p.x = (float)number;
code = pdfi_array_get_number(ctx, array, 1, &number);
if (code < 0) goto errorExit;
rect->p.y = (float)number;
code = pdfi_array_get_number(ctx, array, 2, &number);
if (code < 0) goto errorExit;
rect->q.x = (float)number;
code = pdfi_array_get_number(ctx, array, 3, &number);
if (code < 0) goto errorExit;
rect->q.y = (float)number;
return 0;
errorExit:
return code;
}
/* Create a new PDF array object with 4 entires, and store the values from a
* gs_rect to it.
*/
int pdfi_gs_rect_to_array(pdf_context *ctx, gs_rect *rect, pdf_array **new_array)
{
pdf_num *num = NULL;
int code = 0;
code = pdfi_array_alloc(ctx, 4, new_array);
if (code < 0)
return code;
pdfi_countup(*new_array);
code = pdfi_num_alloc(ctx, rect->p.x, &num);
if (code < 0)
goto error;
code = pdfi_array_put(ctx, *new_array, 0, (pdf_obj *)num);
if (code < 0)
goto error;
code = pdfi_num_alloc(ctx, rect->p.y, &num);
if (code < 0)
goto error;
code = pdfi_array_put(ctx, *new_array, 1, (pdf_obj *)num);
if (code < 0)
goto error;
code = pdfi_num_alloc(ctx, rect->q.x, &num);
if (code < 0)
goto error;
code = pdfi_array_put(ctx, *new_array, 2, (pdf_obj *)num);
if (code < 0)
goto error;
code = pdfi_num_alloc(ctx, rect->q.y, &num);
if (code < 0)
goto error;
code = pdfi_array_put(ctx, *new_array, 3, (pdf_obj *)num);
if (code < 0)
goto error;
return 0;
error:
pdfi_countdown(new_array);
return code;
}
/* Turn a /Matrix Array into a gs_matrix. If Array is NULL, makes an identity matrix */
int pdfi_array_to_gs_matrix(pdf_context *ctx, pdf_array *array, gs_matrix *mat)
{
double number;
int code = 0;
/* Init to identity matrix to allow sane continuation on errors */
mat->xx = 1.0;
mat->xy = 0.0;
mat->yx = 0.0;
mat->yy = 1.0;
mat->tx = 0.0;
mat->ty = 0.0;
/* Identity matrix if no array */
if (array == NULL || pdfi_type_of(array) != PDF_ARRAY) {
return 0;
}
if (pdfi_array_size(array) != 6) {
return_error(gs_error_rangecheck);
}
code = pdfi_array_get_number(ctx, array, 0, &number);
if (code < 0) goto errorExit;
mat->xx = (float)number;
code = pdfi_array_get_number(ctx, array, 1, &number);
if (code < 0) goto errorExit;
mat->xy = (float)number;
code = pdfi_array_get_number(ctx, array, 2, &number);
if (code < 0) goto errorExit;
mat->yx = (float)number;
code = pdfi_array_get_number(ctx, array, 3, &number);
if (code < 0) goto errorExit;
mat->yy = (float)number;
code = pdfi_array_get_number(ctx, array, 4, &number);
if (code < 0) goto errorExit;
mat->tx = (float)number;
code = pdfi_array_get_number(ctx, array, 5, &number);
if (code < 0) goto errorExit;
mat->ty = (float)number;
return 0;
errorExit:
return code;
}
/* Turn a pdf_array into a double array of specified size */
int pdfi_array_to_num_array(pdf_context *ctx, pdf_array *array, double *out, int offset, int size)
{
int i;
int code;
double num;
for (i=0; i<size; i++) {
code = pdfi_array_get_number(ctx, array, offset+i, &num);
if (code < 0)
return code;
out[i] = num;
}
return 0;
}
/* Transform a BBox by a matrix (from zmatrix.c/zbbox_transform())*/
void
pdfi_bbox_transform(pdf_context *ctx, gs_rect *bbox, gs_matrix *matrix)
{
gs_point aa, az, za, zz;
double temp;
gs_point_transform(bbox->p.x, bbox->p.y, matrix, &aa);
gs_point_transform(bbox->p.x, bbox->q.y, matrix, &az);
gs_point_transform(bbox->q.x, bbox->p.y, matrix, &za);
gs_point_transform(bbox->q.x, bbox->q.y, matrix, &zz);
if ( aa.x > az.x)
temp = aa.x, aa.x = az.x, az.x = temp;
if ( za.x > zz.x)
temp = za.x, za.x = zz.x, zz.x = temp;
if ( za.x < aa.x)
aa.x = za.x; /* min */
if ( az.x > zz.x)
zz.x = az.x; /* max */
if ( aa.y > az.y)
temp = aa.y, aa.y = az.y, az.y = temp;
if ( za.y > zz.y)
temp = za.y, za.y = zz.y, zz.y = temp;
if ( za.y < aa.y)
aa.y = za.y; /* min */
if ( az.y > zz.y)
zz.y = az.y; /* max */
bbox->p.x = aa.x;
bbox->p.y = aa.y;
bbox->q.x = zz.x;
bbox->q.y = zz.y;
}
|