35 #define DVICMDDEF(x) static int x __PROTO((DviContext *, int))
129 #define DVI_BUFLEN 4096
172 static void dvierr(DviContext *dvi,
const char *format, ...)
176 va_start(ap, format);
177 fprintf(stderr,
"%s[%d]: Error: ",
179 vfprintf(stderr, format, ap);
183 static void dviwarn(DviContext *dvi,
const char *format, ...)
187 fprintf(stderr,
"%s[%d]: Warning: ",
189 va_start(ap, format);
190 vfprintf(stderr, format, ap);
194 #define NEEDBYTES(d,n) \
195 ((d)->buffer.pos + (n) > (d)->buffer.length)
209 dviwarn(dvi,
_(
"unexpected EOF\n"));
251 static int dskip(DviContext *dvi,
long offset)
262 static long dsgetn(DviContext *dvi,
size_t n)
273 static int dread(DviContext *dvi,
char *buffer,
size_t len)
282 static long dugetn(DviContext *dvi,
size_t n)
310 #define dsget1(d) dsgetn((d), 1)
311 #define dsget2(d) dsgetn((d), 2)
312 #define dsget3(d) dsgetn((d), 3)
313 #define dsget4(d) dsgetn((d), 4)
314 #define duget1(d) dugetn((d), 1)
315 #define duget2(d) dugetn((d), 2)
316 #define duget3(d) dugetn((d), 3)
317 #define duget4(d) dugetn((d), 4)
320 static void dviprint(DviContext *dvi,
const char *command,
int sub,
const char *fmt, ...)
326 for(i = 0; i < dvi->
depth; i++)
328 printf(
"%4lu: %s",
dtell(dvi), command);
329 if(sub >= 0) printf(
"%d", sub);
330 if(*fmt) printf(
": ");
336 if(_mdvi_debug_mask & DBG_OPCODE) do { dviprint x; } while(0)
338 #define SHOWCMD(x) do { } while(0)
345 for(i = 0; i < dvi->
npages; i++)
346 if(dvi->
pagemap[i][1] == tex_page)
352 static int sort_up(
const void *p1,
const void *p2)
354 return ((
long *)p1)[1] - ((
long *)p2)[1];
358 return ((
long *)p2)[1] - ((
long *)p1)[1];
362 return (rand() % 1) ? -1 : 1;
366 return ((
long *)p1)[0] - ((
long *)p2)[0];
370 return ((
long *)p1)[0] - ((
long *)p2)[0];
375 int (*sortfunc)
__PROTO((
const void *,
const void *));
425 DEBUG((
DBG_FONTS,
"requesting font %d = `%s' at %.1fpt (%dx%d dpi)\n",
445 if(len >= 4 &&
STREQ(name+len-4,
".dvi")) {
447 if(access(name, R_OK) == 0)
454 strcpy(file+len,
".dvi");
456 if(access(file, R_OK) == 0)
461 if(access(file, R_OK) == 0)
478 pars = np ? np : &dvi->
params;
547 va_start(ap, option);
582 np.
gamma = va_arg(ap,
double);
590 np.
mag = va_arg(ap,
double);
597 np.
hdrift = va_arg(ap,
int);
600 np.
vdrift = va_arg(ap,
int);
618 if(np.
dpi <= 0 || np.
vdpi <= 0)
683 if(filename == NULL) {
687 p = fopen(filename,
"rb");
694 memzero(dvi,
sizeof(DviContext));
711 mdvi_error(
_(
"%s: unsupported DVI format (version %u)\n"),
726 (par->
mag > 0 ? par->
mag : (double)dvi->
dvimag / 1000.0);
740 ((
double)dvi->
den / 473628672) / 16.0;
764 fread(dvi->
fileid, 1, n, p);
774 if(fseek(p, (
long)-1, SEEK_END) == -1)
777 if(fseek(p, (
long)-2, SEEK_CUR) < 0)
779 if(op != arg || n < 4)
782 fseek(p, (
long)-5, SEEK_CUR);
785 fseek(p, (
long)arg, SEEK_SET);
796 DEBUG((
DBG_DVI,
"%s: from postamble: stack depth %d, %d page%s\n",
846 while(offset != -1) {
850 fseek(p, offset, SEEK_SET);
854 for(i = 1; i <= 10; i++)
860 DEBUG((
DBG_DVI,
"Page %d (%ld.%ld.%ld.%ld.%ld.%ld.%ld.%ld.%ld.%ld) ignored by request\n",
861 n, page[1], page[2], page[3], page[4], page[5],
862 page[6], page[7], page[8], page[9], page[10]));
870 if(pagecount >= dvi->
npages) {
904 mdvi_error(
_(
"%s: File corrupted, or not a DVI file\n"), file);
945 if(pageno > dvi->
npages-1)
968 saved_buffer = dvi->
buffer;
969 saved_file = dvi->
in;
981 if(dvi_commands[opcode](dvi, opcode) < 0)
985 dviwarn(dvi,
_(
"%s: vf macro had errors\n"),
988 dviwarn(dvi,
_(
"%s: stack not empty after vf macro\n"),
995 dvi->
buffer = saved_buffer;
996 dvi->
in = saved_file;
999 return (opcode !=
DVI_EOP ? -1 : 0);
1009 if(dvi->
in == NULL) {
1012 if(dvi->
in == NULL) {
1029 if(pageno < 0 || pageno > dvi->
npages-1) {
1035 fseek(dvi->
in, (
long)dvi->
pagemap[pageno][0], SEEK_SET);
1043 fseek(dvi->
in, (
long)44, SEEK_CUR);
1091 if(dvi_commands[op](dvi, op) < 0)
1100 dviwarn(dvi,
_(
"stack not empty at end of page\n"));
1108 dvi->
pos.
v += amount;
1112 if(amount > dvi->
params.
vsmallsp || amount <= -dvi->params.vsmallsp)
1131 dvi->
pos.
h += amount;
1135 else if(amount > dvi->
params.
thinsp || amount <= -6 * dvi->params.thinsp)
1165 #define DBGSUM(a,b,c) \
1166 (a), (b) > 0 ? '+' : '-', \
1167 (b) > 0 ? (b) : -(b), (c)
1257 dvierr(dvi,
_(
"no default font set yet\n"));
1262 if(ch == NULL || ch->
missing) {
1267 _(
"requested character %d does not exist in `%s'\n"),
1287 SHOWCMD((dvi,
"setchar", num,
"(%d,%d) h:=%d%c%d=%d, hh:=%d (%s)\n",
1306 if(a > 0 && b > 0) {
1309 "width %d, height %d (%dx%d pixels)\n",
1319 "(moving left only, by %d)\n", b));
1332 SHOWCMD((dvi,
"noop", -1,
""));
1336 int push(DviContext *dvi,
int opcode)
1340 dviwarn(dvi,
_(
"enlarging stack\n"));
1347 "level %d: (h=%d,v=%d,w=%d,x=%d,y=%d,z=%d,hh=%d,vv=%d)\n",
1355 int pop(DviContext *dvi,
int opcode)
1358 dvierr(dvi,
_(
"stack underflow\n"));
1363 "level %d: (h=%d,v=%d,w=%d,x=%d,y=%d,z=%d,hh=%d,vv=%d)\n",
1380 "%d h:=%d%c%d=%d, hh:=%d\n",
1395 "%d v:=%d%c%d=%d, vv:=%d\n",
1410 "%d h:=%d%c%d=%d, hh:=%d\n",
1425 "%d h:=%d%c%d=%d, hh:=%d\n",
1440 "%d h:=%d%c%d=%d, hh:=%d\n",
1455 "%d h:=%d%c%d=%d, hh:=%d\n",
1470 ref = dvi->findref(dvi, ndx);
1472 dvierr(dvi,
_(
"font %d is not defined\n"),
1473 opcode - DVI_FNT_NUM0);
1476 SHOWCMD((dvi,
"fntnum", opcode - DVI_FNT_NUM0,
1477 "current font is %s\n",
1492 ref = dvi->findref(dvi, arg);
1494 dvierr(dvi,
_(
"font %d is not defined\n"), arg);
1498 "current font is %s (id %d)\n",
1511 dvierr(dvi,
_(
"malformed special length\n"));
1533 ref = dvi->findref(dvi, arg);
1538 dvierr(dvi,
_(
"font %d is not defined in postamble\n"), arg);
1542 "%d -> %s (%d links)\n",
1550 dvierr(dvi,
_(
"unexpected opcode %d\n"), opcode);
1556 dvierr(dvi,
_(
"undefined opcode %d\n"), opcode);