| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2009-2010 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 |
const char *pdf_obj_typenames[PDF_OBJ_TYPE_MAX + 1] = { |
| 25 |
"font", "outline", "dest", "struct dest", "obj", "xform", "ximage", "thread", |
| 26 |
"pagestream", "page", "pages", "catalog", "info", "link", "annot", "annots", |
| 27 |
"bead", "beads", "objstm", "others" |
| 28 |
}; |
| 29 |
|
| 30 |
int pdf_last_annot; |
| 31 |
int pdf_last_link; |
| 32 |
int pdf_last_obj; |
| 33 |
int pdf_retval; |
| 34 |
|
| 35 |
/*tex |
| 36 |
|
| 37 |
AVL sort entry into |avl_table[]|. |
| 38 |
*/ |
| 39 |
|
| 40 |
static int compare_info(const void *pa, const void *pb, void *param) |
| 41 |
{ |
| 42 |
const oentry *a = (const oentry *) pa; |
| 43 |
const oentry *b = (const oentry *) pb; |
| 44 |
(void) param; |
| 45 |
if (a->u_type == b->u_type) { |
| 46 |
if (a->u_type == union_type_int) { |
| 47 |
return ((a->u.int0 < b->u.int0 ? -1 : (a->u.int0 > b->u.int0 ? 1 : 0))); |
| 48 |
} else { |
| 49 |
/*tex String type: */ |
| 50 |
return strcmp(a->u.str0, b->u.str0); |
| 51 |
} |
| 52 |
} else if (a->u_type == union_type_int) { |
| 53 |
return -1; |
| 54 |
} else { |
| 55 |
return 1; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
static void avl_put_obj(PDF pdf, int t, oentry * oe) |
| 60 |
{ |
| 61 |
void **pp; |
| 62 |
if (pdf->obj_tree[t] == NULL) { |
| 63 |
pdf->obj_tree[t] = avl_create(compare_info, NULL, &avl_xallocator); |
| 64 |
if (pdf->obj_tree[t] == NULL) |
| 65 |
formatted_error("pdf backend","avl_create() pdf->obj_tree failed"); |
| 66 |
} |
| 67 |
pp = avl_probe(pdf->obj_tree[t], oe); |
| 68 |
if (pp == NULL) |
| 69 |
formatted_error("pdf backend","avl_probe() out of memory in insertion"); |
| 70 |
} |
| 71 |
|
| 72 |
static void avl_put_int_obj(PDF pdf, int int0, int objptr, int t) |
| 73 |
{ |
| 74 |
oentry *oe = xtalloc(1, oentry); |
| 75 |
oe->u.int0 = int0; |
| 76 |
oe->u_type = union_type_int; |
| 77 |
oe->objptr = objptr; |
| 78 |
avl_put_obj(pdf, t, oe); |
| 79 |
} |
| 80 |
|
| 81 |
static void avl_put_str_obj(PDF pdf, char *str0, int objptr, int t) |
| 82 |
{ |
| 83 |
oentry *oe = xtalloc(1, oentry); |
| 84 |
/*tex No |xstrdup| here! */ |
| 85 |
oe->u.str0 = str0; |
| 86 |
oe->u_type = union_type_cstring; |
| 87 |
oe->objptr = objptr; |
| 88 |
avl_put_obj(pdf, t, oe); |
| 89 |
} |
| 90 |
|
| 91 |
static int avl_find_int_obj(PDF pdf, int t, int i) |
| 92 |
{ |
| 93 |
oentry *p; |
| 94 |
oentry tmp; |
| 95 |
tmp.u.int0 = i; |
| 96 |
tmp.u_type = union_type_int; |
| 97 |
if (pdf->obj_tree[t] == NULL) |
| 98 |
return 0; |
| 99 |
p = (oentry *) avl_find(pdf->obj_tree[t], &tmp); |
| 100 |
if (p == NULL) |
| 101 |
return 0; |
| 102 |
return p->objptr; |
| 103 |
} |
| 104 |
|
| 105 |
static int avl_find_str_obj(PDF pdf, int t, char *s) |
| 106 |
{ |
| 107 |
oentry *p; |
| 108 |
oentry tmp; |
| 109 |
tmp.u.str0 = s; |
| 110 |
tmp.u_type = union_type_cstring; |
| 111 |
if (pdf->obj_tree[t] == NULL) |
| 112 |
return 0; |
| 113 |
p = (oentry *) avl_find(pdf->obj_tree[t], &tmp); |
| 114 |
if (p == NULL) |
| 115 |
return 0; |
| 116 |
return p->objptr; |
| 117 |
} |
| 118 |
|
| 119 |
/*tex Create an object with type |t| and identifier |i|: */ |
| 120 |
|
| 121 |
int pdf_create_obj(PDF pdf, int t, int i) |
| 122 |
{ |
| 123 |
int a; |
| 124 |
char *ss = NULL; |
| 125 |
if (pdf->obj_ptr == sup_obj_tab_size) |
| 126 |
overflow("indirect objects table size", (unsigned) pdf->obj_tab_size); |
| 127 |
if (pdf->obj_ptr == pdf->obj_tab_size) { |
| 128 |
a = pdf->obj_tab_size / 5; |
| 129 |
if (pdf->obj_tab_size < sup_obj_tab_size - a) |
| 130 |
pdf->obj_tab_size = pdf->obj_tab_size + a; |
| 131 |
else |
| 132 |
pdf->obj_tab_size = sup_obj_tab_size; |
| 133 |
pdf->obj_tab = xreallocarray(pdf->obj_tab, obj_entry, (unsigned) pdf->obj_tab_size); |
| 134 |
} |
| 135 |
pdf->obj_ptr++; |
| 136 |
obj_info(pdf, pdf->obj_ptr) = i; |
| 137 |
obj_type(pdf, pdf->obj_ptr) = t; |
| 138 |
set_obj_fresh(pdf, pdf->obj_ptr); |
| 139 |
obj_aux(pdf, pdf->obj_ptr) = 0; |
| 140 |
if (i < 0) { |
| 141 |
ss = makecstring(-i); |
| 142 |
avl_put_str_obj(pdf, ss, pdf->obj_ptr, t); |
| 143 |
} else if (i > 0) |
| 144 |
avl_put_int_obj(pdf, i, pdf->obj_ptr, t); |
| 145 |
if (t <= HEAD_TAB_MAX) { |
| 146 |
obj_link(pdf, pdf->obj_ptr) = pdf->head_tab[t]; |
| 147 |
pdf->head_tab[t] = pdf->obj_ptr; |
| 148 |
if ((t == obj_type_dest) && (i < 0)) |
| 149 |
append_dest_name(pdf, makecstring(-obj_info(pdf, pdf->obj_ptr)), pdf->obj_ptr); /* why not just -i */ |
| 150 |
} |
| 151 |
return pdf->obj_ptr; |
| 152 |
} |
| 153 |
|
| 154 |
int find_obj(PDF pdf, int t, int i, boolean byname) |
| 155 |
{ |
| 156 |
char *ss = NULL; |
| 157 |
int ret; |
| 158 |
if (byname) { |
| 159 |
ss = makecstring(i); |
| 160 |
ret = avl_find_str_obj(pdf, t, ss); |
| 161 |
free(ss); |
| 162 |
} else { |
| 163 |
ret = avl_find_int_obj(pdf, t, i); |
| 164 |
} |
| 165 |
return ret; |
| 166 |
} |
| 167 |
|
| 168 |
/*tex |
| 169 |
|
| 170 |
The following function finds an object with identifier |i| and type |t|. |
| 171 |
Identifier |i| is either an integer or a token list index. If no such object |
| 172 |
exists then it will be created. This function is used mainly to find |
| 173 |
destination for link annotations and outlines; however it is also used in |
| 174 |
|ship_out| (to check whether a Page object already exists) so we need to |
| 175 |
declare it together with subroutines needed in |hlist_out| and |vlist_out|. |
| 176 |
|
| 177 |
*/ |
| 178 |
|
| 179 |
int pdf_get_obj(PDF pdf, int t, int i, boolean byname) |
| 180 |
{ |
| 181 |
int r; |
| 182 |
str_number s; |
| 183 |
if (byname > 0) { |
| 184 |
s = tokens_to_string(i); |
| 185 |
r = find_obj(pdf, t, s, true); |
| 186 |
} else { |
| 187 |
s = 0; |
| 188 |
r = find_obj(pdf, t, i, false); |
| 189 |
} |
| 190 |
if (r == 0) { |
| 191 |
if (byname > 0) { |
| 192 |
r = pdf_create_obj(pdf, t, -s); |
| 193 |
s = 0; |
| 194 |
} else { |
| 195 |
r = pdf_create_obj(pdf, t, i); |
| 196 |
} |
| 197 |
if (t == obj_type_dest) |
| 198 |
set_obj_dest_ptr(pdf, r, null); |
| 199 |
} |
| 200 |
if (s != 0) |
| 201 |
flush_str(s); |
| 202 |
return r; |
| 203 |
} |
| 204 |
|
| 205 |
/*tex Some object checking: */ |
| 206 |
|
| 207 |
void check_obj_exists(PDF pdf, int objnum) |
| 208 |
{ |
| 209 |
if (objnum < 0 || objnum > pdf->obj_ptr) |
| 210 |
normal_error("pdf backend", "cannot find referenced object"); |
| 211 |
} |
| 212 |
|
| 213 |
void check_obj_type(PDF pdf, int t, int objnum) |
| 214 |
{ |
| 215 |
int u; |
| 216 |
check_obj_exists(pdf, objnum); |
| 217 |
u = obj_type(pdf, objnum); |
| 218 |
if (t != u) { |
| 219 |
formatted_error("pdf backend", "referenced object has wrong type %s; should be %s", |
| 220 |
pdf_obj_typenames[u], pdf_obj_typenames[t]); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/* |
| 225 |
The next helper is needed for multiline TRT links. Several variants were tried but in the end |
| 226 |
this one is the most reliable: we calculate the width till we meet an endlink or the end of |
| 227 |
the line. When we have an inline one, for now we just use the old method and keep the running |
| 228 |
property (that way we're compatible). For practical purposes this calculation resides in the |
| 229 |
file that handles the list. By moving the code here instead of dealing with it in lists we |
| 230 |
handle all rectangles but we'll see where that fails in which case we will have a parameter |
| 231 |
passed to |set_rect_dimens| and have control per feature, maybe even under parameter control. |
| 232 |
*/ |
| 233 |
|
| 234 |
halfword calculate_width_to_endlink(halfword p, halfword this_box, halfword *leftskip, halfword *rightskip); |
| 235 |
|
| 236 |
void set_rect_dimens(PDF pdf, halfword p, halfword parent_box, scaledpos cur, scaled_whd alt_rule) |
| 237 |
{ |
| 238 |
/*tex |
| 239 |
The positions relative to cur: |pdf| contains current point on page): |
| 240 |
*/ |
| 241 |
scaledpos ll, ur, pos_ll, pos_ur; |
| 242 |
posstructure localpos; |
| 243 |
localpos.dir = pdf->posstruct->dir; |
| 244 |
|
| 245 |
halfword lcrap = 0; |
| 246 |
halfword rcrap = 0; |
| 247 |
halfword lskip = 0; |
| 248 |
halfword rskip = 0; |
| 249 |
|
| 250 |
if (pdf_linking) { |
| 251 |
/* begin of experiment */ |
| 252 |
if (subtype(p) == pdf_link_data_node) { |
| 253 |
alt_rule.wd = calculate_width_to_endlink(list_ptr(parent_box), parent_box, &lskip, &rskip); |
| 254 |
if (pdf->link_stack[pdf->link_stack_ptr].direction == dir_TRT) { |
| 255 |
alt_rule.wd = -alt_rule.wd; |
| 256 |
localpos.dir = dir_TRT; |
| 257 |
lcrap = lskip; |
| 258 |
} else { |
| 259 |
lcrap = -lskip; |
| 260 |
} |
| 261 |
} else if (subtype(p) == pdf_start_link_node) { |
| 262 |
alt_rule.wd = calculate_width_to_endlink(p, parent_box, &lskip, &rskip); |
| 263 |
} |
| 264 |
/* end of experiment */ |
| 265 |
} |
| 266 |
|
| 267 |
ll.h = - lcrap - rcrap; |
| 268 |
if (is_running(alt_rule.wd)) |
| 269 |
ur.h = width(parent_box) - cur.h; |
| 270 |
else |
| 271 |
ur.h = alt_rule.wd - lcrap - rcrap; |
| 272 |
if (is_running(alt_rule.dp)) |
| 273 |
ll.v = depth(parent_box) - cur.v; |
| 274 |
else |
| 275 |
ll.v = alt_rule.dp; |
| 276 |
if (is_running(alt_rule.ht)) |
| 277 |
ur.v = -height(parent_box) - cur.v; |
| 278 |
else |
| 279 |
ur.v = -alt_rule.ht; |
| 280 |
synch_pos_with_cur(&localpos, pdf->posstruct, ll); |
| 281 |
pos_ll = localpos.pos; |
| 282 |
synch_pos_with_cur(&localpos, pdf->posstruct, ur); |
| 283 |
pos_ur = localpos.pos; |
| 284 |
/* |
| 285 |
The test for swapping has been moved to the moment we write the rectangle. This has to |
| 286 |
do with the fact that we have code dealing with directions and that the |pdf_ann_*| |
| 287 |
horizontal edges get recalculated in some places. |
| 288 |
*/ |
| 289 |
if (global_shipping_mode == SHIPPING_PAGE && matrixused()) { |
| 290 |
matrixtransformrect(pos_ll.h, pos_ll.v, pos_ur.h, pos_ur.v); |
| 291 |
pos_ll.h = getllx(); |
| 292 |
pos_ll.v = getlly(); |
| 293 |
pos_ur.h = geturx(); |
| 294 |
pos_ur.v = getury(); |
| 295 |
} |
| 296 |
pdf_ann_left(p) = pos_ll.h; |
| 297 |
pdf_ann_bottom(p) = pos_ll.v; |
| 298 |
pdf_ann_right(p) = pos_ur.h; |
| 299 |
pdf_ann_top(p) = pos_ur.v; |
| 300 |
} |
| 301 |
|
| 302 |
void libpdffinish(PDF pdf) |
| 303 |
{ |
| 304 |
strbuf_free(pdf->fb); |
| 305 |
xfree(pdf->job_id_string); |
| 306 |
fm_free(); |
| 307 |
t1_free(); |
| 308 |
enc_free(); |
| 309 |
epdf_free(); |
| 310 |
ttf_free(); |
| 311 |
glyph_unicode_free(); |
| 312 |
zip_free(pdf); |
| 313 |
} |