| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2009-2012 Taco Hoekwater <taco@@luatex.org> |
| 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 |
To implement nested link annotations, we need a stack to hold copy of |
| 27 |
|pdf_start_link_node|'s that are being written out, together with their box |
| 28 |
nesting level. |
| 29 |
|
| 30 |
*/ |
| 31 |
|
| 32 |
void pdf_out_link_state(PDF pdf, halfword p) |
| 33 |
{ |
| 34 |
if (pdf_link_state(p) >= 0 && pdf_link_state(p) <= 1) { |
| 35 |
pdf->link_state = pdf_link_state(p); |
| 36 |
} else { |
| 37 |
/* ignores so one can use them for whatever purpose */ |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
void push_link_level(PDF pdf, halfword p) |
| 43 |
{ |
| 44 |
if (pdf->link_stack_ptr >= pdf_max_link_level) |
| 45 |
overflow("pdf link stack size", pdf_max_link_level); |
| 46 |
pdf->link_stack_ptr++; |
| 47 |
pdf->link_stack[pdf->link_stack_ptr].nesting_level = cur_s; |
| 48 |
pdf->link_stack[pdf->link_stack_ptr].link_node = copy_node_list(p); |
| 49 |
pdf->link_stack[pdf->link_stack_ptr].ref_link_node = p; |
| 50 |
pdf->link_stack[pdf->link_stack_ptr].direction = pdf->posstruct->dir; |
| 51 |
pdf->link_stack[pdf->link_stack_ptr].count = 0; |
| 52 |
} |
| 53 |
|
| 54 |
void pop_link_level(PDF pdf) |
| 55 |
{ |
| 56 |
flush_node_list(pdf->link_stack[pdf->link_stack_ptr].link_node); |
| 57 |
pdf->link_stack_ptr--; |
| 58 |
} |
| 59 |
|
| 60 |
void do_link(PDF pdf, halfword p, halfword parent_box, scaledpos cur) |
| 61 |
{ |
| 62 |
scaled_whd alt_rule; |
| 63 |
int k; |
| 64 |
if (type(p) == vlist_node) |
| 65 |
normal_error("pdf backend", "'startlink' ended up in vlist"); |
| 66 |
if (global_shipping_mode == SHIPPING_FORM) |
| 67 |
normal_error("pdf backend", "link annotations cannot be inside an xform"); |
| 68 |
if (is_obj_scheduled(pdf, pdf_link_objnum(p))) |
| 69 |
pdf_link_objnum(p) = pdf_create_obj(pdf, obj_type_others, 0); |
| 70 |
push_link_level(pdf, p); |
| 71 |
alt_rule.wd = width(p); |
| 72 |
alt_rule.ht = height(p); |
| 73 |
alt_rule.dp = depth(p); |
| 74 |
pdf->link_stack[pdf->link_stack_ptr].count++; |
| 75 |
set_rect_dimens(pdf, p, parent_box, cur, alt_rule); |
| 76 |
pdf_ann_margin(p) = pdf_link_margin; |
| 77 |
/*tex The reference for the annot object must be set here. */ |
| 78 |
obj_annot_ptr(pdf, pdf_link_objnum(p)) = p; |
| 79 |
k = pdf_link_objnum(p); |
| 80 |
set_obj_scheduled(pdf, pdf_link_objnum(p)); |
| 81 |
addto_page_resources(pdf, obj_type_link, k); |
| 82 |
} |
| 83 |
|
| 84 |
void end_link(PDF pdf, halfword p) |
| 85 |
{ |
| 86 |
halfword q; |
| 87 |
scaledpos pos = pdf->posstruct->pos; |
| 88 |
if (type(p) == vlist_node) |
| 89 |
normal_error("pdf backend","'endlink' ended up in vlist"); |
| 90 |
if (pdf->link_stack_ptr < 1) |
| 91 |
normal_error("pdf backend","pdf link_stack empty, 'endlink' used without 'startlink'"); |
| 92 |
if (pdf->link_stack[pdf->link_stack_ptr].nesting_level != cur_s) |
| 93 |
normal_error("pdf backend","'endlink' ended up in different nesting level than 'startlink'"); |
| 94 |
/*tex |
| 95 |
The test for running link must be done on |link_node| and not |
| 96 |
|ref_link_node|, as |ref_link_node| can be set by |do_link| or |
| 97 |
|append_link| already. |
| 98 |
*/ |
| 99 |
if (is_running(width(pdf->link_stack[pdf->link_stack_ptr].link_node))) { |
| 100 |
q = pdf->link_stack[pdf->link_stack_ptr].ref_link_node; |
| 101 |
if (global_shipping_mode == SHIPPING_PAGE && matrixused()) { |
| 102 |
matrixrecalculate(pos.h); |
| 103 |
pdf_ann_left(q) = getllx(); |
| 104 |
pdf_ann_top(q) = getlly(); |
| 105 |
pdf_ann_right(q) = geturx(); |
| 106 |
pdf_ann_bottom(q) = getury(); |
| 107 |
} else { |
| 108 |
switch (pdf->posstruct->dir) { |
| 109 |
case dir_TLT: |
| 110 |
pdf_ann_right(q) = pos.h; |
| 111 |
break; |
| 112 |
case dir_TRT: |
| 113 |
/*tex |
| 114 |
In version 1.13.0 the test for swapping was moved |
| 115 |
to the moment we write the rectangle, but it did not |
| 116 |
consider this case. |
| 117 |
*/ |
| 118 |
if (pdf_linking) { |
| 119 |
/* begin of experiment, can be simplified */ |
| 120 |
if (pdf->link_stack[pdf->link_stack_ptr].count > 1) { |
| 121 |
pdf_ann_right(q) = pdf_ann_right(q) - pdf_ann_left(q) + pos.h; |
| 122 |
pdf_ann_left(q) = pos.h; |
| 123 |
if (pdf_ann_left(q) > pdf_ann_right(q)) { |
| 124 |
halfword r = pdf_ann_right(q); |
| 125 |
/* halfword l = pdf_ann_left(q); */ |
| 126 |
pdf_ann_right(q) = pos.h - r; |
| 127 |
pdf_ann_left(q) = pos.h; |
| 128 |
} |
| 129 |
break; |
| 130 |
} |
| 131 |
/* end of experiment */ |
| 132 |
} |
| 133 |
if (pdf_ann_left(q)<pdf_ann_right(q)) { |
| 134 |
pdf_ann_left(q) = pos.h; |
| 135 |
} else { |
| 136 |
pdf_ann_right(q) = pos.h; |
| 137 |
} |
| 138 |
break; |
| 139 |
case dir_LTL: |
| 140 |
case dir_RTT: |
| 141 |
pdf_ann_bottom(q) = pos.v; |
| 142 |
break; |
| 143 |
default: |
| 144 |
pdf_ann_right(q) = pos.h; |
| 145 |
formatted_warning("pdf backend","forcing bad dir %i to TLT in link",pdf->posstruct->dir); |
| 146 |
break; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
pop_link_level(pdf); |
| 151 |
} |
| 152 |
|
| 153 |
/*tex |
| 154 |
|
| 155 |
For ``running'' annotations we must append a new node when the end of |
| 156 |
annotation is in other box than its start. The new created node is identical |
| 157 |
to corresponding whatsit node representing the start of annotation, but its |
| 158 |
|info| field is |max_halfword|. We set |info| field just before destroying |
| 159 |
the node, in order to use |flush_node_list| to do the job. |
| 160 |
|
| 161 |
*/ |
| 162 |
|
| 163 |
void append_link(PDF pdf, halfword parent_box, scaledpos cur, small_number i) |
| 164 |
{ |
| 165 |
halfword p; |
| 166 |
int k; |
| 167 |
scaled_whd alt_rule; |
| 168 |
p = copy_node(pdf->link_stack[(int) i].link_node); |
| 169 |
pdf->link_stack[(int) i].ref_link_node = p; |
| 170 |
pdf->link_stack[(int) i].count++; |
| 171 |
/*tex This node is not a normal link node. */ |
| 172 |
subtype(p) = pdf_link_data_node; |
| 173 |
alt_rule.wd = width(p); |
| 174 |
alt_rule.ht = height(p); |
| 175 |
alt_rule.dp = depth(p); |
| 176 |
set_rect_dimens(pdf, p, parent_box, cur, alt_rule); |
| 177 |
k = pdf_create_obj(pdf, obj_type_others, 0); |
| 178 |
obj_annot_ptr(pdf, k) = p; |
| 179 |
set_obj_scheduled(pdf, pdf_link_objnum(p)); |
| 180 |
addto_page_resources(pdf, obj_type_link, k); |
| 181 |
} |
| 182 |
|
| 183 |
void scan_startlink(PDF pdf) |
| 184 |
{ |
| 185 |
int k; |
| 186 |
halfword r; |
| 187 |
if (abs(cur_list.mode_field) == vmode) |
| 188 |
normal_error("pdf backend", "startlink cannot be used in vertical mode"); |
| 189 |
k = pdf_create_obj(pdf, obj_type_others, 0); |
| 190 |
new_annot_whatsit(pdf_start_link_node); |
| 191 |
set_pdf_link_attr(cur_list.tail_field, null); |
| 192 |
if (scan_keyword("attr")) { |
| 193 |
scan_toks(false, true); |
| 194 |
set_pdf_link_attr(cur_list.tail_field, def_ref); |
| 195 |
} |
| 196 |
r = scan_action(pdf); |
| 197 |
set_pdf_link_action(cur_list.tail_field, r); |
| 198 |
set_pdf_link_objnum(cur_list.tail_field, k); |
| 199 |
pdf_last_link = k; |
| 200 |
/*tex |
| 201 |
Although it is possible to set |obj_annot_ptr(k) := tail| here, it is not |
| 202 |
safe if nodes are later copied/destroyed/moved; a better place to do this |
| 203 |
is inside |do_link|, when the whatsit node is written out. |
| 204 |
*/ |
| 205 |
} |