| 1 |
/* |
| 2 |
|
| 3 |
Copyright 1996-2006 Han The Thanh <[email protected]> |
| 4 |
Copyright 2006-2013 Taco Hoekwater <[email protected]> |
| 5 |
|
| 6 |
This file is part of LuaTeX. |
| 7 |
|
| 8 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 9 |
the terms of the GNU General Public License as published by the Free |
| 10 |
Software Foundation; either version 2 of the License, or (at your |
| 11 |
option) any later version. |
| 12 |
|
| 13 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 14 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 16 |
License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License along |
| 19 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
*/ |
| 22 |
|
| 23 |
#include "ptexlib.h" |
| 24 |
#include "lua/luatex-api.h" |
| 25 |
|
| 26 |
/*tex |
| 27 |
|
| 28 |
Some macros for processing character packets. |
| 29 |
|
| 30 |
*/ |
| 31 |
|
| 32 |
int packet_cur_s; |
| 33 |
int packet_stack_ptr; |
| 34 |
|
| 35 |
#define packet_number(fw) { \ |
| 36 |
fw = *(vfp++); \ |
| 37 |
fw = fw * 256 + *(vfp++); \ |
| 38 |
fw = fw * 256 + *(vfp++); \ |
| 39 |
fw = fw * 256 + *(vfp++); \ |
| 40 |
} |
| 41 |
|
| 42 |
#define packet_scaled(a, fs) { \ |
| 43 |
int fw; \ |
| 44 |
fw = *(vfp++); \ |
| 45 |
if (fw > 127) \ |
| 46 |
fw = fw - 256; \ |
| 47 |
fw = fw * 256 + *(vfp++); \ |
| 48 |
fw = fw * 256 + *(vfp++); \ |
| 49 |
fw = fw * 256 + *(vfp++); \ |
| 50 |
a = store_scaled_f(fw, fs); \ |
| 51 |
} |
| 52 |
|
| 53 |
vf_struct *new_vfstruct(void) |
| 54 |
{ |
| 55 |
vf_struct *vp = (vf_struct *) xmalloc(sizeof(vf_struct)); |
| 56 |
vp->packet_stack_level = vp->packet_stack_minlevel = 0; |
| 57 |
vp->packet_stack = (packet_stack_record *) xmalloc(packet_stack_size * sizeof(packet_stack_record)); |
| 58 |
vp->lf = 0; |
| 59 |
vp->fs_f = 0; |
| 60 |
vp->packet_cur_s = 0; |
| 61 |
vp->refpos = NULL; |
| 62 |
vp->vflua = false; |
| 63 |
return vp; |
| 64 |
} |
| 65 |
|
| 66 |
/*tex |
| 67 |
|
| 68 |
Count the number of bytes in a command packet. |
| 69 |
*/ |
| 70 |
|
| 71 |
int vf_packet_bytes(charinfo * co) |
| 72 |
{ |
| 73 |
eight_bits *vf_packets, *vfp; |
| 74 |
unsigned k; |
| 75 |
int cmd; |
| 76 |
vfp = vf_packets = get_charinfo_packets(co); |
| 77 |
if (vf_packets == NULL) { |
| 78 |
return 0; |
| 79 |
} |
| 80 |
while ((cmd = *(vfp++)) != packet_end_code) { |
| 81 |
switch (cmd) { |
| 82 |
case packet_nop_code: |
| 83 |
case packet_pop_code: |
| 84 |
case packet_push_code: |
| 85 |
break; |
| 86 |
case packet_char_code: |
| 87 |
case packet_down_code: |
| 88 |
case packet_font_code: |
| 89 |
case packet_image_code: |
| 90 |
case packet_node_code: |
| 91 |
case packet_right_code: |
| 92 |
vfp += 4; |
| 93 |
break; |
| 94 |
case packet_rule_code: |
| 95 |
vfp += 8; |
| 96 |
break; |
| 97 |
case packet_pdf_mode: |
| 98 |
vfp += 4; |
| 99 |
break; |
| 100 |
case packet_pdf_code: |
| 101 |
vfp += 4; |
| 102 |
/*tex Plus a string so we fall through: */ |
| 103 |
case packet_special_code: |
| 104 |
/*tex |+4| */ |
| 105 |
packet_number(k); |
| 106 |
vfp += (int) k; |
| 107 |
break; |
| 108 |
default: |
| 109 |
normal_error("vf", "invalid DVI command (1)"); |
| 110 |
} |
| 111 |
}; |
| 112 |
return (vfp - vf_packets); |
| 113 |
} |
| 114 |
|
| 115 |
/*tex |
| 116 |
|
| 117 |
Typeset the \.{DVI} commands in the character packet for character |c| in |
| 118 |
current font |f|. |
| 119 |
*/ |
| 120 |
|
| 121 |
const char *packet_command_names[] = { |
| 122 |
/*tex |slot| maps to |char| and |font| */ |
| 123 |
"char", |
| 124 |
"font", |
| 125 |
"pop", |
| 126 |
"push", |
| 127 |
"special", |
| 128 |
"image", |
| 129 |
"right", |
| 130 |
"down", |
| 131 |
"rule", |
| 132 |
"node", |
| 133 |
"nop", |
| 134 |
"end", |
| 135 |
/*tex the next one is not (yet) supported */ |
| 136 |
"scale", |
| 137 |
"lua", |
| 138 |
"pdf", |
| 139 |
NULL |
| 140 |
}; |
| 141 |
|
| 142 |
static float packet_float(eight_bits ** vfpp) |
| 143 |
{ |
| 144 |
unsigned int i; |
| 145 |
union U { |
| 146 |
float a; |
| 147 |
eight_bits b[sizeof(float)]; |
| 148 |
} u; |
| 149 |
eight_bits *vfp = *vfpp; |
| 150 |
for (i = 0; i < sizeof(float); i++) |
| 151 |
u.b[i] = *(vfp++); |
| 152 |
*vfpp = vfp; |
| 153 |
return u.a; |
| 154 |
} |
| 155 |
|
| 156 |
/*tex |
| 157 |
|
| 158 |
The |do_vf_packet| procedure is called in order to interpret the character |
| 159 |
packet for a virtual character. Such a packet may contain the instruction to |
| 160 |
typeset a character from the same or an other virtual font; in such cases |
| 161 |
|do_vf_packet| calls itself recursively. The recursion level, i.e., the |
| 162 |
number of times this has happened, is kept in the global variable |
| 163 |
|packet_cur_s| and should not exceed |packet_max_recursion|. |
| 164 |
*/ |
| 165 |
|
| 166 |
void do_vf_packet(PDF pdf, internal_font_number vf_f, int c, int ex_glyph) |
| 167 |
{ |
| 168 |
eight_bits *vfp; |
| 169 |
posstructure *save_posstruct, localpos; |
| 170 |
vf_struct *save_vfstruct, localvfstruct, *vp; |
| 171 |
int cmd, w, mode; |
| 172 |
unsigned k; |
| 173 |
scaledpos size; |
| 174 |
scaled i; |
| 175 |
str_number s; |
| 176 |
float f; |
| 177 |
packet_stack_record *mat_p; |
| 178 |
vfp = get_charinfo_packets(get_charinfo(vf_f, c)); |
| 179 |
save_posstruct = pdf->posstruct; |
| 180 |
/*tex use local structure for recursion */ |
| 181 |
pdf->posstruct = &localpos; |
| 182 |
localpos.pos = save_posstruct->pos; |
| 183 |
/*tex invariably for vf */ |
| 184 |
localpos.dir = dir_TLT; |
| 185 |
save_vfstruct = pdf->vfstruct; |
| 186 |
vp = pdf->vfstruct = &localvfstruct; |
| 187 |
localvfstruct = *save_vfstruct; |
| 188 |
vp->packet_stack_minlevel = ++(vp->packet_stack_level); |
| 189 |
vp->lf = 0; |
| 190 |
vp->fs_f = font_size(vf_f); |
| 191 |
vp->ex_glyph = ex_glyph; |
| 192 |
vp->packet_cur_s++; |
| 193 |
if (vp->packet_cur_s == packet_max_recursion) |
| 194 |
overflow("max level recursion of virtual fonts", packet_max_recursion); |
| 195 |
vp->refpos = save_posstruct; |
| 196 |
vp->vflua = false; |
| 197 |
mat_p = &(vp->packet_stack[vp->packet_stack_level]); |
| 198 |
mat_p->c0 = 1.0; |
| 199 |
mat_p->c1 = 0.0; |
| 200 |
mat_p->c2 = 0.0; |
| 201 |
mat_p->c3 = 1.0; |
| 202 |
mat_p->pos.h = 0; |
| 203 |
mat_p->pos.v = 0; |
| 204 |
while ((cmd = *(vfp++)) != packet_end_code) { |
| 205 |
switch (cmd) { |
| 206 |
case packet_font_code: |
| 207 |
packet_number(vp->lf); |
| 208 |
break; |
| 209 |
case packet_push_code: |
| 210 |
vp->packet_stack_level++; |
| 211 |
if (vp->packet_stack_level == packet_stack_size) |
| 212 |
normal_error("vf", "packet_stack_level overflow"); |
| 213 |
vp->packet_stack[vp->packet_stack_level] = *mat_p; |
| 214 |
mat_p = &(vp->packet_stack[vp->packet_stack_level]); |
| 215 |
break; |
| 216 |
case packet_pop_code: |
| 217 |
if (vp->packet_stack_level == vp->packet_stack_minlevel) |
| 218 |
normal_error("vf", "packet_stack_level underflow"); |
| 219 |
vp->packet_stack_level--; |
| 220 |
mat_p = &(vp->packet_stack[vp->packet_stack_level]); |
| 221 |
break; |
| 222 |
case packet_char_code: |
| 223 |
packet_number(k); |
| 224 |
/*tex We also check if |c == k| and |font(c) == font(k)| */ |
| 225 |
if (!char_exists(vp->lf, (int) k)) { |
| 226 |
char_warning(vp->lf, (int) k); |
| 227 |
} else if (! ((c == k && vp->lf == vf_f)) && (has_packet(vp->lf, (int) k))) { |
| 228 |
do_vf_packet(pdf, vp->lf, (int) k, ex_glyph); |
| 229 |
} else { |
| 230 |
backend_out_node_glyph[BACKEND_INDEX(glyph_node)] (pdf, vp->lf, (int) k, ex_glyph); |
| 231 |
} |
| 232 |
w = char_width(vp->lf, (int) k); |
| 233 |
if (ex_glyph != 0 && w != 0) |
| 234 |
w = round_xn_over_d(w, 1000 + ex_glyph, 1000); |
| 235 |
mat_p->pos.h += w; |
| 236 |
break; |
| 237 |
case packet_rule_code: |
| 238 |
packet_scaled(size.v, vp->fs_f); |
| 239 |
packet_scaled(size.h, vp->fs_f); |
| 240 |
if (ex_glyph != 0 && size.h > 0) |
| 241 |
size.h = round_xn_over_d(size.h, 1000 + ex_glyph, 1000); |
| 242 |
if (size.h > 0 && size.v > 0) |
| 243 |
backend_out_node_rule[BACKEND_INDEX(rule_node)](pdf, 0, size,0); |
| 244 |
mat_p->pos.h += size.h; |
| 245 |
break; |
| 246 |
case packet_right_code: |
| 247 |
packet_scaled(i, vp->fs_f); |
| 248 |
if (ex_glyph != 0 && i != 0) |
| 249 |
i = round_xn_over_d(i, 1000 + ex_glyph, 1000); |
| 250 |
mat_p->pos.h += i; |
| 251 |
break; |
| 252 |
case packet_down_code: |
| 253 |
packet_scaled(i, vp->fs_f); |
| 254 |
mat_p->pos.v += i; |
| 255 |
break; |
| 256 |
case packet_pdf_code: |
| 257 |
packet_number(mode); |
| 258 |
packet_number(k); |
| 259 |
str_room(k); |
| 260 |
while (k > 0) { |
| 261 |
k--; |
| 262 |
append_char(*(vfp++)); |
| 263 |
} |
| 264 |
s = make_string(); |
| 265 |
pdf_literal(pdf, s, mode, false); |
| 266 |
flush_str(s); |
| 267 |
break; |
| 268 |
case packet_pdf_mode: |
| 269 |
packet_number(mode); |
| 270 |
pdf_literal_set_mode(pdf, mode); |
| 271 |
break; |
| 272 |
case packet_special_code: |
| 273 |
packet_number(k); |
| 274 |
str_room(k); |
| 275 |
while (k > 0) { |
| 276 |
k--; |
| 277 |
append_char(*(vfp++)); |
| 278 |
} |
| 279 |
s = make_string(); |
| 280 |
pdf_literal(pdf, s, scan_special, false); |
| 281 |
flush_str(s); |
| 282 |
break; |
| 283 |
case packet_lua_code: |
| 284 |
packet_number(k); |
| 285 |
vp->vflua = true; |
| 286 |
luacall_vf(k, vf_f, c); |
| 287 |
/*tex |
| 288 |
|
| 289 |
We don't release as we (can ) flush multiple times, so no: |
| 290 |
|
| 291 |
\starttyping |
| 292 |
luaL_unref(Luas, LUA_REGISTRYINDEX, k); |
| 293 |
\stoptyping |
| 294 |
|
| 295 |
here! |
| 296 |
|
| 297 |
*/ |
| 298 |
vp->vflua = false; |
| 299 |
break; |
| 300 |
case packet_image_code: |
| 301 |
packet_number(k); |
| 302 |
vf_out_image(pdf, k); |
| 303 |
break; |
| 304 |
case packet_node_code: |
| 305 |
packet_number(k); |
| 306 |
hlist_out(pdf, (halfword) k, 0); |
| 307 |
break; |
| 308 |
case packet_nop_code: |
| 309 |
break; |
| 310 |
case packet_scale_code: |
| 311 |
/*tex This is not yet supported in the backend. */ |
| 312 |
f = packet_float(&vfp); |
| 313 |
mat_p->c0 = mat_p->c0 * f; |
| 314 |
mat_p->c3 = mat_p->c3 * f; |
| 315 |
/* pdf->pstruct->scale = f; */ |
| 316 |
pdf->pstruct->need_tm = true; |
| 317 |
pdf->pstruct->need_tf = true; |
| 318 |
break; |
| 319 |
default: |
| 320 |
normal_error("vf", "invalid DVI command (2)"); |
| 321 |
} |
| 322 |
/*tex The trivial case, always |TLT|. */ |
| 323 |
synch_pos_with_cur(&localpos, save_posstruct, mat_p->pos); |
| 324 |
} |
| 325 |
pdf->posstruct = save_posstruct; |
| 326 |
pdf->vfstruct = save_vfstruct; |
| 327 |
} |
| 328 |
|
| 329 |
int *packet_local_fonts(internal_font_number f, int *num) |
| 330 |
{ |
| 331 |
int c, cmd, lf, k, l, i; |
| 332 |
int localfonts[256] = { 0 }; |
| 333 |
int *lfs; |
| 334 |
charinfo *co; |
| 335 |
eight_bits *vf_packets, *vfp; |
| 336 |
k = 0; |
| 337 |
for (c = font_bc(f); c <= font_ec(f); c++) { |
| 338 |
if (quick_char_exists(f, c)) { |
| 339 |
co = get_charinfo(f, c); |
| 340 |
vfp = vf_packets = get_charinfo_packets(co); |
| 341 |
if (vf_packets == NULL) |
| 342 |
continue; |
| 343 |
while ((cmd = *(vfp++)) != packet_end_code) { |
| 344 |
switch (cmd) { |
| 345 |
case packet_font_code: |
| 346 |
packet_number(lf); |
| 347 |
for (l = 0; l < k; l++) { |
| 348 |
if (localfonts[l] == lf) { |
| 349 |
break; |
| 350 |
} |
| 351 |
} |
| 352 |
if (l == k) { |
| 353 |
localfonts[k++] = lf; |
| 354 |
} |
| 355 |
break; |
| 356 |
case packet_nop_code: |
| 357 |
case packet_pop_code: |
| 358 |
case packet_push_code: |
| 359 |
break; |
| 360 |
case packet_char_code: |
| 361 |
case packet_down_code: |
| 362 |
case packet_image_code: |
| 363 |
case packet_node_code: |
| 364 |
case packet_right_code: |
| 365 |
vfp += 4; |
| 366 |
break; |
| 367 |
case packet_rule_code: |
| 368 |
vfp += 8; |
| 369 |
break; |
| 370 |
case packet_special_code: |
| 371 |
packet_number(i); |
| 372 |
vfp += i; |
| 373 |
break; |
| 374 |
default: |
| 375 |
normal_error("vf", "invalid DVI command (3)"); |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
*num = k; |
| 381 |
if (k > 0) { |
| 382 |
lfs = xmalloc((unsigned) ((unsigned) k * sizeof(int))); |
| 383 |
memcpy(lfs, localfonts, (size_t) ((unsigned) k * sizeof(int))); |
| 384 |
return lfs; |
| 385 |
} |
| 386 |
return NULL; |
| 387 |
} |
| 388 |
|
| 389 |
void replace_packet_fonts(internal_font_number f, int *old_fontid, int *new_fontid, int count) |
| 390 |
{ |
| 391 |
int c, cmd, lf, k, l; |
| 392 |
charinfo *co; |
| 393 |
eight_bits *vf_packets, *vfp; |
| 394 |
for (c = font_bc(f); c <= font_ec(f); c++) { |
| 395 |
if (quick_char_exists(f, c)) { |
| 396 |
co = get_charinfo(f, c); |
| 397 |
vfp = vf_packets = get_charinfo_packets(co); |
| 398 |
if (vf_packets == NULL) |
| 399 |
continue; |
| 400 |
while ((cmd = *(vfp++)) != packet_end_code) { |
| 401 |
switch (cmd) { |
| 402 |
case packet_font_code: |
| 403 |
packet_number(lf); |
| 404 |
for (l = 0; l < count; l++) { |
| 405 |
if (old_fontid[l] == lf) { |
| 406 |
break; |
| 407 |
} |
| 408 |
} |
| 409 |
if (l < count) { |
| 410 |
k = new_fontid[l]; |
| 411 |
*(vfp - 4) = (eight_bits) |
| 412 |
((k & 0xFF000000) >> 24); |
| 413 |
*(vfp - 3) = (eight_bits) |
| 414 |
((k & 0x00FF0000) >> 16); |
| 415 |
*(vfp - 2) = (eight_bits) |
| 416 |
((k & 0x0000FF00) >> 8); |
| 417 |
*(vfp - 1) = (eight_bits) (k & 0x000000FF); |
| 418 |
} |
| 419 |
break; |
| 420 |
case packet_nop_code: |
| 421 |
case packet_pop_code: |
| 422 |
case packet_push_code: |
| 423 |
break; |
| 424 |
case packet_char_code: |
| 425 |
case packet_down_code: |
| 426 |
case packet_image_code: |
| 427 |
case packet_node_code: |
| 428 |
case packet_right_code: |
| 429 |
case packet_rule_code: |
| 430 |
vfp += 8; |
| 431 |
break; |
| 432 |
case packet_pdf_mode: |
| 433 |
vfp += 4; |
| 434 |
break; |
| 435 |
case packet_pdf_code: |
| 436 |
vfp += 4; |
| 437 |
/*tex Plus a string so we fall through. */ |
| 438 |
case packet_special_code: |
| 439 |
packet_number(k); |
| 440 |
vfp += k; |
| 441 |
break; |
| 442 |
default: |
| 443 |
normal_error("vf", "invalid DVI command (4)"); |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
} |