| 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 |
/* Miscellaneous routines */ |
| 17 |
|
| 18 |
#include "pdf_int.h" |
| 19 |
#include "pdf_stack.h" |
| 20 |
#include "pdf_misc.h" |
| 21 |
#include "pdf_font_types.h" |
| 22 |
#include "pdf_gstate.h" |
| 23 |
#include "gspath.h" /* For gs_strokepath() */ |
| 24 |
#include "gspaint.h" /* For gs_erasepage() */ |
| 25 |
#include "gsicc_manage.h" /* For gsicc_get_default_type() */ |
| 26 |
#include "gsstate.h" /* for gs_setrenderingintent() */ |
| 27 |
|
| 28 |
/* Get current bbox, possibly from stroking current path (utility function) */ |
| 29 |
int pdfi_get_current_bbox(pdf_context *ctx, gs_rect *bbox, bool stroked) |
| 30 |
{ |
| 31 |
int code, code1; |
| 32 |
|
| 33 |
if (stroked) { |
| 34 |
code = pdfi_gsave(ctx); |
| 35 |
if (code < 0) |
| 36 |
return code; |
| 37 |
code = gs_strokepath(ctx->pgs); |
| 38 |
if (code < 0) |
| 39 |
goto exit; |
| 40 |
} |
| 41 |
code = gs_upathbbox(ctx->pgs, bbox, false); |
| 42 |
|
| 43 |
exit: |
| 44 |
if (stroked) { |
| 45 |
code1 = pdfi_grestore(ctx); |
| 46 |
if (code == 0) |
| 47 |
code = code1; |
| 48 |
} |
| 49 |
return code; |
| 50 |
} |
| 51 |
|
| 52 |
/* Get the current color space (the base one) from a color space |
| 53 |
*/ |
| 54 |
gs_color_space_index pdfi_get_color_space_index(pdf_context *ctx, const gs_color_space *pcs) |
| 55 |
{ |
| 56 |
gs_color_space_index csi; |
| 57 |
|
| 58 |
/* Get the color space index */ |
| 59 |
csi = gs_color_space_get_index(pcs); |
| 60 |
|
| 61 |
/* If its an Indexed space, then use the base space */ |
| 62 |
if (csi == gs_color_space_index_Indexed) |
| 63 |
csi = gs_color_space_get_index(pcs->base_space); |
| 64 |
|
| 65 |
/* If its ICC, see if its a substitution for one of the device |
| 66 |
* spaces. If so then we will want to behave as if we were using the |
| 67 |
* device space. |
| 68 |
*/ |
| 69 |
if (csi == gs_color_space_index_ICC && pcs->cmm_icc_profile_data) |
| 70 |
csi = gsicc_get_default_type(pcs->cmm_icc_profile_data); |
| 71 |
|
| 72 |
return csi; |
| 73 |
} |
| 74 |
|
| 75 |
/* Get the current color space (the base one) from current graphics state. |
| 76 |
* index -- tells whether to pull from 0 or 1 (probably 0) |
| 77 |
*/ |
| 78 |
gs_color_space_index pdfi_currentcolorspace(pdf_context *ctx, int index) |
| 79 |
{ |
| 80 |
const gs_color_space *pcs; |
| 81 |
|
| 82 |
pcs = ctx->pgs->color[index].color_space; |
| 83 |
|
| 84 |
return pdfi_get_color_space_index(ctx, pcs); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
int |
| 89 |
pdfi_name_strcmp(const pdf_name *n, const char *s) |
| 90 |
{ |
| 91 |
int len = strlen(s); |
| 92 |
if (n->length == len) |
| 93 |
return memcmp(n->data, s, len); |
| 94 |
return -1; |
| 95 |
} |
| 96 |
|
| 97 |
bool |
| 98 |
pdfi_string_is(const pdf_string *n, const char *s) |
| 99 |
{ |
| 100 |
int len = strlen(s); |
| 101 |
if (n->length == len) |
| 102 |
return (memcmp(n->data, s, len) == 0); |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
bool |
| 107 |
pdfi_name_is(const pdf_name *n, const char *s) |
| 108 |
{ |
| 109 |
int len = strlen(s); |
| 110 |
if (n->length == len) |
| 111 |
return (memcmp(n->data, s, len) == 0); |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
int |
| 116 |
pdfi_name_cmp(const pdf_name *n1, const pdf_name *n2) |
| 117 |
{ |
| 118 |
if (n1->length != n2->length) |
| 119 |
return -1; |
| 120 |
return memcmp(n1->data, n2->data, n1->length); |
| 121 |
} |
| 122 |
|
| 123 |
int |
| 124 |
pdfi_string_cmp(const pdf_string *n1, const pdf_string *n2) |
| 125 |
{ |
| 126 |
if (n1->length != n2->length) |
| 127 |
return -1; |
| 128 |
return memcmp(n1->data, n2->data, n1->length); |
| 129 |
} |
| 130 |
|
| 131 |
/* Set rendering intent, translating from name to number */ |
| 132 |
int pdfi_setrenderingintent(pdf_context *ctx, pdf_name *n) |
| 133 |
{ |
| 134 |
int code = 0; |
| 135 |
|
| 136 |
if (pdfi_name_is(n, "Perceptual")) { |
| 137 |
code = gs_setrenderingintent(ctx->pgs, 0); |
| 138 |
} else if (pdfi_name_is(n, "Saturation")) { |
| 139 |
code = gs_setrenderingintent(ctx->pgs, 2); |
| 140 |
} else if (pdfi_name_is(n, "RelativeColorimetric")) { |
| 141 |
code = gs_setrenderingintent(ctx->pgs, 1); |
| 142 |
} else if (pdfi_name_is(n, "AbsoluteColorimetric")) { |
| 143 |
code = gs_setrenderingintent(ctx->pgs, 3); |
| 144 |
} else { |
| 145 |
/* PDF 1.7 Reference, bottom of page 260 if a PDF uses an unknown rendering intent |
| 146 |
* then use RelativeColoimetric. But flag a warning. |
| 147 |
*/ |
| 148 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_RENDERINGINTENT, "pdfi_setrenderingintent", ""); |
| 149 |
code = gs_setrenderingintent(ctx->pgs, 1); |
| 150 |
} |
| 151 |
return code; |
| 152 |
} |
| 153 |
|
| 154 |
int pdfi_string_from_name(pdf_context *ctx, pdf_name *n, char **str, int *len) |
| 155 |
{ |
| 156 |
if (pdfi_type_of(n) != PDF_NAME) |
| 157 |
return gs_note_error(gs_error_typecheck); |
| 158 |
|
| 159 |
*str = NULL; |
| 160 |
*len = 0; |
| 161 |
|
| 162 |
*str = (char *)gs_alloc_bytes(ctx->memory, (size_t)n->length + 1, "pdfi_string_from_name"); |
| 163 |
if (*str == NULL) |
| 164 |
return gs_note_error(gs_error_VMerror); |
| 165 |
|
| 166 |
memcpy(*str, n->data, n->length); |
| 167 |
(*str)[n->length] = 0x00; |
| 168 |
*len = n->length; |
| 169 |
|
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
int pdfi_free_string_from_name(pdf_context *ctx, char *str) |
| 174 |
{ |
| 175 |
if (str != NULL) |
| 176 |
gs_free_object(ctx->memory, str, "pdfi_free_string_from_name"); |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
void normalize_rectangle(double *d) |
| 181 |
{ |
| 182 |
double d1[4]; |
| 183 |
int i; |
| 184 |
|
| 185 |
if (d[0] < d[2]) { |
| 186 |
d1[0] = d[0]; |
| 187 |
d1[2] = d[2]; |
| 188 |
} else { |
| 189 |
d1[0] = d[2]; |
| 190 |
d1[2] = d[0]; |
| 191 |
} |
| 192 |
if (d[1] < d[3]) { |
| 193 |
d1[1] = d[1]; |
| 194 |
d1[3] = d[3]; |
| 195 |
} else { |
| 196 |
d1[1] = d[3]; |
| 197 |
d1[3] = d[1]; |
| 198 |
} |
| 199 |
for (i=0;i<=3;i++){ |
| 200 |
d[i] = d1[i]; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/* Free an array of cstrings, sets the pointer to null */ |
| 205 |
void pdfi_free_cstring_array(pdf_context *ctx, char ***pstrlist) |
| 206 |
{ |
| 207 |
char **ptr = *pstrlist; |
| 208 |
|
| 209 |
if (ptr == NULL) |
| 210 |
return; |
| 211 |
|
| 212 |
while (*ptr) { |
| 213 |
gs_free_object(ctx->memory, *ptr, "pdfi_free_cstring_array(item)"); |
| 214 |
ptr ++; |
| 215 |
} |
| 216 |
gs_free_object(ctx->memory, *pstrlist, "pdfi_free_cstring_array(array)"); |
| 217 |
*pstrlist = NULL; |
| 218 |
} |
| 219 |
|
| 220 |
/* Parse an argument string of names into an array of cstrings */ |
| 221 |
/* Format: /Item1,/Item2,/Item3 (no white space) */ |
| 222 |
int pdfi_parse_name_cstring_array(pdf_context *ctx, char *data, uint64_t size, char ***pstrlist) |
| 223 |
{ |
| 224 |
char **strlist = NULL; |
| 225 |
char **templist = NULL; |
| 226 |
int numitems = 0, item; |
| 227 |
int strnum; |
| 228 |
uint64_t i; |
| 229 |
char *strptr; |
| 230 |
int code = 0; |
| 231 |
|
| 232 |
/* Free it if it already exists */ |
| 233 |
if (*pstrlist != NULL) |
| 234 |
pdfi_free_cstring_array(ctx, pstrlist); |
| 235 |
|
| 236 |
/* find out how many '/' characters there are -- this is the max possible number |
| 237 |
* of items in the list |
| 238 |
*/ |
| 239 |
for (i=0, strptr = data; i<size; i++,strptr++) { |
| 240 |
if (*strptr == '/') |
| 241 |
numitems ++; |
| 242 |
/* early exit if we hit a null */ |
| 243 |
if (*strptr == 0) |
| 244 |
break; |
| 245 |
} |
| 246 |
|
| 247 |
/* Allocate space for the array of char * (plus one extra for null termination) */ |
| 248 |
strlist = (char **)gs_alloc_bytes(ctx->memory, ((size_t)numitems+1)*sizeof(char *), |
| 249 |
"pdfi_parse_cstring_array(strlist)"); |
| 250 |
if (strlist == NULL) |
| 251 |
return_error(gs_error_VMerror); |
| 252 |
|
| 253 |
memset(strlist, 0, (numitems+1)*sizeof(char *)); |
| 254 |
|
| 255 |
/* Allocate a temp array */ |
| 256 |
templist = (char **)gs_alloc_bytes(ctx->memory, ((size_t)numitems+1)*sizeof(char *), |
| 257 |
"pdfi_parse_cstring_array(templist)"); |
| 258 |
if (templist == NULL) { |
| 259 |
code = gs_note_error(gs_error_VMerror); |
| 260 |
goto exit; |
| 261 |
} |
| 262 |
|
| 263 |
memset(templist, 0, (numitems+1)*sizeof(char *)); |
| 264 |
|
| 265 |
/* Find start ptr of each string */ |
| 266 |
item = 0; |
| 267 |
for (i=0, strptr = data; i<size; i++,strptr++) { |
| 268 |
if (*strptr == '/') { |
| 269 |
templist[item] = strptr+1; |
| 270 |
item++; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/* Find each substring, alloc, copy into string array */ |
| 275 |
strnum = 0; |
| 276 |
for (i=0; i<numitems; i++) { |
| 277 |
char *curstr, *nextstr; |
| 278 |
int length; |
| 279 |
char *newstr; |
| 280 |
|
| 281 |
curstr = templist[i]; |
| 282 |
nextstr = templist[i+1]; |
| 283 |
if (!curstr) |
| 284 |
break; |
| 285 |
if (*curstr == '/' || *curstr == ',') { |
| 286 |
/* Empty string, skip it */ |
| 287 |
continue; |
| 288 |
} |
| 289 |
if (nextstr == NULL) { |
| 290 |
length = size-(curstr-data); |
| 291 |
} else { |
| 292 |
length = nextstr - curstr - 1; |
| 293 |
} |
| 294 |
if (curstr[length-1] == ',') |
| 295 |
length --; |
| 296 |
|
| 297 |
/* Allocate the string and copy it */ |
| 298 |
newstr = (char *)gs_alloc_bytes(ctx->memory, (size_t)length+1, |
| 299 |
"pdfi_parse_cstring_array(newstr)"); |
| 300 |
if (newstr == NULL) { |
| 301 |
code = gs_note_error(gs_error_VMerror); |
| 302 |
goto exit; |
| 303 |
} |
| 304 |
memcpy(newstr, curstr, length); |
| 305 |
newstr[length+1] = 0; /* Null terminate */ |
| 306 |
strlist[strnum] = newstr; |
| 307 |
strnum ++; |
| 308 |
} |
| 309 |
|
| 310 |
*pstrlist = strlist; |
| 311 |
|
| 312 |
exit: |
| 313 |
if (code < 0) |
| 314 |
pdfi_free_cstring_array(ctx, &strlist); |
| 315 |
if (templist) |
| 316 |
gs_free_object(ctx->memory, templist, "pdfi_parse_cstring_array(templist(array))"); |
| 317 |
return code; |
| 318 |
} |