summaryrefslogtreecommitdiff
path: root/systems/msdos/emtex-contrib/dvimfj/dvimfj.src/dvimfj.c
blob: 56bff96a588779b00d474db96b4ec1a1a8fba561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*****************************************************************************/
/*                                                                           */
/*   dvimfj  ---  generates an MFJOB program from a .dvi file.               */
/*                                                                           */
/*****************************************************************************/

#include <stdio.h>
#include <ctype.h>
#include "commands.h"
#if defined(MSDOS)
        #include <stdlib.h>
        #include <string.h>
        #include <math.h>
        #include <process.h>
#endif

#define LASTCHAR        127    /* max dvi character, above are commands    */

#define get1()           num(1)
#define get2()           num(2)
#define get3()           num(3)
#define get4()           num(4)
#define sget1()         snum(1)
#define sget2()         snum(2)
#define sget3()         snum(3)
#define sget4()         snum(4)

typedef struct{
    char *name;
    char *mag;
    int  base;
    int  mode;
} FONTDEF;

typedef struct{
    char *pattern;
    int  base;
    int  mode;
} FONTPAT;

#define MaxBAidx  10      /* maximum number of bases (plain, cm, etc.) */
#define MaxMOidx  10      /* maximum number of modes (lj, fx, etc.) */

int FDsize, FDidx; FONTDEF **Fontdefs;
int FPsize, FPidx; FONTPAT **FPatterns;
int BAidx;         char *BAses[MaxBAidx];
int MOidx;         char *MOdes[MaxMOidx];
int Nomfj;

char *Inputmodes, *MFjobcmd, *Outname="dvimfj.mfj";

FILE * dvifp;
char * dvi_name;
long   pc;

long Numerator, Denominator, Magnification;
double Scale = 1;

char *          realloc          ();

void            main             ();
void            read_dvi_file    ();
void            bop              ();
void            preamble         ();
void            postamble        ();
void            postpostamble    ();
void            fontdef          ();
void            special          ();
unsigned long   num              ();
long            snum             ();
void            addfontdef       ();
void *          Realloc          ();
char *          Strdup           ();
void            read_config_file ();
void            read_config_80   ();
void            addpatterndef    ();
void            write_mfj        ();
void            run_mfjob        ();




/*---------------------------------------------------------------------------*/

void main(argc, argv)
int argc;
char **argv;
{
    register int i, jx;
    long scale;

    if (argc < 2) {
        usage:
        printf("\nUsage: %s dvifile [magstep] [no]\n", *argv);
        printf("\tFonts scaled (1.2^magstep) or (magstep/1000).\n"
               "\tNo MFjob spawn.\n");
        exit(1);
    }

    else{
        if ((i = strlen(argv[1])) == 0) {
            printf("Illegal empty filename\n");
            exit(2);
        }
        if ((i >= 5) && !strcmpi(argv[1]+i-4, ".dvi")) dvi_name = argv[1];
        else {
            dvi_name = Realloc(0, (i+5) * sizeof(char));
            strcpy(dvi_name, argv[1]);
            strcat(dvi_name, ".dvi");
        }
        if ((dvifp = fopen(dvi_name, "rb")) == NULL) {
            printf("Cant open %s\n", dvi_name);
            exit(3);
        }
        if (argc > 2){
            for (jx = 2; jx < argc; jx++){
                if ('N' == toupper(argv[jx][0])){
                    Nomfj = 1;
                    continue;
                }
                scale = 0;
                if (1 > sscanf(argv[jx], " %ld", &scale)) goto usage;
                if (scale > 30) Scale = scale/1000.f;
                else            Scale = pow((double)1.2, (double)scale);
            }
        }
    }

    read_config_file(argv[0]);
    read_dvi_file();
    write_mfj();
    run_mfjob();
    exit(0);

} /* main */


void read_dvi_file()
{
    int opcode;

    while ((opcode = (int) get1()) != EOF) {    /* process until end of file */
        if ((opcode <= LASTCHAR) && isprint(opcode)) {
            while ((opcode <= LASTCHAR) && isprint(opcode)) {
                opcode = (int) get1();
            }
        }

        if (opcode <= LASTCHAR) ;
        else if ((opcode >= FONT_00) && (opcode <= FONT_63)) ;
        else switch (opcode) {
            case SET1     :
            case SET2     :
            case SET3     :
            case SET4     : num(opcode - SET1 + 1);         break;
            case SET_RULE : sget4(); sget4();               break;
            case PUT1     :
            case PUT2     :
            case PUT3     :
            case PUT4     : num(opcode - PUT1 + 1);         break;
            case PUT_RULE : sget4(); sget4();
            case NOP      : break;
            case BOP      : bop();                          break;
            case EOP      :
            case PUSH     :
            case POP      : break;
            case RIGHT1   :
            case RIGHT2   :
            case RIGHT3   :
            case RIGHT4   : snum(opcode - RIGHT1 + 1);
            case W0       : break;
            case W1       :
            case W2       :
            case W3       :
            case W4       : snum(opcode - W0);
            case X0       : break;
            case X1       :
            case X2       :
            case X3       :
            case X4       : snum(opcode - X0);              break;
            case DOWN1    :
            case DOWN2    :
            case DOWN3    :
            case DOWN4    : snum(opcode - DOWN1 + 1);
            case Y0       : break;
            case Y1       :
            case Y2       :
            case Y3       :
            case Y4       : snum(opcode - Y0);
            case Z0       : break;
            case Z1       :
            case Z2       :
            case Z3       :
            case Z4       : snum(opcode - Z0);              break;
            case FNT1     :
            case FNT2     :
            case FNT3     :
            case FNT4     : num(opcode - FNT1 + 1);         break;
            case XXX1     :
            case XXX2     :
            case XXX3     :
            case XXX4     : special(opcode - XXX1 + 1);     break;
            case FNT_DEF1 :
            case FNT_DEF2 :
            case FNT_DEF3 :
            case FNT_DEF4 : fontdef(opcode - FNT_DEF1 + 1); break;
            case PRE      : preamble();                     break;
            case POST     : postamble();                    break;
            case POST_POST: postpostamble();                break;
        }
    }

} /* read_dvi_file */


/*----------------------------------------------------------------------------*/


void bop()
{
    int i;

    for (i=0; i < 11; i++) get4();

} /* bop */


/*---------------------------------------------------------------------------*/

void postamble()
{
    sget4();
    if (Numerator     != get4() ||
        Denominator   != get4() ||
        Magnification != get4()){

        printf("Postamble does not match preamble\n");
        exit(3);
    }
    get4();
    get4();
    get2();
    get2();

} /* postamble */

void preamble()
{
    long ix;

    get1();
    Numerator     = get4();
    Denominator   = get4();
    Magnification = get4();
    ix = (long)(Scale*1000);    /* truncate at third decimal place */
    Scale = ((double)ix)/1000;
    printf("\nmagnification: %ld   scale: %05.3f\n\n", Magnification, Scale);
    get1();

} /* preamble */


void postpostamble()
{
    register int i;

    get4();
    get1();
    while ((i = (int) get1()) == TRAILER) ;
    while (i != EOF) {
        printf("%06ld: ", pc - 1);
        printf("BAD DVI FILE END: 0x%02X\n", i);
        i = (int) get1();
    }

} /* postpostamble */



void special(x)
register int x;
{
    register long len;
    register long i;

    len = num(x);
    for (i = 0; i < len; i++) get1();

} /* special */



void fontdef(x)
register int x;
{
    register int ix, jx;
    long scale, design;
    double mag;
    char fontname[10], fontmag[10];

    num(x);
    get4();
    scale = get4();
    design = get4();
    if (0==design) mag = -1;
    else{
        mag = (double)scale/(double)design;
        mag = Scale*Magnification*mag/1000;
    }
    sprintf(fontmag, "%07.3f", mag);
    ix = (int) get1() + (int) get1();

#if defined(MSDOS)
    #define LIMIT8   && jx < 8
#else
    #define LIMIT8
#endif

    for (jx=0; jx < ix LIMIT8; jx++) fontname[jx] = (char) get1();
    fontname[jx] = 0;
    for (; jx < ix; jx++) get1();
    addfontdef(fontname, fontmag);

} /* fontdef */



unsigned long num(size)
register int size;
{
    register int i;
    register long x = 0;

    pc += size;
    for (i = 0; i < size; i++)
        x = (x << 8) + (unsigned) getc(dvifp);
    return x;

} /* num */



long snum(size)
register int size;
{
    register int i;
    register long x = 0;

    pc += size;
    x = getc(dvifp);
    if (x & 0x80)
        x -= 0x100;
    for (i = 1; i < size; i++)
        x = (x << 8) + (unsigned) getc(dvifp);
    return x;

} /* snum */

void addfontdef(name, mag)
char *name, *mag;
{
    int ix, jx, found, base, mode;
    char *pat;

    if (FDsize < 2+FDidx){
        FDsize = 30+FDidx;
        Fontdefs = Realloc(Fontdefs, FDsize * sizeof Fontdefs[0]);
    }
    for (ix=0; ix < FDidx; ix++){
        if (!strcmp(Fontdefs[ix]->name, name) &&
            !strcmp(Fontdefs[ix]->mag, mag)){
            return;
        }
    }
    for (found=ix=0; ix < FPidx; ix++){
        pat = FPatterns[ix]->pattern;
        for (jx=0; pat[jx] && pat[jx]==name[jx]; jx++);
        if (pat[jx]=='*' || !strcmp(pat, name)){
            base = FPatterns[ix]->base;
            mode = FPatterns[ix]->mode;
            found = 1;
            break;
        }
    }
    if (!found){
        printf("Font %s not found in .CFG file\n", name);
        exit(3);
    }
    Fontdefs[FDidx] = Realloc(NULL, sizeof(FONTDEF));
    Fontdefs[FDidx]->name = Strdup(name);
    Fontdefs[FDidx]->mag  = Strdup(mag);
    Fontdefs[FDidx]->base = base;
    Fontdefs[FDidx]->mode = mode;
    FDidx++;
    printf("font: %-8s mag: %s  base: %-8s mode: %s\n",
     name, mag, BAses[base], MOdes[mode]);

} /* addfontdef */

void *Realloc(ptr, size)
void *ptr;
int size;
{
    void *outpntr;

    if (size < 0) exit(11);
    outpntr = realloc(ptr, size);
    if (0==outpntr){
        printf("Memory error\n");
        exit(4);
    }
    return outpntr;

} /* Realloc */

char *Strdup(str)
char *str;
{
    char *newstr;

    newstr = strdup(str);
    if (0==newstr){
        printf("Strdup error\n");
        exit(4);
    }
    return newstr;

} /* Strdup */


char Buf80[81];
FILE *Cfp;

void read_config_file(name)
char *name;
{
int ix, sl;

    sl = strlen(name);  if (sl >= (80-5)) exit(10);  strcpy(Buf80, name);
    for (ix = sl-1; ix > 0 && ix > sl-5; ix--){
        if (Buf80[ix]=='.'){
            strcpy(Buf80+ix+1, "CFG");
            Cfp = fopen(Buf80, "r");  if (0==Cfp){
                printf("Can't open %s\n", Buf80);
                exit(3);
            }
            read_config_80(); MFjobcmd   = Strdup(Buf80);
            read_config_80(); Inputmodes = Strdup(Buf80);
            for (;;){
                read_config_80();

                if (!strcmp(Buf80, "*end")){
                    fclose(Cfp);
                    return;
                }
                if (!strcmp(Buf80, "*base")){
                    read_config_80();
                    BAses[BAidx++] = Strdup(Buf80);
                    if (BAidx >= MaxBAidx){
                        printf("Too many bases\n");
                        exit(3);
                    }
                    continue;
                }
                if (!strcmp(Buf80, "*mode")){
                    read_config_80();
                    MOdes[MOidx++] = Strdup(Buf80);
                    if (MOidx >= MaxMOidx){
                        printf("Too many modes\n");
                        exit(3);
                    }
                    continue;
                }
                if (!BAidx){
                    printf("Missing *base command\n");
                    exit(3);
                }
                if (!MOidx){
                    printf("Missing *mode command\n");
                    exit(3);
                }
                addpatterndef(Buf80, BAidx-1, MOidx-1);
            }
        }
    }
    printf("Missing *end command\n");
    exit(3);

} /* read_config_file */

void read_config_80()
{
    if (1==fscanf(Cfp, " %80s", Buf80)) return;
    printf("Can't read config file\n");
    exit(3);
}

void addpatterndef(pattern, base, mode)
char *pattern;
int base, mode;
{
    if (FPsize < 2+FPidx){
        FPsize = 10+FPidx;
        FPatterns = Realloc(FPatterns, FPsize * sizeof FPatterns[0]);
    }
    FPatterns[FPidx] = Realloc(NULL, sizeof(FONTDEF));
    FPatterns[FPidx]->pattern = Strdup(pattern);
    FPatterns[FPidx]->base    = base;
    FPatterns[FPidx]->mode    = mode;
    FPidx++;

} /* addpatterndef */

void write_mfj()
{
    int ix, jx, mx, nx, flag;
    FILE *fp;

    fp = fopen(Outname, "w");
    if (0==fp){
        printf("Can't create %s\n", Outname);
        exit(3);
    }
    fprintf(fp, "%s\n", Inputmodes);
    for (mx=0; mx < MOidx; mx++){
        for (jx=0; jx < BAidx; jx++){
            flag = 0;
            for (ix=0; ix < FDidx; ix++){
                if (mx != Fontdefs[ix]->mode) continue;
                if (jx != Fontdefs[ix]->base) continue;
                if (!flag){
                    fprintf(fp, "{\n  base=%s;\n  %s;\n",
                     BAses[jx], MOdes[mx]);
                    flag = 1;
                }
                nx = fprintf(fp, "  { font=%s;", Fontdefs[ix]->name);
                fprintf(fp, "%*smag=%s; }\n", 21-nx, " ", Fontdefs[ix]->mag);
            }
            if (flag) fprintf(fp, "}\n");
        }
    }
    fclose(fp);

} /* write_mfj */

void run_mfjob()
{
    int retcode;

    if (Nomfj) return;
    printf("\nSpawning %s %s\n\n", MFjobcmd, Outname);
    retcode = spawnl(P_WAIT, MFjobcmd, MFjobcmd, Outname, NULL);
    if (0==retcode){
       printf("\nDeleting %s\n", Outname);
       unlink(Outname);
    }

} /* run_mfjob */



/*


================================================================================
==                          DVI file format                                   ==
================================================================================
no_ops          >= 0 bytes     (NOP, nops before the preamble)
preamble_marker    1 ubyte     (PRE)
version_id         1 ubyte     (should be version 2)
numerator          4 ubytes    (numerater must equal the one in postamble)
denominator        4 ubytes    (denominator must equal the one in postamble)
magnification      4 ubytes    (magnification must equal the one in postamble)
id_len             1 ubyte     (lenght of identification string)
id_string     id_len ubytes    (identification string)

no_ops          >= 0 bytes     (NOP, nops before a page)
begin_of_page      1 ubyte     (BOP)
page_nr            4 sbytes    (page number)
do_be_do          36 bytes     (filler ????)
prev_page_offset   4 sbytes    (offset in file where previous page starts, -1 for none)
... PAGE DATA ...
end_of_page        1 ubyte     (EOP)

no_ops ???      >= 0 bytes     (NOPS, I think they are allowed here...)
postamble_marker   1 ubyte     (POST)
last_page_offset   4 sbytes    (offset in file where last page starts)
numerator          4 ubytes    (numerater must equal the one in preamble)
denominator        4 ubytes    (denominator must equal the one in preamble)
magnification      4 ubytes    (magnification must equal the one in preamble)
max_page_height    4 ubytes    (maximum page height)
max_page_width     4 ubytes    (maximum page width)
max_stack          2 ubytes    (maximum stack depth needed)
total_pages        2 ubytes    (number of pages in file)
... FONT DEFINITIONS ...

postamble_offset   4 sbytes    (offset in file where postamble starts)
version_id         1 ubyte     (should be version 2)
trailer         >= 4 ubytes    (TRAILER)
<EOF>


FONT DEFINITIONS:
   do {
      switch (c = getc(dvi_fp) {
          case FNTDEF1  :
          case FNTDEF2  :
          case FNTDEF3  :
          case FNTDEF4  :  define_font(c);
          case POSTPOST : break;
          default       : error;
      }
   } while (c != POSTPOST);

===== A font def looks like:

1,2,3 or 4 ubytes TeXfontnumber for FNTDEF1 .. FNTDEF4
4 ubytes checksum
4 ubytes scale
4 ubytes design size
1 byte deflen1
1 byte deflen2
deflen1 + deflen2 bytes fontname.

===== A special looks like:

1,2,3 or 4 ubytes telling length of special command for XXX1 .. XXX4
all bytes in the special command are used as defined in the dvi driver.


*/