Debian Codegraph @

texk/web2c/luatexdir/pdf/pdfaction.c

raw
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 Read an action specification. */
25
26 halfword scan_action(PDF pdf)
27 {
28 int p = new_node(whatsit_node, pdf_action_node);
29 (void) pdf;
30 if (scan_keyword("user"))
31 set_pdf_action_type(p, pdf_action_user);
32 else if (scan_keyword("goto"))
33 set_pdf_action_type(p, pdf_action_goto);
34 else if (scan_keyword("thread"))
35 set_pdf_action_type(p, pdf_action_thread);
36 else
37 normal_error("pdf backend", "action type missing");
38 if (pdf_action_type(p) == pdf_action_user) {
39 scan_toks(false, true);
40 set_pdf_action_tokens(p, def_ref);
41 return p;
42 }
43 pdf_action_named_id(p) = 0;
44 if (scan_keyword("file")) {
45 scan_toks(false, true);
46 set_pdf_action_file(p, def_ref);
47 }
48 if (scan_keyword("struct")) {
49 if (pdf_action_type(p) != pdf_action_goto)
50 normal_error("pdf backend", "only GoTo action can be used with `struct'");
51 if (pdf_action_file(p) != null) {
52 scan_toks(false, true);
53 pdf_action_named_id(p) |= 2;
54 pdf_action_struct_id(p) = def_ref;
55 } else if (scan_keyword("name")) {
56 scan_toks(false, true);
57 pdf_action_named_id(p) |= 2;
58 pdf_action_struct_id(p) = def_ref;
59 }
60 else if (scan_keyword("num")) {
61 scan_int();
62 if (cur_val <= 0)
63 normal_error("pdf backend", "num identifier must be positive");
64 pdf_action_struct_id(p) = cur_val;
65 }
66 else
67 normal_error("pdf backend", "identifier type missing");
68 } else
69 pdf_action_struct_id(p) = null;
70 if (scan_keyword("page")) {
71 if (pdf_action_type(p) != pdf_action_goto)
72 normal_error("pdf backend", "only GoTo action can be used with 'page'");
73 set_pdf_action_type(p, pdf_action_page);
74 scan_int();
75 if (cur_val <= 0)
76 normal_error("pdf backend", "page number must be positive");
77 set_pdf_action_id(p, cur_val);
78 scan_toks(false, true);
79 set_pdf_action_tokens(p, def_ref);
80 } else if (scan_keyword("name")) {
81 scan_toks(false, true);
82 pdf_action_named_id(p) |= 1;
83 set_pdf_action_id(p, def_ref);
84 } else if (scan_keyword("num")) {
85 if ((pdf_action_type(p) == pdf_action_goto) &&
86 (pdf_action_file(p) != null))
87 normal_error("pdf backend", "'goto' option cannot be used with both 'file' and 'num'");
88 scan_int();
89 if (cur_val <= 0)
90 normal_error("pdf backend", "num identifier must be positive");
91 set_pdf_action_id(p, cur_val);
92 } else {
93 normal_error("pdf backend", "identifier type missing");
94 }
95 if (scan_keyword("newwindow")) {
96 set_pdf_action_new_window(p, pdf_window_new);
97 /*tex Scan an optional space. */
98 get_x_token();
99 if (cur_cmd != spacer_cmd)
100 back_input();
101 } else if (scan_keyword("nonewwindow")) {
102 set_pdf_action_new_window(p, pdf_window_nonew);
103 /*tex Scan an optional space. */
104 get_x_token();
105 if (cur_cmd != spacer_cmd)
106 back_input();
107 } else {
108 set_pdf_action_new_window(p, pdf_window_notset);
109 }
110 if ((pdf_action_new_window(p) > pdf_window_notset) &&
111 (((pdf_action_type(p) != pdf_action_goto) &&
112 (pdf_action_type(p) != pdf_action_page)) ||
113 (pdf_action_file(p) == null)))
114 normal_error("pdf backend","'newwindow' or 'nonewwindow' must be used with 'goto' and 'file' option");
115 return p;
116 }
117
118 /*tex Write an action specification. */
119
120 void write_action(PDF pdf, halfword p)
121 {
122 char *s;
123 int d = 0;
124 if (pdf_action_type(p) == pdf_action_user) {
125 pdf_out(pdf, '\n');
126 pdf_print_toks(pdf, pdf_action_tokens(p));
127 pdf_out(pdf, '\n');
128 return;
129 }
130 pdf_begin_dict(pdf);
131 if (pdf_action_file(p) != null) {
132 pdf_add_name(pdf, "F");
133 pdf_out(pdf, ' ');
134 s = tokenlist_to_cstring(pdf_action_file(p), true, NULL);
135 pdf_print_str(pdf, s);
136 xfree(s);
137 pdf_out(pdf, ' ');
138 if (pdf_action_new_window(p) > pdf_window_notset) {
139 if (pdf_action_new_window(p) == pdf_window_new)
140 pdf_dict_add_bool(pdf, "NewWindow", 1);
141 else
142 pdf_dict_add_bool(pdf, "NewWindow", 0);
143 }
144 }
145 switch (pdf_action_type(p)) {
146 case pdf_action_page:
147 pdf_dict_add_name(pdf, "S", "GoTo");
148 if (pdf_action_file(p) == null) {
149 pdf_add_name(pdf, "D");
150 pdf_begin_array(pdf);
151 pdf_add_ref(pdf, pdf_get_obj(pdf, obj_type_page, pdf_action_id(p), false));
152 } else {
153 pdf_add_name(pdf, "D");
154 pdf_begin_array(pdf);
155 pdf_print_int(pdf, pdf_action_id(p) - 1);
156 }
157 {
158 char *tokstr = tokenlist_to_cstring(pdf_action_tokens(p), true, NULL);
159 pdf_printf(pdf, " %s", tokstr);
160 pdf_end_array(pdf);
161 xfree(tokstr);
162 }
163 break;
164 case pdf_action_goto:
165 if (pdf_action_file(p) == null) {
166 pdf_dict_add_name(pdf, "S", "GoTo");
167 d = pdf_get_obj(pdf, obj_type_dest, pdf_action_id(p), (pdf_action_named_id(p) & 1) == 1);
168 } else
169 pdf_dict_add_name(pdf, "S", "GoToR");
170 if (pdf_action_named_id(p) & 1) {
171 char *tokstr = tokenlist_to_cstring(pdf_action_id(p), true, NULL);
172 pdf_dict_add_string(pdf, "D", tokstr);
173 xfree(tokstr);
174 } else if (pdf_action_file(p) == null) {
175 pdf_dict_add_ref(pdf, "D", d);
176 } else {
177 normal_error("pdf backend","'goto' option cannot be used with both 'file' and 'num'");
178 }
179 break;
180 case pdf_action_thread:
181 pdf_dict_add_name(pdf, "S", "Thread");
182 if (pdf_action_file(p) == null) {
183 d = pdf_get_obj(pdf, obj_type_thread, pdf_action_id(p), (pdf_action_named_id(p) & 1) == 1);
184 if (pdf_action_named_id(p) & 1) {
185 char *tokstr = tokenlist_to_cstring(pdf_action_id(p), true, NULL);
186 pdf_dict_add_string(pdf, "D", tokstr);
187 xfree(tokstr);
188 } else if (pdf_action_file(p) == null) {
189 pdf_dict_add_ref(pdf, "D", d);
190 } else {
191 pdf_dict_add_int(pdf, "D", pdf_action_id(p));
192 }
193 }
194 break;
195 }
196 if (pdf_action_struct_id(p) != null) {
197 if (pdf_action_file(p) == null) {
198 d = pdf_get_obj(pdf, obj_type_struct_dest, pdf_action_struct_id(p), (pdf_action_named_id(p) & 2) == 2);
199 pdf_dict_add_ref(pdf, "SD", d);
200 } else {
201 pdf_add_name(pdf, "SD");
202 pdf_print_toks(pdf, pdf_action_struct_id(p));
203 }
204 }
205 pdf_end_dict(pdf);
206 }