| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2010-2012 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 |
scaledpos shipbox_refpos; |
| 25 |
|
| 26 |
/*tex |
| 27 |
|
| 28 |
|ship_out| is used to shipout a box to PDF or DVI mode. If |shipping_mode| is |
| 29 |
set to |SHIPPING_FORM| then the output will be a |Form| object (only PDF), and |
| 30 |
if it is set to |SHIPPING_PAGE| it will be a |Page| object. |
| 31 |
|
| 32 |
*/ |
| 33 |
|
| 34 |
void ship_out(PDF pdf, halfword p, shipping_mode_e shipping_mode) |
| 35 |
{ |
| 36 |
/*tex Indices to first ten count registers: */ |
| 37 |
int j, k; |
| 38 |
int post_callback_id; |
| 39 |
int pre_callback_id; |
| 40 |
/*tex The origin position on the page: */ |
| 41 |
posstructure refpoint; |
| 42 |
int rule_callback_id = 0; |
| 43 |
scaledpos cur = { 0, 0 }; |
| 44 |
refpoint.pos.h = 0; |
| 45 |
refpoint.pos.v = 0; |
| 46 |
ensure_output_state(pdf, ST_HEADER_WRITTEN); |
| 47 |
/*tex This is only for complaining if \.{\\outputmode} has changed: */ |
| 48 |
fix_o_mode(); |
| 49 |
pdf->f_cur = null_font; |
| 50 |
/*tex |
| 51 |
Start sheet {\sl Sync\TeX} information record. We assume that |
| 52 |
|pdf_output| is properly set up. |
| 53 |
*/ |
| 54 |
if (synctex_par) { |
| 55 |
if (output_mode_used == OMODE_DVI) { |
| 56 |
synctexsheet(mag_par); |
| 57 |
} else { |
| 58 |
synctexsheet(1000); |
| 59 |
} |
| 60 |
} |
| 61 |
pre_callback_id = callback_defined(start_page_number_callback); |
| 62 |
post_callback_id = callback_defined(stop_page_number_callback); |
| 63 |
if ((tracing_output_par > 0) && (pre_callback_id == 0)) { |
| 64 |
tprint_nl(""); |
| 65 |
print_ln(); |
| 66 |
tprint("Completed box being shipped out"); |
| 67 |
} |
| 68 |
global_shipping_mode = shipping_mode; |
| 69 |
if (shipping_mode == SHIPPING_PAGE) { |
| 70 |
if (pre_callback_id > 0) |
| 71 |
(void) run_callback(pre_callback_id, "->"); |
| 72 |
else if (pre_callback_id == 0) { |
| 73 |
if (term_offset > max_print_line - 9) |
| 74 |
print_ln(); |
| 75 |
else if ((term_offset > 0) || (file_offset > 0)) |
| 76 |
print_char(' '); |
| 77 |
print_char('['); |
| 78 |
j = 9; |
| 79 |
while ((count(j) == 0) && (j > 0)) |
| 80 |
j--; |
| 81 |
for (k = 0; k <= j; k++) { |
| 82 |
print_int(count(k)); |
| 83 |
if (k < j) |
| 84 |
print_char('.'); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
if ((tracing_output_par > 0) && shipping_mode == SHIPPING_PAGE) { |
| 89 |
if (pre_callback_id == 0) { |
| 90 |
print_char(']'); |
| 91 |
} |
| 92 |
update_terminal(); |
| 93 |
begin_diagnostic(); |
| 94 |
show_box(p); |
| 95 |
end_diagnostic(true); |
| 96 |
} |
| 97 |
/*tex Ship out box |p|:*/ |
| 98 |
if (shipping_mode == SHIPPING_PAGE && box_dir(p) != page_direction_par) |
| 99 |
normal_warning("backend","shipped box direction differs from page direction, the output may be placed wrongly on the page"); |
| 100 |
/*tex |
| 101 |
Update the values of |max_h| and |max_v|; but if the page is too large, |
| 102 |
|goto done|. Sometimes the user will generate a huge page because other |
| 103 |
error messages are being ignored. Such pages are not output to the |
| 104 |
\.{dvi} file, since they may confuse the printing software. |
| 105 |
*/ |
| 106 |
if ((height(p) > max_dimen) || (depth(p) > max_dimen) || (height(p) + depth(p) + v_offset_par > max_dimen) || (width(p) + h_offset_par > max_dimen)) { |
| 107 |
const char *hlp[] = { |
| 108 |
"The page just created is more than 18 feet tall or", |
| 109 |
"more than 18 feet wide, so I suspect something went wrong.", |
| 110 |
NULL |
| 111 |
}; |
| 112 |
tex_error("Huge page cannot be shipped out", hlp); |
| 113 |
if (tracing_output_par <= 0) { |
| 114 |
begin_diagnostic(); |
| 115 |
tprint_nl("The following box has been deleted:"); |
| 116 |
show_box(p); |
| 117 |
end_diagnostic(true); |
| 118 |
} |
| 119 |
goto DONE; |
| 120 |
} |
| 121 |
if (height(p) + depth(p) + v_offset_par > max_v) |
| 122 |
max_v = height(p) + depth(p) + v_offset_par; |
| 123 |
if (width(p) + h_offset_par > max_h) |
| 124 |
max_h = width(p) + h_offset_par; |
| 125 |
/*tex Calculate page dimensions and margins. */ |
| 126 |
if (global_shipping_mode == SHIPPING_PAGE) { |
| 127 |
if (page_width_par > 0) |
| 128 |
pdf->page_size.h = page_width_par; |
| 129 |
else { |
| 130 |
switch (page_direction_par) { |
| 131 |
case dir_TLT: |
| 132 |
pdf->page_size.h = width(p) + 2 * page_left_offset_par; |
| 133 |
break; |
| 134 |
case dir_TRT: |
| 135 |
pdf->page_size.h = width(p) + 2 * page_right_offset_par; |
| 136 |
break; |
| 137 |
case dir_LTL: |
| 138 |
pdf->page_size.h = height(p) + depth(p) + 2 * page_left_offset_par; |
| 139 |
break; |
| 140 |
case dir_RTT: |
| 141 |
pdf->page_size.h = height(p) + depth(p) + 2 * page_right_offset_par; |
| 142 |
break; |
| 143 |
default: |
| 144 |
pdf->page_size.h = width(p) + 2 * page_left_offset_par; |
| 145 |
normal_warning("pdf backend","bad page direction, assuming TLT, case 1"); |
| 146 |
} |
| 147 |
} |
| 148 |
if (page_height_par > 0) |
| 149 |
pdf->page_size.v = page_height_par; |
| 150 |
else { |
| 151 |
switch (page_direction_par) { |
| 152 |
case dir_TLT: |
| 153 |
case dir_TRT: |
| 154 |
pdf->page_size.v = height(p) + depth(p) + 2 * page_top_offset_par; |
| 155 |
break; |
| 156 |
case dir_LTL: |
| 157 |
case dir_RTT: |
| 158 |
pdf->page_size.v = width(p) + 2 * page_top_offset_par; |
| 159 |
break; |
| 160 |
default: |
| 161 |
pdf->page_size.v = height(p) + depth(p) + 2 * page_top_offset_par; |
| 162 |
normal_warning("pdf backend","bad page direction, assuming TLT, case 2"); |
| 163 |
} |
| 164 |
} |
| 165 |
/*tex |
| 166 |
Think in upright page/paper coordinates (page origin = lower left edge). |
| 167 |
First preset |refpoint.pos| to the DVI origin (near upper left page edge). |
| 168 |
*/ |
| 169 |
backend_out_control_ref[BACKEND_INDEX(backend_control_set_reference_point)](pdf,&refpoint); |
| 170 |
/*tex |
| 171 |
Then shift |refpoint.pos| of the DVI origin depending on the |
| 172 |
|page_direction| within the upright (TLT) page coordinate system. |
| 173 |
*/ |
| 174 |
switch (page_direction_par) { |
| 175 |
case dir_TLT: |
| 176 |
case dir_LTL: |
| 177 |
refpoint.pos.h += h_offset_par; |
| 178 |
refpoint.pos.v -= v_offset_par; |
| 179 |
break; |
| 180 |
case dir_TRT: |
| 181 |
case dir_RTT: |
| 182 |
refpoint.pos.h += pdf->page_size.h - page_right_offset_par - one_true_inch; |
| 183 |
refpoint.pos.v -= v_offset_par; |
| 184 |
break; |
| 185 |
default: |
| 186 |
refpoint.pos.h += h_offset_par; |
| 187 |
refpoint.pos.v -= v_offset_par; |
| 188 |
normal_warning("pdf backend","bad page direction, assuming TLT, case 3"); |
| 189 |
} |
| 190 |
/*tex |
| 191 |
Then switch to page box coordinate system; do |height(p)| movement, |
| 192 |
to get the location of the box origin. |
| 193 |
*/ |
| 194 |
pdf->posstruct->dir = page_direction_par; |
| 195 |
cur.h = 0; |
| 196 |
cur.v = height(p); |
| 197 |
synch_pos_with_cur(pdf->posstruct, &refpoint, cur); |
| 198 |
} else { |
| 199 |
/*tex We're shipping out a |/Form|. */ |
| 200 |
pdf->posstruct->dir = box_dir(p); |
| 201 |
switch (pdf->posstruct->dir) { |
| 202 |
case dir_TLT: |
| 203 |
case dir_TRT: |
| 204 |
pdf->page_size.h = width(p); |
| 205 |
pdf->page_size.v = height(p) + depth(p); |
| 206 |
break; |
| 207 |
case dir_LTL: |
| 208 |
case dir_RTT: |
| 209 |
pdf->page_size.h = height(p) + depth(p); |
| 210 |
pdf->page_size.v = width(p); |
| 211 |
break; |
| 212 |
default: |
| 213 |
pdf->page_size.h = width(p); |
| 214 |
pdf->page_size.v = height(p) + depth(p); |
| 215 |
normal_warning("pdf backend","bad page direction, assuming TLT, case 4"); |
| 216 |
} |
| 217 |
switch (pdf->posstruct->dir) { |
| 218 |
case dir_TLT: |
| 219 |
pdf->posstruct->pos.h = 0; |
| 220 |
pdf->posstruct->pos.v = depth(p); |
| 221 |
break; |
| 222 |
case dir_TRT: |
| 223 |
pdf->posstruct->pos.h = width(p); |
| 224 |
pdf->posstruct->pos.v = depth(p); |
| 225 |
break; |
| 226 |
case dir_LTL: |
| 227 |
pdf->posstruct->pos.h = height(p); |
| 228 |
pdf->posstruct->pos.v = width(p); |
| 229 |
break; |
| 230 |
case dir_RTT: |
| 231 |
pdf->posstruct->pos.h = depth(p); |
| 232 |
pdf->posstruct->pos.v = width(p); |
| 233 |
break; |
| 234 |
default: |
| 235 |
pdf->posstruct->pos.h = 0; |
| 236 |
pdf->posstruct->pos.v = depth(p); |
| 237 |
normal_warning("pdf backend","bad page direction, assuming TLT, case 5"); |
| 238 |
} |
| 239 |
} |
| 240 |
/*tex |
| 241 |
Now we are at the point on the page where the origin of the page box |
| 242 |
should go. First we register the poisition for \.{\\gleaders}. |
| 243 |
*/ |
| 244 |
shipbox_refpos = pdf->posstruct->pos; |
| 245 |
backend_out_control[BACKEND_INDEX(backend_control_begin_page)](pdf); |
| 246 |
rule_callback_id = callback_defined(process_rule_callback); |
| 247 |
switch (type(p)) { |
| 248 |
case vlist_node: |
| 249 |
vlist_out(pdf, p, rule_callback_id); |
| 250 |
break; |
| 251 |
case hlist_node: |
| 252 |
hlist_out(pdf, p, rule_callback_id); |
| 253 |
break; |
| 254 |
default: |
| 255 |
normal_error("pdf backend", "no vlist or hlist in (xform) shipout"); |
| 256 |
} |
| 257 |
if (shipping_mode == SHIPPING_PAGE) |
| 258 |
total_pages++; |
| 259 |
cur_s = -1; |
| 260 |
/*tex Finish shipping */ |
| 261 |
backend_out_control[BACKEND_INDEX(backend_control_end_page)](pdf); |
| 262 |
DONE: |
| 263 |
if ((tracing_output_par <= 0) && (post_callback_id == 0) && shipping_mode == SHIPPING_PAGE) { |
| 264 |
print_char(']'); |
| 265 |
update_terminal(); |
| 266 |
} |
| 267 |
dead_cycles = 0; |
| 268 |
/*tex Flush the box from memory, showing statistics if requested. */ |
| 269 |
if ((tracing_stats_par > 1) && (pre_callback_id == 0)) { |
| 270 |
tprint_nl("Memory usage before: "); |
| 271 |
print_int(var_used); |
| 272 |
print_char('&'); |
| 273 |
print_int(dyn_used); |
| 274 |
print_char(';'); |
| 275 |
} |
| 276 |
flush_node_list(p); |
| 277 |
if ((tracing_stats_par > 1) && (post_callback_id == 0)) { |
| 278 |
tprint(" after: "); |
| 279 |
print_int(var_used); |
| 280 |
print_char('&'); |
| 281 |
print_int(dyn_used); |
| 282 |
print_ln(); |
| 283 |
} |
| 284 |
if (shipping_mode == SHIPPING_PAGE && (post_callback_id > 0)) |
| 285 |
(void) run_callback(post_callback_id, "->"); |
| 286 |
/*tex Finish sheet {\sl Sync\TeX} information record. */ |
| 287 |
if (synctex_par) |
| 288 |
synctexteehs(); |
| 289 |
global_shipping_mode = NOT_SHIPPING; |
| 290 |
} |