| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2009-2011 Taco Hoekwater <[email protected]> |
| 4 |
|
| 5 |
This file is part of LuaTeX. |
| 6 |
|
| 7 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 2 of the License, or (at your |
| 10 |
option) any later version. |
| 11 |
|
| 12 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 |
License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License along |
| 18 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
*/ |
| 21 |
|
| 22 |
#include "ptexlib.h" |
| 23 |
|
| 24 |
/*tex |
| 25 |
|
| 26 |
Here we implement subroutines for work with objects and related things. Some |
| 27 |
of them are used in former parts too, so we need to declare them forward. |
| 28 |
Memory will grow dynamically. |
| 29 |
|
| 30 |
*/ |
| 31 |
|
| 32 |
void init_dest_names(PDF pdf) |
| 33 |
{ |
| 34 |
pdf->dest_names_size = inf_dest_names_size; |
| 35 |
pdf->dest_names = xmallocarray(dest_name_entry, inf_dest_names_size); |
| 36 |
} |
| 37 |
|
| 38 |
void append_dest_name(PDF pdf, char *s, int n) |
| 39 |
{ |
| 40 |
int a; |
| 41 |
if (pdf->dest_names_ptr == sup_dest_names_size) |
| 42 |
overflow("number of destination names (dest_names_size)",(unsigned) pdf->dest_names_size); |
| 43 |
if (pdf->dest_names_ptr == pdf->dest_names_size) { |
| 44 |
a = pdf->dest_names_size / 5; |
| 45 |
if (pdf->dest_names_size < sup_dest_names_size - a) |
| 46 |
pdf->dest_names_size = pdf->dest_names_size + a; |
| 47 |
else |
| 48 |
pdf->dest_names_size = sup_dest_names_size; |
| 49 |
pdf->dest_names = xreallocarray(pdf->dest_names, dest_name_entry, (unsigned) pdf->dest_names_size); |
| 50 |
} |
| 51 |
pdf->dest_names[pdf->dest_names_ptr].objname = xstrdup(s); |
| 52 |
pdf->dest_names[pdf->dest_names_ptr].objnum = n; |
| 53 |
pdf->dest_names_ptr++; |
| 54 |
} |
| 55 |
|
| 56 |
/*tex |
| 57 |
|
| 58 |
When a destination is created we need to check whether another destination |
| 59 |
with the same identifier already exists and give a warning if needed. |
| 60 |
|
| 61 |
*/ |
| 62 |
|
| 63 |
static void warn_dest_dup(int id, small_number byname) |
| 64 |
{ |
| 65 |
if (byname > 0) { |
| 66 |
char *ss = tokenlist_to_cstring(id, true, NULL); |
| 67 |
formatted_warning("pdf backend", "ignoring duplicate destination with the name '%s'",ss); |
| 68 |
} else { |
| 69 |
formatted_warning("pdf backend", "ignoring duplicate destination with the num '%d'",id); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/*tex |
| 74 |
|
| 75 |
In |do_dest|, structure destinations and regular destinations use separate object |
| 76 |
types for |pdf_get_obj| but share an object type for |addto_page_resources|. |
| 77 |
This allows |write_out_pdf_mark_destinations| to handle both kinds of destinations |
| 78 |
in a uniform way, while still allowing to use the same name for both kinds of |
| 79 |
destinations. This is a useful feature since it's rather common to have both kinds |
| 80 |
of destinations for the same object and therefore they will often have the same name. |
| 81 |
|
| 82 |
*/ |
| 83 |
void do_dest(PDF pdf, halfword p, halfword parent_box, scaledpos cur) |
| 84 |
{ |
| 85 |
scaledpos pos = pdf->posstruct->pos; |
| 86 |
scaled_whd alt_rule; |
| 87 |
int k; |
| 88 |
if (global_shipping_mode == SHIPPING_FORM) |
| 89 |
normal_error("pdf backend", "destinations cannot be inside an xform"); |
| 90 |
if (doing_leaders) |
| 91 |
return; |
| 92 |
k = pdf_get_obj(pdf, |
| 93 |
pdf_dest_objnum(p) == null ? obj_type_dest : obj_type_struct_dest, |
| 94 |
pdf_dest_id(p), pdf_dest_named_id(p)); |
| 95 |
if (obj_dest_ptr(pdf, k) != null) { |
| 96 |
warn_dest_dup(pdf_dest_id(p), (small_number) pdf_dest_named_id(p)); |
| 97 |
return; |
| 98 |
} |
| 99 |
obj_dest_ptr(pdf, k) = p; |
| 100 |
addto_page_resources(pdf, obj_type_dest, k); |
| 101 |
alt_rule.wd = width(p); |
| 102 |
alt_rule.ht = height(p); |
| 103 |
alt_rule.dp = depth(p); |
| 104 |
/*tex |
| 105 |
The different branches for matrixused is somewhat strange and should |
| 106 |
always be used |
| 107 |
*/ |
| 108 |
pdf_ann_margin(p) = pdf_dest_margin; |
| 109 |
switch (pdf_dest_type(p)) { |
| 110 |
case pdf_dest_xyz: |
| 111 |
if (matrixused()) |
| 112 |
set_rect_dimens(pdf, p, parent_box, cur, alt_rule); |
| 113 |
else { |
| 114 |
pdf_ann_left(p) = pos.h; |
| 115 |
pdf_ann_top(p) = pos.v; |
| 116 |
} |
| 117 |
break; |
| 118 |
case pdf_dest_fith: |
| 119 |
case pdf_dest_fitbh: |
| 120 |
if (matrixused()) |
| 121 |
set_rect_dimens(pdf, p, parent_box, cur, alt_rule); |
| 122 |
else |
| 123 |
pdf_ann_top(p) = pos.v; |
| 124 |
break; |
| 125 |
case pdf_dest_fitv: |
| 126 |
case pdf_dest_fitbv: |
| 127 |
if (matrixused()) |
| 128 |
set_rect_dimens(pdf, p, parent_box, cur, alt_rule); |
| 129 |
else |
| 130 |
pdf_ann_left(p) = pos.h; |
| 131 |
break; |
| 132 |
case pdf_dest_fit: |
| 133 |
case pdf_dest_fitb: |
| 134 |
break; |
| 135 |
case pdf_dest_fitr: |
| 136 |
set_rect_dimens(pdf, p, parent_box, cur, alt_rule); |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void write_out_pdf_mark_destinations(PDF pdf) |
| 142 |
{ |
| 143 |
pdf_object_list *k; |
| 144 |
if ((k = get_page_resources_list(pdf, obj_type_dest)) != NULL) { |
| 145 |
while (k != NULL) { |
| 146 |
if (is_obj_written(pdf, k->info)) { |
| 147 |
normal_error("pdf backend","destination has been already written (this shouldn't happen)"); |
| 148 |
} else { |
| 149 |
int i; |
| 150 |
int objnum; |
| 151 |
i = obj_dest_ptr(pdf, k->info); |
| 152 |
objnum = pdf_dest_objnum(i); |
| 153 |
pdf_begin_obj(pdf, k->info, OBJSTM_ALWAYS); |
| 154 |
if (pdf_dest_named_id(i) > 0 && objnum == null) { |
| 155 |
pdf_begin_dict(pdf); |
| 156 |
pdf_add_name(pdf, "D"); |
| 157 |
} |
| 158 |
pdf_begin_array(pdf); |
| 159 |
pdf_add_ref(pdf, objnum == null ? pdf->last_page : objnum); |
| 160 |
switch (pdf_dest_type(i)) { |
| 161 |
case pdf_dest_xyz: |
| 162 |
pdf_add_name(pdf, "XYZ"); |
| 163 |
pdf_add_bp(pdf, pdf_ann_left(i)); |
| 164 |
pdf_add_bp(pdf, pdf_ann_top(i)); |
| 165 |
if (pdf_dest_xyz_zoom(i) == null) { |
| 166 |
pdf_add_null(pdf); |
| 167 |
} else { |
| 168 |
pdf_check_space(pdf); |
| 169 |
pdf_print_int(pdf, pdf_dest_xyz_zoom(i) / 1000); |
| 170 |
pdf_out(pdf, '.'); |
| 171 |
pdf_print_int(pdf, (pdf_dest_xyz_zoom(i) % 1000)); |
| 172 |
pdf_set_space(pdf); |
| 173 |
} |
| 174 |
break; |
| 175 |
case pdf_dest_fit: |
| 176 |
pdf_add_name(pdf, "Fit"); |
| 177 |
break; |
| 178 |
case pdf_dest_fith: |
| 179 |
pdf_add_name(pdf, "FitH"); |
| 180 |
pdf_add_bp(pdf, pdf_ann_top(i)); |
| 181 |
break; |
| 182 |
case pdf_dest_fitv: |
| 183 |
pdf_add_name(pdf, "FitV"); |
| 184 |
pdf_add_bp(pdf, pdf_ann_left(i)); |
| 185 |
break; |
| 186 |
case pdf_dest_fitb: |
| 187 |
pdf_add_name(pdf, "FitB"); |
| 188 |
break; |
| 189 |
case pdf_dest_fitbh: |
| 190 |
pdf_add_name(pdf, "FitBH"); |
| 191 |
pdf_add_bp(pdf, pdf_ann_top(i)); |
| 192 |
break; |
| 193 |
case pdf_dest_fitbv: |
| 194 |
pdf_add_name(pdf, "FitBV"); |
| 195 |
pdf_add_bp(pdf, pdf_ann_left(i)); |
| 196 |
break; |
| 197 |
case pdf_dest_fitr: |
| 198 |
pdf_add_name(pdf, "FitR"); |
| 199 |
pdf_add_rect_spec(pdf, i); |
| 200 |
break; |
| 201 |
default: |
| 202 |
normal_error("pdf backend", "unknown dest type"); |
| 203 |
break; |
| 204 |
} |
| 205 |
pdf_end_array(pdf); |
| 206 |
if (pdf_dest_named_id(i) > 0 && objnum == null) |
| 207 |
pdf_end_dict(pdf); |
| 208 |
pdf_end_obj(pdf); |
| 209 |
} |
| 210 |
k = k->link; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
void scan_pdfdest(PDF pdf) |
| 216 |
{ |
| 217 |
halfword q; |
| 218 |
int k; |
| 219 |
int obj_type; |
| 220 |
str_number i; |
| 221 |
scaled_whd alt_rule; |
| 222 |
q = cur_list.tail_field; |
| 223 |
new_whatsit(pdf_dest_node); |
| 224 |
if (scan_keyword("struct")) { |
| 225 |
scan_int(); |
| 226 |
if (cur_val <= 0) |
| 227 |
normal_error("pdf backend", "struct identifier must be positive"); |
| 228 |
pdf_dest_objnum(cur_list.tail_field) = cur_val; |
| 229 |
obj_type = obj_type_struct_dest; |
| 230 |
} else { |
| 231 |
pdf_dest_objnum(cur_list.tail_field) = null; |
| 232 |
obj_type = obj_type_dest; |
| 233 |
} |
| 234 |
if (scan_keyword("num")) { |
| 235 |
scan_int(); |
| 236 |
if (cur_val <= 0) |
| 237 |
normal_error("pdf backend", "num identifier must be positive"); |
| 238 |
if (cur_val > max_halfword) |
| 239 |
normal_error("pdf backend", "number too big"); |
| 240 |
set_pdf_dest_id(cur_list.tail_field, cur_val); |
| 241 |
set_pdf_dest_named_id(cur_list.tail_field, 0); |
| 242 |
} else if (scan_keyword("name")) { |
| 243 |
scan_toks(false, true); |
| 244 |
set_pdf_dest_id(cur_list.tail_field, def_ref); |
| 245 |
set_pdf_dest_named_id(cur_list.tail_field, 1); |
| 246 |
} else { |
| 247 |
normal_error("pdf backend", "identifier type missing"); |
| 248 |
} |
| 249 |
if (scan_keyword("xyz")) { |
| 250 |
set_pdf_dest_type(cur_list.tail_field, pdf_dest_xyz); |
| 251 |
if (scan_keyword("zoom")) { |
| 252 |
scan_int(); |
| 253 |
if (cur_val > max_halfword) |
| 254 |
normal_error("pdf backend", "number too big"); |
| 255 |
set_pdf_dest_xyz_zoom(cur_list.tail_field, cur_val); |
| 256 |
} else { |
| 257 |
set_pdf_dest_xyz_zoom(cur_list.tail_field, null); |
| 258 |
} |
| 259 |
} else if (scan_keyword("fitbh")) { |
| 260 |
set_pdf_dest_type(cur_list.tail_field, pdf_dest_fitbh); |
| 261 |
} else if (scan_keyword("fitbv")) { |
| 262 |
set_pdf_dest_type(cur_list.tail_field, pdf_dest_fitbv); |
| 263 |
} else if (scan_keyword("fitb")) { |
| 264 |
set_pdf_dest_type(cur_list.tail_field, pdf_dest_fitb); |
| 265 |
} else if (scan_keyword("fith")) { |
| 266 |
set_pdf_dest_type(cur_list.tail_field, pdf_dest_fith); |
| 267 |
} else if (scan_keyword("fitv")) { |
| 268 |
set_pdf_dest_type(cur_list.tail_field, pdf_dest_fitv); |
| 269 |
} else if (scan_keyword("fitr")) { |
| 270 |
set_pdf_dest_type(cur_list.tail_field, pdf_dest_fitr); |
| 271 |
} else if (scan_keyword("fit")) { |
| 272 |
set_pdf_dest_type(cur_list.tail_field, pdf_dest_fit); |
| 273 |
} else { |
| 274 |
normal_error("pdf backend", "destination type missing"); |
| 275 |
} |
| 276 |
/*tex Scan an optional space. */ |
| 277 |
get_x_token(); |
| 278 |
if (cur_cmd != spacer_cmd) |
| 279 |
back_input(); |
| 280 |
if (pdf_dest_type(cur_list.tail_field) == pdf_dest_fitr) { |
| 281 |
alt_rule = scan_alt_rule(); |
| 282 |
set_width(cur_list.tail_field, alt_rule.wd); |
| 283 |
set_height(cur_list.tail_field, alt_rule.ht); |
| 284 |
set_depth(cur_list.tail_field, alt_rule.dp); |
| 285 |
} |
| 286 |
if (pdf_dest_named_id(cur_list.tail_field) != 0) { |
| 287 |
i = tokens_to_string(pdf_dest_id(cur_list.tail_field)); |
| 288 |
k = find_obj(pdf, obj_type, i, true); |
| 289 |
flush_str(i); |
| 290 |
} else { |
| 291 |
k = find_obj(pdf, obj_type, pdf_dest_id(cur_list.tail_field), false); |
| 292 |
} |
| 293 |
if ((k != 0) && (obj_dest_ptr(pdf, k) != null)) { |
| 294 |
warn_dest_dup(pdf_dest_id(cur_list.tail_field),(small_number) pdf_dest_named_id(cur_list.tail_field)); |
| 295 |
flush_node_list(cur_list.tail_field); |
| 296 |
cur_list.tail_field = q; |
| 297 |
vlink(q) = null; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/*tex |
| 302 |
Sort |dest_names| by names: we have to consider the lexical meaning of |
| 303 |
the literal strings, as well as the hexadecimal strings as byte stream. |
| 304 |
*/ |
| 305 |
|
| 306 |
static int unescapebuf(char *buf,const char *src, size_t *newsize){ |
| 307 |
const char *pch = src; |
| 308 |
unsigned char bs = 0; |
| 309 |
int bal = 0; |
| 310 |
|
| 311 |
*newsize=0; |
| 312 |
while (*pch!='\0'){ |
| 313 |
switch (*pch) { |
| 314 |
case '\\': |
| 315 |
if (bs==0){ |
| 316 |
bs=1; |
| 317 |
}else if (bs>=1){ |
| 318 |
/* seen '\\' i.e. a backslash */ |
| 319 |
*buf++ = '\\'; |
| 320 |
(*newsize)++; |
| 321 |
bs=0; |
| 322 |
} |
| 323 |
break; |
| 324 |
case '\r': |
| 325 |
if(bs==1){/* drop '\\','\r' */ pch++;if(*pch!='\n'){ pch--;}; /* drop '\\', '\n' */ bs=0;} else { pch++; if(*pch!='\n'){*buf++='\n';(*newsize)++;}; pch--;/* next cycle will eventually manage \n */} |
| 326 |
break; |
| 327 |
case '\n': |
| 328 |
if(bs==1){/* drop '\\','\n' */ bs=0;} else { *buf++ = '\n';(*newsize)++;} |
| 329 |
break; |
| 330 |
case 'n': |
| 331 |
if(bs==1){ *buf++ = '\n'; bs=0;} else { *buf++ = 'n';} |
| 332 |
(*newsize)++; |
| 333 |
break; |
| 334 |
case 'r': |
| 335 |
if(bs==1){ *buf++ = '\r'; bs=0;} else { *buf++ = 'r';} |
| 336 |
(*newsize)++; |
| 337 |
break; |
| 338 |
case 't': |
| 339 |
if(bs==1){ *buf++ = '\t'; bs=0;} else { *buf++ = 't';} |
| 340 |
(*newsize)++; |
| 341 |
break; |
| 342 |
case 'b': |
| 343 |
if(bs==1){ *buf++ = '\b'; bs=0;} else { *buf++ = 'b';} |
| 344 |
(*newsize)++; |
| 345 |
break; |
| 346 |
case 'f': |
| 347 |
if(bs==1){ *buf++ = '\f'; bs=0;} else { *buf++ = 'f';} |
| 348 |
(*newsize)++; |
| 349 |
break; |
| 350 |
case '(': case ')': |
| 351 |
if(bs==0){if( *pch=='(' ) bal++; else bal--;}; /* we keep track of unescaped '(' or ')' because they can be unbalanced. */ |
| 352 |
bs=0; |
| 353 |
*buf++ = *pch; |
| 354 |
(*newsize)++; |
| 355 |
break; |
| 356 |
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': |
| 357 |
if(bs==1){ |
| 358 |
/* seen \o */ |
| 359 |
char last_seen[]={'\0','\0','\0'}; |
| 360 |
last_seen[0]=*pch-'0'; |
| 361 |
pch++; |
| 362 |
if ('0'<=*pch && *pch<='7'){ |
| 363 |
/* seen \oo */ |
| 364 |
last_seen[1]=*pch-'0'; |
| 365 |
pch++; |
| 366 |
if ('0'<=*pch && *pch<='7'){ |
| 367 |
/* seen \ooo */ |
| 368 |
last_seen[2] = last_seen[0]*64+last_seen[1]*8+(*pch-'0'); |
| 369 |
*buf++ = last_seen[2]; |
| 370 |
(*newsize)++; |
| 371 |
} else { |
| 372 |
/* seen \ooO */ |
| 373 |
pch--; /* next cycle will manage O */ |
| 374 |
last_seen[2] = last_seen[0]*8+last_seen[1] ; |
| 375 |
*buf++ = last_seen[2]; |
| 376 |
(*newsize)++; |
| 377 |
} |
| 378 |
}else { |
| 379 |
/*seen \oO */ |
| 380 |
pch--; /* next cycle will manage O */ |
| 381 |
*buf++ = last_seen[0]; |
| 382 |
(*newsize)++; |
| 383 |
} |
| 384 |
bs=0; |
| 385 |
}else { |
| 386 |
*buf++ = *pch; |
| 387 |
(*newsize)++; |
| 388 |
} |
| 389 |
break; |
| 390 |
default: |
| 391 |
bs=0; /* skip isolated backslash */ |
| 392 |
*buf++ = *pch; |
| 393 |
(*newsize)++; |
| 394 |
break; |
| 395 |
} |
| 396 |
pch++; |
| 397 |
} |
| 398 |
return bal; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
static int dest_cmp(const void *a, const void *b) |
| 403 |
{ |
| 404 |
dest_name_entry aa = *(const dest_name_entry *) a; |
| 405 |
dest_name_entry bb = *(const dest_name_entry *) b; |
| 406 |
|
| 407 |
size_t la=strlen(aa.objname), lb=strlen(bb.objname); |
| 408 |
|
| 409 |
boolean aa_is_hexstring=false, bb_is_hexstring=false; |
| 410 |
boolean aa_is_empty=false, bb_is_empty=false; |
| 411 |
boolean free_cmp_aa=false,free_cmp_bb=false; |
| 412 |
|
| 413 |
char *cmp_aa = NULL; |
| 414 |
char *cmp_bb = NULL; |
| 415 |
size_t cmp_aa_size,cmp_bb_size; |
| 416 |
int res; |
| 417 |
|
| 418 |
aa_is_empty = (la==0) || ( la==2? (aa.objname[0] == '<') && (aa.objname[1] == '>'): false); |
| 419 |
bb_is_empty = (lb==0) || ( lb==2? (bb.objname[0] == '<') && (bb.objname[1] == '>'): false); |
| 420 |
|
| 421 |
|
| 422 |
if (aa_is_empty && bb_is_empty) { |
| 423 |
formatted_warning("pdf backend", "both entries are empty"); |
| 424 |
return 0; |
| 425 |
} |
| 426 |
|
| 427 |
if (aa_is_empty && !(bb_is_empty)) |
| 428 |
return -1; |
| 429 |
|
| 430 |
if (!(aa_is_empty) && bb_is_empty) |
| 431 |
return 1; |
| 432 |
|
| 433 |
|
| 434 |
if ((aa.objname[0] == '<') && (aa.objname[la-1] == '>') && !(odd(la))) { |
| 435 |
/* perhaps a hexadecimal string */ |
| 436 |
cmp_aa_size = (la/2)-1 ; |
| 437 |
cmp_aa = malloc(cmp_aa_size); |
| 438 |
if (cmp_aa) { |
| 439 |
aa_is_hexstring = true; |
| 440 |
free_cmp_aa = true; |
| 441 |
size_t j; |
| 442 |
for (j=1;j<(la-1);j+=2){ |
| 443 |
unsigned char c1=aa.objname[j]; |
| 444 |
unsigned char c0=aa.objname[j+1]; |
| 445 |
if( isxdigit(c1) && isxdigit(c0) ){ |
| 446 |
if (isdigit(c1)) c1=c1-'0'; else c1=(tolower(c1)-'a')+10; |
| 447 |
if (isdigit(c0)) c0=c0-'0'; else c0=(tolower(c0)-'a')+10; |
| 448 |
cmp_aa[(j-1)/2]=c1*16+c0; |
| 449 |
}else { |
| 450 |
aa_is_hexstring = false; |
| 451 |
free_cmp_aa = false; |
| 452 |
cmp_aa_size = 0; |
| 453 |
xfree(cmp_aa); |
| 454 |
break; |
| 455 |
} |
| 456 |
} |
| 457 |
} else { |
| 458 |
/* something went wrong with malloc */ |
| 459 |
free_cmp_aa = false; |
| 460 |
cmp_aa_size = 0; |
| 461 |
cmp_aa = NULL; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
if ( !(aa_is_hexstring) && (strchr(aa.objname,'\\')!=NULL || strchr(aa.objname,'\n')!=NULL || strchr(aa.objname,'\r')!=NULL)){ |
| 466 |
size_t l = la; /* strlen(aa.objname) */ |
| 467 |
int res1 = 0; |
| 468 |
cmp_aa=malloc(l); |
| 469 |
if (cmp_aa) { |
| 470 |
free_cmp_aa = true; |
| 471 |
memset(cmp_aa,'\0',l); |
| 472 |
res1 = unescapebuf(cmp_aa,aa.objname,&cmp_aa_size); |
| 473 |
if (res1) |
| 474 |
formatted_warning("pdf backend", "unbalanced () in (%s)",aa.objname); |
| 475 |
if (0<cmp_aa_size && cmp_aa_size<l){ |
| 476 |
cmp_aa = realloc(cmp_aa, cmp_aa_size); |
| 477 |
} |
| 478 |
} else { |
| 479 |
/* something went wrong with malloc */ |
| 480 |
free_cmp_aa = false; |
| 481 |
cmp_aa_size = 0; |
| 482 |
cmp_aa = NULL; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
if ((bb.objname[0] == '<') && (bb.objname[lb-1] == '>') && !(odd(lb))) { |
| 488 |
/* perhaps a hexadecimal string */ |
| 489 |
cmp_bb_size =(lb/2)-1 ; |
| 490 |
cmp_bb = malloc(cmp_bb_size); |
| 491 |
if (cmp_bb) { |
| 492 |
size_t j; |
| 493 |
for (j=1;j<(lb-1);j+=2){ |
| 494 |
unsigned char c1=bb.objname[j]; |
| 495 |
unsigned char c0=bb.objname[j+1]; |
| 496 |
if( isxdigit(c1) && isxdigit(c0) ){ |
| 497 |
if (isdigit(c1)) c1=c1-'0'; else c1=(tolower(c1)-'a')+10; |
| 498 |
if (isdigit(c0)) c0=c0-'0'; else c0=(tolower(c0)-'a')+10; |
| 499 |
cmp_bb[(j-1)/2]=c1*16+c0; |
| 500 |
}else { |
| 501 |
bb_is_hexstring = false; |
| 502 |
free_cmp_bb = false; |
| 503 |
cmp_bb_size = 0; |
| 504 |
xfree(cmp_bb); |
| 505 |
break; |
| 506 |
} |
| 507 |
} |
| 508 |
} else { |
| 509 |
/* something went wrong with malloc */ |
| 510 |
free_cmp_bb = false; |
| 511 |
cmp_bb_size = 0; |
| 512 |
cmp_bb = NULL; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
if ( !(bb_is_hexstring) && (strchr(bb.objname,'\\')!=NULL || strchr(bb.objname,'\n')!=NULL || strchr(bb.objname,'\r')!=NULL)){ |
| 517 |
size_t l = lb; /* strlen(bb.objname) */ |
| 518 |
int res1 = 0; |
| 519 |
cmp_bb=malloc(l); |
| 520 |
if (cmp_bb) { |
| 521 |
free_cmp_bb = true; |
| 522 |
memset(cmp_bb,'\0',l); |
| 523 |
res1 = unescapebuf(cmp_bb,bb.objname,&cmp_bb_size); |
| 524 |
if (res1) |
| 525 |
formatted_warning("pdf backend", "unbalanced () in (%s)",bb.objname); |
| 526 |
if (0<cmp_bb_size && cmp_bb_size<l){ |
| 527 |
cmp_bb = realloc(cmp_bb, cmp_bb_size); |
| 528 |
} |
| 529 |
} else { |
| 530 |
/* something went wrong with malloc */ |
| 531 |
free_cmp_bb = false; |
| 532 |
cmp_bb_size = 0; |
| 533 |
cmp_bb = NULL; |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
if (cmp_aa==NULL && cmp_bb==NULL){ |
| 538 |
res = strcmp(aa.objname, bb.objname); |
| 539 |
if (res==0) |
| 540 |
formatted_warning("pdf backend", "duplicate entry %s",aa.objname); |
| 541 |
|
| 542 |
} |
| 543 |
if (cmp_aa == NULL) { |
| 544 |
cmp_aa_size= la; |
| 545 |
cmp_aa = aa.objname; |
| 546 |
} |
| 547 |
if (cmp_bb == NULL) { |
| 548 |
cmp_bb_size= lb; |
| 549 |
cmp_bb = bb.objname; |
| 550 |
} |
| 551 |
|
| 552 |
res = memcmp(cmp_aa,cmp_bb, cmp_aa_size<cmp_bb_size? cmp_aa_size : cmp_bb_size); |
| 553 |
if (res==0) { |
| 554 |
if (cmp_aa_size==cmp_bb_size) |
| 555 |
formatted_warning("pdf backend", "entries %s and %s are equivalent to %s",aa.objname,bb.objname,cmp_aa); |
| 556 |
else { |
| 557 |
if (cmp_aa_size<cmp_bb_size) |
| 558 |
res=-1; |
| 559 |
else |
| 560 |
res=1; |
| 561 |
} |
| 562 |
} |
| 563 |
if (free_cmp_aa) |
| 564 |
free(cmp_aa); |
| 565 |
if (free_cmp_bb) |
| 566 |
free(cmp_bb); |
| 567 |
return res; |
| 568 |
} |
| 569 |
|
| 570 |
void sort_dest_names(PDF pdf) |
| 571 |
{ |
| 572 |
qsort(pdf->dest_names, (size_t) pdf->dest_names_ptr, sizeof(dest_name_entry), dest_cmp); |
| 573 |
} |
| 574 |
|
| 575 |
/*tex |
| 576 |
|
| 577 |
Output the name tree. The tree nature of the destination list forces the |
| 578 |
storing of intermediate data in |obj_info| and |obj_aux| fields, which is |
| 579 |
further uglified by the fact that |obj_tab| entries do not accept char |
| 580 |
pointers. |
| 581 |
|
| 582 |
*/ |
| 583 |
|
| 584 |
int output_name_tree(PDF pdf) |
| 585 |
{ |
| 586 |
/*tex A flag for name tree output: is it |/Names| or |/Kids|: */ |
| 587 |
boolean is_names = true; |
| 588 |
/*tex |
| 589 |
The index of current child of |l|; if |k < pdf_dest_names_ptr| then this |
| 590 |
is pointer to |dest_names| array; otherwise it is the pointer to |
| 591 |
|obj_tab| (object number). |
| 592 |
*/ |
| 593 |
int k = 0; |
| 594 |
int b = 0; |
| 595 |
int m, j, l; |
| 596 |
int dests = 0; |
| 597 |
int names_head = 0; |
| 598 |
int names_tail = 0; |
| 599 |
if (pdf->dest_names_ptr == 0) { |
| 600 |
goto DONE; |
| 601 |
} |
| 602 |
sort_dest_names(pdf); |
| 603 |
while (true) { |
| 604 |
do { |
| 605 |
/*tex Create a new node: */ |
| 606 |
l = pdf_create_obj(pdf, obj_type_others, 0); |
| 607 |
if (b == 0) { |
| 608 |
/*tex First in this level: */ |
| 609 |
b = l; |
| 610 |
} |
| 611 |
if (names_head == 0) { |
| 612 |
names_head = l; |
| 613 |
names_tail = l; |
| 614 |
} else { |
| 615 |
set_obj_link(pdf, names_tail, l); |
| 616 |
names_tail = l; |
| 617 |
} |
| 618 |
set_obj_link(pdf, names_tail, 0); |
| 619 |
/*tex Output the current node in this level. */ |
| 620 |
pdf_begin_obj(pdf, l, OBJSTM_ALWAYS); |
| 621 |
pdf_begin_dict(pdf); |
| 622 |
j = 0; |
| 623 |
if (is_names) { |
| 624 |
set_obj_start(pdf, l, pdf->dest_names[k].objname); |
| 625 |
pdf_add_name(pdf, "Names"); |
| 626 |
pdf_begin_array(pdf); |
| 627 |
do { |
| 628 |
pdf_add_string(pdf, pdf->dest_names[k].objname); |
| 629 |
pdf_add_ref(pdf, pdf->dest_names[k].objnum); |
| 630 |
j++; |
| 631 |
k++; |
| 632 |
} while (j != name_tree_kids_max && k != pdf->dest_names_ptr); |
| 633 |
pdf_end_array(pdf); |
| 634 |
/*tex For later use: */ |
| 635 |
set_obj_stop(pdf, l, pdf->dest_names[k - 1].objname); |
| 636 |
if (k == pdf->dest_names_ptr) { |
| 637 |
is_names = false; |
| 638 |
k = names_head; |
| 639 |
b = 0; |
| 640 |
} |
| 641 |
} else { |
| 642 |
set_obj_start(pdf, l, obj_start(pdf, k)); |
| 643 |
pdf_add_name(pdf, "Kids"); |
| 644 |
pdf_begin_array(pdf); |
| 645 |
do { |
| 646 |
pdf_add_ref(pdf, k); |
| 647 |
set_obj_stop(pdf, l, obj_stop(pdf, k)); |
| 648 |
k = obj_link(pdf, k); |
| 649 |
j++; |
| 650 |
} while (j != name_tree_kids_max && k != b |
| 651 |
&& obj_link(pdf, k) != 0); |
| 652 |
pdf_end_array(pdf); |
| 653 |
if (k == b) |
| 654 |
b = 0; |
| 655 |
} |
| 656 |
pdf_add_name(pdf, "Limits"); |
| 657 |
pdf_begin_array(pdf); |
| 658 |
pdf_add_string(pdf, obj_start(pdf, l)); |
| 659 |
pdf_add_string(pdf, obj_stop(pdf, l)); |
| 660 |
pdf_end_array(pdf); |
| 661 |
pdf_end_dict(pdf); |
| 662 |
pdf_end_obj(pdf); |
| 663 |
} while (b != 0); |
| 664 |
if (k == l) { |
| 665 |
dests = l; |
| 666 |
goto DONE; |
| 667 |
} |
| 668 |
} |
| 669 |
DONE: |
| 670 |
if ((dests != 0) || (pdf_names_toks != null)) { |
| 671 |
m = pdf_create_obj(pdf, obj_type_others, 0); |
| 672 |
pdf_begin_obj(pdf, m, OBJSTM_ALWAYS); |
| 673 |
pdf_begin_dict(pdf); |
| 674 |
if (dests != 0) |
| 675 |
pdf_dict_add_ref(pdf, "Dests", dests); |
| 676 |
if (pdf_names_toks != null) { |
| 677 |
pdf_print_toks(pdf, pdf_names_toks); |
| 678 |
delete_token_ref(pdf_names_toks); |
| 679 |
pdf_names_toks = null; |
| 680 |
} |
| 681 |
print_pdf_table_string(pdf, "names"); |
| 682 |
pdf_end_dict(pdf); |
| 683 |
pdf_end_obj(pdf); |
| 684 |
return m; |
| 685 |
} else { |
| 686 |
return 0; |
| 687 |
} |
| 688 |
} |