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
|
/* Copyright (C) 2019-2023 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.
*/
/* Miscellaneous routines */
#include "pdf_int.h"
#include "pdf_stack.h"
#include "pdf_misc.h"
#include "pdf_font_types.h"
#include "pdf_gstate.h"
#include "gspath.h" /* For gs_strokepath() */
#include "gspaint.h" /* For gs_erasepage() */
#include "gsicc_manage.h" /* For gsicc_get_default_type() */
#include "gsstate.h" /* for gs_setrenderingintent() */
/* Get current bbox, possibly from stroking current path (utility function) */
int pdfi_get_current_bbox(pdf_context *ctx, gs_rect *bbox, bool stroked)
{
int code, code1;
if (stroked) {
code = pdfi_gsave(ctx);
if (code < 0)
return code;
code = gs_strokepath(ctx->pgs);
if (code < 0)
goto exit;
}
code = gs_upathbbox(ctx->pgs, bbox, false);
exit:
if (stroked) {
code1 = pdfi_grestore(ctx);
if (code == 0)
code = code1;
}
return code;
}
/* Get the current color space (the base one) from a color space
*/
gs_color_space_index pdfi_get_color_space_index(pdf_context *ctx, const gs_color_space *pcs)
{
gs_color_space_index csi;
/* Get the color space index */
csi = gs_color_space_get_index(pcs);
/* If its an Indexed space, then use the base space */
if (csi == gs_color_space_index_Indexed)
csi = gs_color_space_get_index(pcs->base_space);
/* If its ICC, see if its a substitution for one of the device
* spaces. If so then we will want to behave as if we were using the
* device space.
*/
if (csi == gs_color_space_index_ICC && pcs->cmm_icc_profile_data)
csi = gsicc_get_default_type(pcs->cmm_icc_profile_data);
return csi;
}
/* Get the current color space (the base one) from current graphics state.
* index -- tells whether to pull from 0 or 1 (probably 0)
*/
gs_color_space_index pdfi_currentcolorspace(pdf_context *ctx, int index)
{
const gs_color_space *pcs;
pcs = ctx->pgs->color[index].color_space;
return pdfi_get_color_space_index(ctx, pcs);
}
int
pdfi_name_strcmp(const pdf_name *n, const char *s)
{
int len = strlen(s);
if (n->length == len)
return memcmp(n->data, s, len);
return -1;
}
bool
pdfi_string_is(const pdf_string *n, const char *s)
{
int len = strlen(s);
if (n->length == len)
return (memcmp(n->data, s, len) == 0);
return false;
}
bool
pdfi_name_is(const pdf_name *n, const char *s)
{
int len = strlen(s);
if (n->length == len)
return (memcmp(n->data, s, len) == 0);
return false;
}
int
pdfi_name_cmp(const pdf_name *n1, const pdf_name *n2)
{
if (n1->length != n2->length)
return -1;
return memcmp(n1->data, n2->data, n1->length);
}
int
pdfi_string_cmp(const pdf_string *n1, const pdf_string *n2)
{
if (n1->length != n2->length)
return -1;
return memcmp(n1->data, n2->data, n1->length);
}
/* Set rendering intent, translating from name to number */
int pdfi_setrenderingintent(pdf_context *ctx, pdf_name *n)
{
int code = 0;
if (pdfi_name_is(n, "Perceptual")) {
code = gs_setrenderingintent(ctx->pgs, 0);
} else if (pdfi_name_is(n, "Saturation")) {
code = gs_setrenderingintent(ctx->pgs, 2);
} else if (pdfi_name_is(n, "RelativeColorimetric")) {
code = gs_setrenderingintent(ctx->pgs, 1);
} else if (pdfi_name_is(n, "AbsoluteColorimetric")) {
code = gs_setrenderingintent(ctx->pgs, 3);
} else {
/* PDF 1.7 Reference, bottom of page 260 if a PDF uses an unknown rendering intent
* then use RelativeColoimetric. But flag a warning.
*/
pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_RENDERINGINTENT, "pdfi_setrenderingintent", "");
code = gs_setrenderingintent(ctx->pgs, 1);
}
return code;
}
int pdfi_string_from_name(pdf_context *ctx, pdf_name *n, char **str, int *len)
{
if (pdfi_type_of(n) != PDF_NAME)
return gs_note_error(gs_error_typecheck);
*str = NULL;
*len = 0;
*str = (char *)gs_alloc_bytes(ctx->memory, n->length + 1, "pdfi_string_from_name");
if (*str == NULL)
return gs_note_error(gs_error_VMerror);
memcpy(*str, n->data, n->length);
(*str)[n->length] = 0x00;
*len = n->length;
return 0;
}
int pdfi_free_string_from_name(pdf_context *ctx, char *str)
{
if (str != NULL)
gs_free_object(ctx->memory, str, "pdfi_free_string_from_name");
return 0;
}
void normalize_rectangle(double *d)
{
double d1[4];
int i;
if (d[0] < d[2]) {
d1[0] = d[0];
d1[2] = d[2];
} else {
d1[0] = d[2];
d1[2] = d[0];
}
if (d[1] < d[3]) {
d1[1] = d[1];
d1[3] = d[3];
} else {
d1[1] = d[3];
d1[3] = d[1];
}
for (i=0;i<=3;i++){
d[i] = d1[i];
}
}
/* Free an array of cstrings, sets the pointer to null */
void pdfi_free_cstring_array(pdf_context *ctx, char ***pstrlist)
{
char **ptr = *pstrlist;
if (ptr == NULL)
return;
while (*ptr) {
gs_free_object(ctx->memory, *ptr, "pdfi_free_cstring_array(item)");
ptr ++;
}
gs_free_object(ctx->memory, *pstrlist, "pdfi_free_cstring_array(array)");
*pstrlist = NULL;
}
/* Parse an argument string of names into an array of cstrings */
/* Format: /Item1,/Item2,/Item3 (no white space) */
int pdfi_parse_name_cstring_array(pdf_context *ctx, char *data, uint64_t size, char ***pstrlist)
{
char **strlist = NULL;
char **templist = NULL;
int numitems = 0, item;
int strnum;
uint64_t i;
char *strptr;
int code = 0;
/* Free it if it already exists */
if (*pstrlist != NULL)
pdfi_free_cstring_array(ctx, pstrlist);
/* find out how many '/' characters there are -- this is the max possible number
* of items in the list
*/
for (i=0, strptr = data; i<size; i++,strptr++) {
if (*strptr == '/')
numitems ++;
/* early exit if we hit a null */
if (*strptr == 0)
break;
}
/* Allocate space for the array of char * (plus one extra for null termination) */
strlist = (char **)gs_alloc_bytes(ctx->memory, (numitems+1)*sizeof(char *),
"pdfi_parse_cstring_array(strlist)");
if (strlist == NULL)
return_error(gs_error_VMerror);
memset(strlist, 0, (numitems+1)*sizeof(char *));
/* Allocate a temp array */
templist = (char **)gs_alloc_bytes(ctx->memory, (numitems+1)*sizeof(char *),
"pdfi_parse_cstring_array(templist)");
if (templist == NULL) {
code = gs_note_error(gs_error_VMerror);
goto exit;
}
memset(templist, 0, (numitems+1)*sizeof(char *));
/* Find start ptr of each string */
item = 0;
for (i=0, strptr = data; i<size; i++,strptr++) {
if (*strptr == '/') {
templist[item] = strptr+1;
item++;
}
}
/* Find each substring, alloc, copy into string array */
strnum = 0;
for (i=0; i<numitems; i++) {
char *curstr, *nextstr;
int length;
char *newstr;
curstr = templist[i];
nextstr = templist[i+1];
if (!curstr)
break;
if (*curstr == '/' || *curstr == ',') {
/* Empty string, skip it */
continue;
}
if (nextstr == NULL) {
length = size-(curstr-data);
} else {
length = nextstr - curstr - 1;
}
if (curstr[length-1] == ',')
length --;
/* Allocate the string and copy it */
newstr = (char *)gs_alloc_bytes(ctx->memory, length+1,
"pdfi_parse_cstring_array(newstr)");
if (newstr == NULL) {
code = gs_note_error(gs_error_VMerror);
goto exit;
}
memcpy(newstr, curstr, length);
newstr[length+1] = 0; /* Null terminate */
strlist[strnum] = newstr;
strnum ++;
}
*pstrlist = strlist;
exit:
if (code < 0)
pdfi_free_cstring_array(ctx, &strlist);
if (templist)
gs_free_object(ctx->memory, templist, "pdfi_parse_cstring_array(templist(array))");
return code;
}
|