home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / x / volume17 / xdvi / part02 < prev    next >
Encoding:
Text File  |  1992-03-22  |  49.5 KB  |  1,945 lines

  1. Newsgroups: comp.sources.x
  2. Path: uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!mips!msi!dcmartin
  3. From: vojta@powdermilk.berkeley.edu (Paul Vojta)
  4. Subject: v17i024: xdvi, dvi previewer, Part02/05
  5. Message-ID: <1992Mar23.173021.15100@msi.com>
  6. Originator: dcmartin@fascet
  7. Sender: dcmartin@msi.com (David C. Martin - Moderator)
  8. Organization: Molecular Simulations, Inc.
  9. References: <csx-17i023-xdvi@uunet.UU.NET>
  10. Date: Mon, 23 Mar 1992 17:30:21 GMT
  11. Approved: dcmartin@msi.com
  12.  
  13. Submitted-by: vojta@powdermilk.berkeley.edu (Paul Vojta)
  14. Posting-number: Volume 17, Issue 24
  15. Archive-name: xdvi/part02
  16.  
  17. #! /bin/sh
  18. # This is a shell archive.  Remove anything before this line, then unpack
  19. # it by saving it into a file and typing "sh file".  To overwrite existing
  20. # files, type "sh file -c".  You can also feed this as standard input via
  21. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  22. # will see the following message at the end:
  23. #        "End of shell archive."
  24. # Contents:  dvi.h patchlevel.h xdvi.h xdvi_curs.h xdvi_mask.h
  25. #   dvi_draw.c dvi_init.c
  26. # Wrapped by vojta@powdermilk.berkeley.edu on Tue Mar 17 17:46:59 1992
  27. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  28. if test -f 'dvi.h' -a "${1}" != "-c" ; then 
  29.   echo shar: Will not clobber existing file \"'dvi.h'\"
  30. else
  31. echo shar: Extracting \"'dvi.h'\" \(1066 characters\)
  32. sed "s/^X//" >'dvi.h' <<'END_OF_FILE'
  33. X/*
  34. X *    Mnemonics for bytes in dvi file.
  35. X */
  36. X
  37. X#define    SETCHAR0    0
  38. X#define    SET1        128
  39. X#define    SETRULE        132
  40. X#define    PUT1        133
  41. X#define    PUTRULE        137
  42. X#define    NOP        138
  43. X#define    BOP        139
  44. X#define    EOP        140
  45. X#define    PUSH        141
  46. X#define    POP        142
  47. X#define    RIGHT1        143
  48. X#define    RIGHT2        144
  49. X#define    RIGHT3        145
  50. X#define    RIGHT4        146
  51. X#define    W0        147
  52. X#define    W1        148
  53. X#define    W2        149
  54. X#define    W3        150
  55. X#define    W4        151
  56. X#define    X0        152
  57. X#define    X1        153
  58. X#define    X2        154
  59. X#define    X3        155
  60. X#define    X4        156
  61. X#define    DOWN1        157
  62. X#define    DOWN2        158
  63. X#define    DOWN3        159
  64. X#define    DOWN4        160
  65. X#define    Y0        161
  66. X#define    Y1        162
  67. X#define    Y2        163
  68. X#define    Y3        164
  69. X#define    Y4        165
  70. X#define    Z0        166
  71. X#define    Z1        167
  72. X#define    Z2        168
  73. X#define    Z3        169
  74. X#define    Z4        170
  75. X#define    FNTNUM0        171
  76. X#define    FNT1        235
  77. X#define    FNT2        236
  78. X#define    FNT3        237
  79. X#define    FNT4        238
  80. X#define    XXX1        239
  81. X#define    XXX2        240
  82. X#define    XXX3        241
  83. X#define    XXX4        242
  84. X#define    FNTDEF1        243
  85. X#define    FNTDEF2        244
  86. X#define    FNTDEF3        245
  87. X#define    FNTDEF4        246
  88. X#define    PRE        247
  89. X#define    POST        248
  90. X#define    POSTPOST    249
  91. X
  92. X#define    TRAILER        223    /* Trailing bytes at end of file */
  93. END_OF_FILE
  94. if test 1066 -ne `wc -c <'dvi.h'`; then
  95.     echo shar: \"'dvi.h'\" unpacked with wrong size!
  96. fi
  97. # end of 'dvi.h'
  98. fi
  99. if test -f 'patchlevel.h' -a "${1}" != "-c" ; then 
  100.   echo shar: Will not clobber existing file \"'patchlevel.h'\"
  101. else
  102. echo shar: Extracting \"'patchlevel.h'\" \(22 characters\)
  103. sed "s/^X//" >'patchlevel.h' <<'END_OF_FILE'
  104. X#define PATCHLEVEL 12
  105. END_OF_FILE
  106. if test 22 -ne `wc -c <'patchlevel.h'`; then
  107.     echo shar: \"'patchlevel.h'\" unpacked with wrong size!
  108. fi
  109. # end of 'patchlevel.h'
  110. fi
  111. if test -f 'xdvi.h' -a "${1}" != "-c" ; then 
  112.   echo shar: Will not clobber existing file \"'xdvi.h'\"
  113. else
  114. echo shar: Extracting \"'xdvi.h'\" \(11773 characters\)
  115. sed "s/^X//" >'xdvi.h' <<'END_OF_FILE'
  116. X/*
  117. X *    Written by Eric C. Cooper, CMU
  118. X */
  119. X
  120. X/********************************
  121. X *    The C environment    *
  122. X *******************************/
  123. X
  124. X#ifndef X10
  125. X#include <X11/Xlib.h>    /* include Xfuncs.h, if available */
  126. X#include <X11/Xos.h>    /* same as below */
  127. X#else    /* X10 */
  128. X#include <X/Xlib.h>    /* get type of Window */
  129. X#include <sys/types.h>    /* for sites without X11 */
  130. X#ifdef    SYSV
  131. X#include <string.h>
  132. X#define    index    strchr
  133. X#define    rindex    strrchr
  134. X#include <fcntl.h>
  135. X#else /* SYSV */
  136. X#include <strings.h>
  137. X#endif /* SYSV */
  138. X#include <sys/file.h>
  139. X#endif    /* X10 */
  140. X
  141. X#ifdef    VMS
  142. X#include <string.h>
  143. X#define    index    strchr
  144. X#define    rindex    strrchr
  145. X#define    bzero(a, b)    (void) memset ((void *) (a), 0, (size_t) (b))
  146. X#define bcopy(a, b, c)  (void) memmove ((void *) (b), (void *) (a), (size_t) (c))
  147. X#endif
  148. X
  149. X#include <stdio.h>
  150. X#include <setjmp.h>
  151. X
  152. X#ifndef    OPEN_MODE
  153. X#ifndef    VMS
  154. X#define    OPEN_MODE    "r"
  155. X#else    /* VMS */
  156. X#define    OPEN_MODE    "r", "ctx=stm"
  157. X#endif    /* VMS */
  158. X#endif    /* OPEN_MODE */
  159. X
  160. X#ifndef    SYSV
  161. X#ifndef    VMS
  162. X#define    HAS_SIGIO        /* has SIGIO on _sockets_ */
  163. X#endif
  164. X#endif
  165. X
  166. X#ifndef    NeedFunctionPrototypes
  167. X#ifdef    __STDC__
  168. X#define    NeedFunctionPrototypes    1
  169. X#else    /* STDC */
  170. X#define    NeedFunctionPrototypes    0
  171. X#endif    /* STDC */
  172. X#endif    /* NeedFunctionPrototypes */
  173. X
  174. X#ifndef    NeedWidePrototypes
  175. X#define    NeedWidePrototypes    NeedFunctionPrototypes
  176. X#endif
  177. X
  178. X#if    NeedWidePrototypes
  179. X#define    WIDEARG(a, b)    b
  180. X#else
  181. X#define    WIDEARG(a, b)    a
  182. X#endif
  183. X
  184. X#ifndef    NeedVarargsPrototypes
  185. X#define    NeedVarargsPrototypes    NeedFunctionPrototypes
  186. X#endif
  187. X
  188. X#ifndef    _XFUNCPROTOBEGIN
  189. X#define    _XFUNCPROTOBEGIN
  190. X#define    _XFUNCPROTOEND
  191. X#endif
  192. X
  193. X#ifndef    _Xconst
  194. X#ifdef    __STDC__
  195. X#define    _Xconst    const
  196. X#else    /* STDC */
  197. X#define    _Xconst
  198. X#endif    /* STDC */
  199. X#endif    /* _Xconst */
  200. X
  201. X#ifndef    __GNUC__
  202. X#define    volatile
  203. X#endif
  204. X
  205. X#define    Printf    (void) printf
  206. X#define    Fprintf    (void) fprintf
  207. X#define    Sprintf    (void) sprintf
  208. X#define    Fseek    (void) fseek
  209. X#define    Fread    (void) fread
  210. X#define    Fputs    (void) fputs
  211. X#define    Putc    (void) putc
  212. X#define    Putchar    (void) putchar
  213. X#define    Fclose    (void) fclose
  214. X#define    Strcpy    (void) strcpy
  215. X
  216. X/********************************
  217. X *     Types and data        *
  218. X *******************************/
  219. X
  220. X#ifndef    EXTERN
  221. X#define    EXTERN    extern
  222. X#define    INIT(x)
  223. X#endif
  224. X
  225. Xtypedef    unsigned char ubyte;
  226. X#define    Boolean    char
  227. X#define    True    1
  228. X#define    False    0
  229. X
  230. X#define    MAXDIM        32767
  231. X
  232. X/*
  233. X *    pixel_conv is currently used only for converting absolute positions
  234. X *    to pixel values; although normally it should be
  235. X *        ((int) ((x) / shrink_factor + (1 << 15) >> 16)),
  236. X *    the rounding is achieved instead by moving the constant 1 << 15 to
  237. X *    PAGE_OFFSET in dvi_draw.c.
  238. X */
  239. X#define    pixel_conv(x)        ((int) ((x) / shrink_factor >> 16))
  240. X#define    pixel_round(x)        ((int) ROUNDUP(x, shrink_factor << 16))
  241. X#define    spell_conv0(n, f)    ((long) (n * f))
  242. X#define    spell_conv(n)        spell_conv0(n, dimconv)
  243. X
  244. X#ifdef X10
  245. X#undef    MSBITFIRST
  246. X#undef    BMLONG
  247. X#define    BMSHORT
  248. X#endif
  249. X
  250. X#ifdef    BMLONG
  251. X#define    BMUNIT            unsigned long
  252. X#define    BITS_PER_BMUNIT        32
  253. X#define    BYTES_PER_BMUNIT    4
  254. X#else    /* BMLONG */
  255. X#ifdef    BMSHORT
  256. X#define    BMUNIT            unsigned short
  257. X#define    BITS_PER_BMUNIT        16
  258. X#define    BYTES_PER_BMUNIT    2
  259. X#else    /* BMSHORT */
  260. X#define    BMUNIT            unsigned char
  261. X#define    BITS_PER_BMUNIT        8
  262. X#define    BYTES_PER_BMUNIT    1
  263. X#endif    /* BMSHORT */
  264. X#endif    /* BMLONG */
  265. X
  266. X#define    ADD(a, b)    ((BMUNIT *) (((char *) a) + b))
  267. X#define    SUB(a, b)    ((BMUNIT *) (((char *) a) - b))
  268. X
  269. Xextern    BMUNIT    bit_masks[BITS_PER_BMUNIT + 1];
  270. X
  271. Xstruct frame {
  272. X    struct framedata {
  273. X        long dvi_h, dvi_v, w, x, y, z;
  274. X        int pxl_v;
  275. X    } data;
  276. X    struct frame *next, *prev;
  277. X};
  278. X
  279. X#if    NeedFunctionPrototypes
  280. Xtypedef    long    (*set_char_proc)(WIDEARG(ubyte,int));
  281. X#else
  282. Xtypedef    long    (*set_char_proc)();
  283. X#endif
  284. X
  285. Xstruct drawinf {    /* this information is saved when using virtual fonts */
  286. X    struct framedata data;
  287. X    struct font    *fontp;
  288. X    set_char_proc    set_char_p;
  289. X    struct tn    *tn_head;
  290. X    ubyte        *pos, *end;
  291. X    Boolean        virtual;
  292. X};
  293. X
  294. XEXTERN    struct drawinf    currinf;
  295. X
  296. X/* entries below with the characters 'dvi' in them are actually stored in
  297. X   scaled pixel units */
  298. X
  299. X#define DVI_H   currinf.data.dvi_h
  300. X#define PXL_H   pixel_conv(currinf.data.dvi_h)
  301. X#define DVI_V   currinf.data.dvi_v
  302. X#define PXL_V   currinf.data.pxl_v
  303. X#define WW      currinf.data.w
  304. X#define XX      currinf.data.x
  305. X#define YY      currinf.data.y
  306. X#define ZZ      currinf.data.z
  307. X#define ROUNDUP(x,y) (((x)+(y)-1)/(y))
  308. X
  309. XEXTERN    int    current_page;
  310. XEXTERN    int    total_pages;
  311. XEXTERN    double    dimconv;
  312. XEXTERN    int    n_files_left    INIT(32767);    /* for LRU closing of fonts */
  313. XEXTERN    time_t    dvi_time;        /* last mod. time for dvi file */
  314. XEXTERN    int    page_w, page_h;
  315. X
  316. X/*
  317. X * Table of page offsets in DVI file, indexed by page number - 1.
  318. X * Initialized in prepare_pages().
  319. X */
  320. XEXTERN    long    *page_offset;
  321. X
  322. X/*
  323. X * Mechanism for reducing repeated warning about specials, lost characters, etc.
  324. X */
  325. XEXTERN    Boolean    hush_spec    INIT(False);
  326. XEXTERN    Boolean    hush_spec_now;
  327. XEXTERN    Boolean    hush_chars    INIT(False);
  328. X
  329. X
  330. X/*
  331. X * Bitmap structure for raster ops.
  332. X */
  333. Xstruct bitmap {
  334. X    short w, h;        /* width and height in pixels */
  335. X    short bytes_wide;    /* scan-line width in bytes */
  336. X    char *bits;        /* pointer to the bits */
  337. X};
  338. X
  339. X/*
  340. X * Per-character information.
  341. X * There is one of these for each character in a font (raster fonts only).
  342. X * All fields are filled in at font definition time,
  343. X * except for the bitmap, which is "faulted in"
  344. X * when the character is first referenced.
  345. X */
  346. Xstruct glyph {
  347. X    long addr;        /* address of bitmap in font file */
  348. X    long dvi_adv;        /* DVI units to move reference point */
  349. X    short x, y;        /* x and y offset in pixels */
  350. X    struct bitmap bitmap;    /* bitmap for character */
  351. X    short x2, y2;        /* x and y offset in pixels (shrunken bitmap) */
  352. X    struct bitmap bitmap2;    /* shrunken bitmap for character */
  353. X};
  354. X
  355. X/*
  356. X * Per character information for virtual fonts
  357. X */
  358. Xstruct macro {
  359. X    ubyte    *pos;        /* address of first byte of macro */
  360. X    ubyte    *end;        /* address of last+1 byte */
  361. X    long    dvi_adv;    /* DVI units to move reference point */
  362. X    Boolean    free_me;    /* if free(pos) should be called when */
  363. X                /* freeing space */
  364. X};
  365. X
  366. X/*
  367. X * The layout of a font information block.
  368. X * There is one of these for every loaded font or magnification thereof.
  369. X * Duplicates are eliminated:  this is necessary because of possible recursion
  370. X * in virtual fonts.
  371. X *
  372. X * Also note the strange units.  The design size is in 1/2^20 point
  373. X * units (also called micro-points), and the individual character widths
  374. X * are in the TFM file in 1/2^20 ems units, i.e., relative to the design size.
  375. X *
  376. X * We then change the sizes to SPELL units (unshrunk pixel / 2^16).
  377. X */
  378. X
  379. X#if    NeedFunctionPrototypes
  380. Xtypedef    void (*read_char_proc)(struct font *, WIDEARG(ubyte,int));
  381. X#else
  382. Xtypedef    void (*read_char_proc)();
  383. X#endif
  384. X
  385. Xstruct font {
  386. X    struct font *next;        /* link to next font info block */
  387. X    char *fontname;            /* name of font */
  388. X    float fsize;            /* size information (dots per inch) */
  389. X    FILE *file;            /* open font file or NULL */
  390. X    char *filename;            /* name of font file */
  391. X    unsigned short timestamp;    /* for LRU management of fonts */
  392. X    ubyte flags;            /* flags byte (see values below) */
  393. X    ubyte maxchar;            /* largest character code */
  394. X    double dimconv;            /* size conversion factor */
  395. X    set_char_proc set_char_p;    /* proc used to set char */
  396. X        /* these fields are used by (loaded) raster fonts */
  397. X    read_char_proc read_char;    /* function to read bitmap */
  398. X    struct glyph *glyph;
  399. X        /* these fields are used by (loaded) virtual fonts */
  400. X    struct tn *vf_chain;        /* list of fonts used by this vf */
  401. X    struct font *first_font;    /* first font defined */
  402. X    struct macro *macro;
  403. X        /* I suppose the above could be put into a union, but we */
  404. X        /* wouldn't save all that much space. */
  405. X};
  406. X
  407. X#define    FONT_IN_USE    1    /* used for housekeeping */
  408. X#define    FONT_LOADED    2    /* if font file has been read */
  409. X#define    FONT_VIRTUAL    4    /* if font is virtual */
  410. X
  411. Xstruct tn {
  412. X    struct tn *next;        /* link to next TeXnumber info block */
  413. X    int TeXnumber;            /* font number (in DVI file) */
  414. X    struct font *fontp;        /* pointer to the rest of the info */
  415. X};
  416. X
  417. XEXTERN    struct font    *font_head    INIT(NULL);
  418. XEXTERN    struct tn    *tn_head    INIT(NULL);
  419. XEXTERN    ubyte        maxchar;
  420. XEXTERN    unsigned short    current_timestamp INIT(0);
  421. X
  422. X/*
  423. X * Command line flags.
  424. X */
  425. X
  426. XEXTERN    int    debug    INIT(0);
  427. X
  428. X#define    DBG_BITMAP    0x1
  429. X#define    DBG_DVI        0x2
  430. X#define    DBG_PK        0x4
  431. X#define    DBG_BATCH    0x8
  432. X#define    DBG_EVENT    0x10
  433. X#define    DBG_OPEN    0x20
  434. X#define    DBG_ALL        (DBG_BITMAP|DBG_DVI|DBG_PK|DBG_EVENT|DBG_OPEN)
  435. X
  436. XEXTERN    Boolean    list_fonts    INIT(False);
  437. X
  438. XEXTERN    int    pixels_per_inch    INIT(300);
  439. XEXTERN    int    offset_x, offset_y;
  440. XEXTERN    int    unshrunk_paper_w, unshrunk_paper_h;
  441. XEXTERN    int    unshrunk_page_w, unshrunk_page_h;
  442. XEXTERN    int    density        INIT(40);
  443. XEXTERN    double    specialConv;
  444. X
  445. XEXTERN    char    *dvi_name    INIT(NULL);
  446. XEXTERN    FILE    *dvi_file;                /* user's file */
  447. XEXTERN    _Xconst    char    *alt_font    INIT(ALTFONT);
  448. XEXTERN    char    *prog;
  449. X
  450. Xstruct    WindowRec {
  451. X    Window    win;
  452. X    int    shrinkfactor;
  453. X    int    base_x, base_y;
  454. X    int    width, height;
  455. X    int    min_x, max_x, min_y, max_y;    /* for pending expose events */
  456. X};
  457. X
  458. Xextern    struct WindowRec mane, alt, currwin;
  459. X
  460. X#define    WINDOW(wr)    ((Window) (wr).win)
  461. X#define    shrink_factor    currwin.shrinkfactor
  462. X
  463. XEXTERN    jmp_buf    dvi_env;    /* mechanism to communicate dvi file errors */
  464. X
  465. X/********************************
  466. X *       Procedures        *
  467. X *******************************/
  468. X
  469. X_XFUNCPROTOBEGIN
  470. X#if    NeedFunctionPrototypes
  471. X
  472. Xextern    void    line_btw(int, int, int, int);
  473. Xextern    void    dot_at(int, int);
  474. Xextern    void    do_attribute_path(int, int, int, int);
  475. Xextern    void    put_bitmap(struct bitmap *, int, int);
  476. Xextern    void    put_rectangle(int, int, int, int, WIDEARG(Boolean, int));
  477. Xextern    void    redraw_page(void);
  478. X#if    NeedVarargsPrototypes
  479. Xextern    volatile void    oops(_Xconst char *, ...);
  480. X#else
  481. Xextern    volatile void    oops();
  482. X#endif
  483. Xextern    char    *xmalloc(unsigned, _Xconst char *);
  484. Xextern    void    alloc_bitmap(struct bitmap *);
  485. Xextern    FILE    *xfopen(_Xconst char *);
  486. Xextern    unsigned long    num(FILE *, WIDEARG(ubyte, int));
  487. Xextern    long    snum(FILE *, WIDEARG(ubyte, int));
  488. Xextern    void    reset_fonts(void);
  489. Xextern    void    realloc_font(struct font *, WIDEARG(ubyte, int));
  490. Xextern    void    realloc_virtual_font(struct font *, WIDEARG(ubyte, int));
  491. Xextern    void    load_font(struct font *);
  492. Xextern    void    define_font(FILE *, WIDEARG(ubyte, unsigned int), struct font *,
  493. X            struct tn **);
  494. Xextern    void    init_page(void);
  495. Xextern    void    open_dvi_file(void);
  496. Xextern    Boolean    check_dvi_file(void);
  497. Xextern    long    set_char(WIDEARG(ubyte, int));
  498. Xextern    long    set_vf_char(WIDEARG(ubyte, int));
  499. Xextern    void    draw_page(void);
  500. Xextern    void    init_font_open(void);
  501. Xextern    FILE    *font_open(_Xconst char *, char **,
  502. X            WIDEARG(float, double), int *, char **);
  503. Xextern    void    applicationDoSpecial(char *);
  504. Xextern    void    read_PK_index(struct font *);
  505. Xextern    void    read_GF_index(struct font *);
  506. Xextern    void    read_PXL_index(struct font *);
  507. Xextern    void    read_VF_index(struct font *);
  508. X
  509. X#else    /* ! NeedFunctionPrototypes */
  510. X
  511. Xextern    void    line_btw();
  512. Xextern    void    dot_at();
  513. Xextern    void    do_attribute_path();
  514. Xextern    void    put_bitmap();
  515. Xextern    void    put_rectangle();
  516. Xextern    void    redraw_page();
  517. Xextern    volatile void    oops();
  518. Xextern    char    *xmalloc();
  519. Xextern    void    alloc_bitmap();
  520. Xextern    FILE    *xfopen();
  521. Xextern    unsigned long    num();
  522. Xextern    long    snum();
  523. Xextern    void    reset_fonts();
  524. Xextern    void    realloc_font();
  525. Xextern    void    realloc_virtual_font();
  526. Xextern    void    load_font();
  527. Xextern    void    define_font();
  528. Xextern    void    init_page();
  529. Xextern    void    open_dvi_file();
  530. Xextern    Boolean    check_dvi_file();
  531. Xextern    long    set_char();
  532. Xextern    long    set_vf_char();
  533. Xextern    void    draw_page();
  534. Xextern    void    init_font_open();
  535. Xextern    FILE    *font_open();
  536. Xextern    void    applicationDoSpecial();
  537. Xextern    void    read_PK_index();
  538. Xextern    void    read_GF_index();
  539. Xextern    void    read_PXL_index();
  540. Xextern    void    read_VF_index();
  541. X
  542. X#endif    /* NeedFunctionPrototypes */
  543. X
  544. X#define one(fp)        ((unsigned char) getc(fp))
  545. X#define sone(fp)    ((long) one(fp))
  546. X#define two(fp)        num (fp, 2)
  547. X#define stwo(fp)    snum(fp, 2)
  548. X#define four(fp)    num (fp, 4)
  549. X#define sfour(fp)    snum(fp, 4)
  550. X
  551. X_XFUNCPROTOEND
  552. END_OF_FILE
  553. if test 11773 -ne `wc -c <'xdvi.h'`; then
  554.     echo shar: \"'xdvi.h'\" unpacked with wrong size!
  555. fi
  556. # end of 'xdvi.h'
  557. fi
  558. if test -f 'xdvi_curs.h' -a "${1}" != "-c" ; then 
  559.   echo shar: Will not clobber existing file \"'xdvi_curs.h'\"
  560. else
  561. echo shar: Extracting \"'xdvi_curs.h'\" \(249 characters\)
  562. sed "s/^X//" >'xdvi_curs.h' <<'END_OF_FILE'
  563. X#define xdvi_width 15
  564. X#define xdvi_height 15
  565. X#define xdvi_x_hot 7
  566. X#define xdvi_y_hot 7
  567. Xstatic short xdvi_bits[] = {
  568. X   0x0080, 0x01c0, 0x03e0, 0x06b0,
  569. X   0x0c98, 0x188c, 0x3086, 0x7fff,
  570. X   0x3086, 0x188c, 0x0c98, 0x06b0,
  571. X   0x03e0, 0x01c0, 0x0080};
  572. END_OF_FILE
  573. if test 249 -ne `wc -c <'xdvi_curs.h'`; then
  574.     echo shar: \"'xdvi_curs.h'\" unpacked with wrong size!
  575. fi
  576. # end of 'xdvi_curs.h'
  577. fi
  578. if test -f 'xdvi_mask.h' -a "${1}" != "-c" ; then 
  579.   echo shar: Will not clobber existing file \"'xdvi_mask.h'\"
  580. else
  581. echo shar: Extracting \"'xdvi_mask.h'\" \(222 characters\)
  582. sed "s/^X//" >'xdvi_mask.h' <<'END_OF_FILE'
  583. X#define xdvi_mask_width 15
  584. X#define xdvi_mask_height 15
  585. Xstatic short xdvi_mask_bits[] = {
  586. X   0x01c0, 0x03e0, 0x07f0, 0x0ff8,
  587. X   0x1ffc, 0x3ffe, 0x7fff, 0x7fff,
  588. X   0x7fff, 0x3ffe, 0x1ffc, 0x0ff8,
  589. X   0x07f0, 0x03e0, 0x01c0};
  590. END_OF_FILE
  591. if test 222 -ne `wc -c <'xdvi_mask.h'`; then
  592.     echo shar: \"'xdvi_mask.h'\" unpacked with wrong size!
  593. fi
  594. # end of 'xdvi_mask.h'
  595. fi
  596. if test -f 'dvi_draw.c' -a "${1}" != "-c" ; then 
  597.   echo shar: Will not clobber existing file \"'dvi_draw.c'\"
  598. else
  599. echo shar: Extracting \"'dvi_draw.c'\" \(16733 characters\)
  600. sed "s/^X//" >'dvi_draw.c' <<'END_OF_FILE'
  601. X/*
  602. X * DVI previewer for X.
  603. X *
  604. X * Eric Cooper, CMU, September 1985.
  605. X *
  606. X * Code derived from dvi-imagen.c.
  607. X *
  608. X * Modification history:
  609. X * 1/1986    Modified for X.10 by Bob Scheifler, MIT LCS.
  610. X * 7/1988    Modified for X.11 by Mark Eichin, MIT
  611. X * 12/1988    Added 'R' option, toolkit, magnifying glass
  612. X *            --Paul Vojta, UC Berkeley.
  613. X * 2/1989    Added tpic support    --Jeffrey Lee, U of Toronto
  614. X * 4/1989    Modified for System V by Donald Richardson, Clarkson Univ.
  615. X * 3/1990    Added VMS support    --Scott Allendorf, U of Iowa
  616. X *
  617. X *    Compilation options:
  618. X *    SYSV    compile for System V
  619. X *    VMS    compile for VMS
  620. X *    X10    compile for X10
  621. X *    NOTOOL    compile without toolkit (X11 only)
  622. X *    BUTTONS    compile with buttons on the side of the window (needs toolkit)
  623. X *    MSBITFIRST    store bitmaps internally with most significant bit first
  624. X *    BMSHORT    store bitmaps in shorts instead of bytes
  625. X *    BMLONG    store bitmaps in longs instead of bytes
  626. X *    ALTFONT    default for -altfont option
  627. X *    A4    use European size paper
  628. X */
  629. X
  630. X#include <ctype.h>
  631. X#include "xdvi.h"
  632. X#include "dvi.h"
  633. X
  634. X#ifndef    X_NOT_STDC_ENV
  635. X#include <stdlib.h>
  636. X#endif
  637. X
  638. Xstatic    struct frame    frame0;        /* dummy head of list */
  639. X
  640. X#ifndef    DVI_BUFFER_LEN
  641. X#define    DVI_BUFFER_LEN    512
  642. X#endif
  643. X
  644. Xstatic    ubyte    dvi_buffer[DVI_BUFFER_LEN];
  645. Xstatic    struct frame    *current_frame;
  646. X
  647. X/*
  648. X *    Explanation of the following constant:
  649. X *    offset_[xy]   << 16:    margin (defaults to one inch)
  650. X *    shrink_factor << 16:    one pixel page border
  651. X *    shrink_factor << 15:    rounding for pixel_conv
  652. X */
  653. X#define OFFSET_X    (offset_x << 16) + (shrink_factor * 3 << 15)
  654. X#define OFFSET_Y    (offset_y << 16) + (shrink_factor * 3 << 15)
  655. X
  656. X#ifndef    BMLONG
  657. X#ifndef    BMSHORT
  658. Xunsigned char    bit_masks[9] = {
  659. X    0x0,    0x1,    0x3,    0x7,
  660. X    0xf,    0x1f,    0x3f,    0x7f,
  661. X    0xff
  662. X};
  663. X#else    /* BMSHORT */
  664. Xunsigned short    bit_masks[17] = {
  665. X    0x0,    0x1,    0x3,    0x7,
  666. X    0xf,    0x1f,    0x3f,    0x7f,
  667. X    0xff,    0x1ff,    0x3ff,    0x7ff,
  668. X    0xfff,    0x1fff,    0x3fff,    0x7fff,
  669. X    0xffff
  670. X};
  671. X#endif    /* BMSHORT */
  672. X#else    /* BMLONG */
  673. Xunsigned long    bit_masks[33] = {
  674. X    0x0,        0x1,        0x3,        0x7,
  675. X    0xf,        0x1f,        0x3f,        0x7f,
  676. X    0xff,        0x1ff,        0x3ff,        0x7ff,
  677. X    0xfff,        0x1fff,        0x3fff,        0x7fff,
  678. X    0xffff,        0x1ffff,    0x3ffff,    0x7ffff,
  679. X    0xfffff,    0x1fffff,    0x3fffff,    0x7fffff,
  680. X    0xffffff,    0x1ffffff,    0x3ffffff,    0x7ffffff,
  681. X    0xfffffff,    0x1fffffff,    0x3fffffff,    0x7fffffff,
  682. X    0xffffffff
  683. X};
  684. X#endif    /* BMLONG */
  685. X
  686. Xextern    char    *xmalloc();
  687. Xextern    volatile void    exit();
  688. X
  689. X#ifndef VMS
  690. Xextern    off_t    lseek();
  691. X#else
  692. Xextern  int     lseek();
  693. X#endif
  694. X
  695. X#ifndef    SEEK_SET    /* if <unistd.h> is not provided (or for <X11R5) */
  696. X#define    SEEK_SET    0
  697. X#define    SEEK_CUR    1
  698. X#define    SEEK_END    2
  699. X#endif
  700. X
  701. Xstatic    void    draw_part();
  702. X
  703. Xstatic    void
  704. Xprint_bitmap(bitmap)
  705. X    register struct bitmap *bitmap;
  706. X{
  707. X    register BMUNIT *ptr = (BMUNIT *) bitmap->bits;
  708. X    register int x, y, i;
  709. X
  710. X    if (ptr == NULL) oops("print_bitmap called with null pointer.");
  711. X    Printf("w = %d, h = %d, bytes wide = %d\n",
  712. X        bitmap->w, bitmap->h, bitmap->bytes_wide);
  713. X    for (y = 0; y < bitmap->h; ++y) {
  714. X        for (x = bitmap->bytes_wide; x > 0; x -= BYTES_PER_BMUNIT) {
  715. X#ifndef    MSBITFIRST
  716. X        for (i = 0; i < BITS_PER_BMUNIT; ++i)
  717. X#else
  718. X        for (i = BITS_PER_BMUNIT - 1; i >= 0; --i)
  719. X#endif
  720. X            Putchar((*ptr & (1 << i)) ? '@' : ' ');
  721. X        ++ptr;
  722. X        }
  723. X        Putchar('\n');
  724. X    }
  725. X}
  726. X
  727. Xstatic    void
  728. Xprint_char(ch, g)
  729. X    ubyte ch;
  730. X    struct glyph *g;
  731. X{
  732. X    Printf("char %d", ch);
  733. X    if (isprint(ch))
  734. X        Printf(" (%c)", ch);
  735. X    Putchar('\n');
  736. X    Printf("x = %d, y = %d, dvi = %d\n", g->x, g->y, g->dvi_adv);
  737. X    print_bitmap(&g->bitmap);
  738. X}
  739. X
  740. Xstatic    _Xconst    char    *dvi_table1[] = {
  741. X    "SET1", NULL, NULL, NULL, "SETRULE", "PUT1", NULL, NULL,
  742. X    NULL, "PUTRULE", "NOP", "BOP", "EOP", "PUSH", "POP", "RIGHT1",
  743. X    "RIGHT2", "RIGHT3", "RIGHT4", "W0", "W1", "W2", "W3", "W4",
  744. X    "X0", "X1", "X2", "X3", "X4", "DOWN1", "DOWN2", "DOWN3",
  745. X    "DOWN4", "Y0", "Y1", "Y2", "Y3", "Y4", "Z0", "Z1",
  746. X    "Z2", "Z3", "Z4"};
  747. X
  748. Xstatic    _Xconst    char    *dvi_table2[] = {
  749. X    "FNT1", "FNT2", "FNT3", "FNT4", "XXX1", "XXX2", "XXX3", "XXX4",
  750. X    "FNTDEF1", "FNTDEF2", "FNTDEF3", "FNTDEF4", "PRE", "POST", "POSTPOST",
  751. X    NULL, NULL, NULL, NULL, NULL, NULL};
  752. X
  753. Xstatic    void
  754. Xprint_dvi(ch)
  755. X    ubyte ch;
  756. X{
  757. X    _Xconst    char    *s;
  758. X
  759. X    Printf("%4d %4d ", PXL_H, PXL_V);
  760. X    if (ch <= SETCHAR0 + 127) {
  761. X        Printf("SETCHAR%-3d", ch - SETCHAR0);
  762. X        if (isprint(ch))
  763. X        Printf(" (%c)", ch);
  764. X        Putchar('\n');
  765. X        return;
  766. X    }
  767. X    else if (ch < FNTNUM0) s = dvi_table1[ch - 128];
  768. X    else if (ch <= FNTNUM0 + 63) {
  769. X        Printf("FNTNUM%d\n", ch - FNTNUM0);
  770. X        return;
  771. X    }
  772. X    else s = dvi_table2[ch - (FNTNUM0+64)];
  773. X    if (s) puts(s);
  774. X    else oops("Unknown op-code %d, offset %d", ch, ftell(dvi_file) - 1);
  775. X}
  776. X
  777. X/*
  778. X *    Byte reading routines for dvi file.
  779. X */
  780. X
  781. Xstatic    ubyte
  782. Xxxone()
  783. X{
  784. X    if (currinf.virtual) {
  785. X        ++currinf.pos;
  786. X        return EOP;
  787. X    }
  788. X    currinf.end = dvi_buffer +
  789. X        read(fileno(dvi_file), (_Xconst char *) (currinf.pos = dvi_buffer),
  790. X        DVI_BUFFER_LEN);
  791. X    return currinf.end > dvi_buffer ? *(currinf.pos)++ : EOF;
  792. X}
  793. X
  794. X#define    xone()  (currinf.pos < currinf.end ? *(currinf.pos)++ : xxone())
  795. X
  796. Xstatic    unsigned long
  797. Xxnum(size)
  798. X    register ubyte size;
  799. X{
  800. X    register long x = 0;
  801. X
  802. X    while (size--) x = (x << 8) | xone();
  803. X    return x;
  804. X}
  805. X
  806. Xstatic    long
  807. Xxsnum(size)
  808. X    register ubyte size;
  809. X{
  810. X    register long x;
  811. X
  812. X#ifdef    __STDC__
  813. X    x = (signed char) xone();
  814. X#else
  815. X    x = xone();
  816. X    if (x & 0x80) x -= 0x100;
  817. X#endif
  818. X    while (--size) x = (x << 8) | xone();
  819. X    return x;
  820. X}
  821. X
  822. X#define    xsfour()    xsnum(4)
  823. X
  824. Xstatic    void
  825. Xxskip(offset)
  826. X    long    offset;
  827. X{
  828. X    currinf.pos += offset;
  829. X    if (!currinf.virtual && currinf.pos > currinf.end)
  830. X        (void) lseek(fileno(dvi_file), (long) (currinf.pos - currinf.end),
  831. X        SEEK_CUR);
  832. X}
  833. X
  834. X
  835. X/*
  836. X *    Count the number of set bits in a given region of the bitmap
  837. X */
  838. X
  839. Xchar    sample_count[]    = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
  840. X
  841. Xstatic    int
  842. Xsample(bits, bytes_wide, bit_skip, w, h)
  843. X    BMUNIT    *bits;
  844. X    int    bytes_wide, bit_skip, w, h;
  845. X{
  846. X    BMUNIT    *ptr, *endp;
  847. X    register BMUNIT *cp;
  848. X    int    bits_left;
  849. X    register int n, bit_shift, wid;
  850. X
  851. X    ptr = bits + bit_skip / BITS_PER_BMUNIT;
  852. X    endp = ADD(bits, h * bytes_wide);
  853. X    bits_left = w;
  854. X#ifndef    MSBITFIRST
  855. X    bit_shift = bit_skip % BITS_PER_BMUNIT;
  856. X#else
  857. X    bit_shift = BITS_PER_BMUNIT - bit_skip % BITS_PER_BMUNIT;
  858. X#endif
  859. X    n = 0;
  860. X    while (bits_left) {
  861. X#ifndef    MSBITFIRST
  862. X        wid = BITS_PER_BMUNIT - bit_shift;
  863. X#else
  864. X        wid = bit_shift;
  865. X#endif
  866. X        if (wid > bits_left) wid = bits_left;
  867. X        if (wid > 4) wid = 4;
  868. X#ifdef    MSBITFIRST
  869. X        bit_shift -= wid;
  870. X#endif
  871. X        for (cp = ptr; cp < endp; cp = ADD(cp, bytes_wide))
  872. X        n += sample_count[(*cp >> bit_shift) & bit_masks[wid]];
  873. X#ifndef    MSBITFIRST
  874. X        bit_shift += wid;
  875. X        if (bit_shift == BITS_PER_BMUNIT) {
  876. X        bit_shift = 0;
  877. X        ++ptr;
  878. X        }
  879. X#else
  880. X        if (bit_shift == 0) {
  881. X        bit_shift = BITS_PER_BMUNIT;
  882. X        ++ptr;
  883. X        }
  884. X#endif
  885. X        bits_left -= wid;
  886. X    }
  887. X    return n;
  888. X}
  889. X
  890. Xstatic    void
  891. Xshrink_glyph(g)
  892. X    register struct glyph *g;
  893. X{
  894. X    int shrunk_bytes_wide, shrunk_height;
  895. X    int rows_left, rows, init_cols, cols_left;
  896. X    register int cols;
  897. X    BMUNIT *old_ptr, *new_ptr;
  898. X    register BMUNIT m, *cp;
  899. X    int min_sample = shrink_factor * shrink_factor * density / 100;
  900. X
  901. X    /* These machinations ensure that the character is shrunk according to
  902. X       its hot point, rather than its upper left-hand corner. */
  903. X    g->x2 = g->x / shrink_factor;
  904. X    init_cols = g->x - g->x2 * shrink_factor;
  905. X    if (init_cols <= 0) init_cols += shrink_factor;
  906. X    else ++g->x2;
  907. X    g->bitmap2.w = g->x2 + ROUNDUP(g->bitmap.w - g->x, shrink_factor);
  908. X    /* include row zero with the positively numbered rows */
  909. X    cols = g->y + 1; /* spare register variable */
  910. X    g->y2 = cols / shrink_factor;
  911. X    rows = cols - g->y2 * shrink_factor;
  912. X    if (rows <= 0) {
  913. X        rows += shrink_factor;
  914. X        --g->y2;
  915. X    }
  916. X    g->bitmap2.h = shrunk_height = g->y2 +
  917. X        ROUNDUP(g->bitmap.h - cols, shrink_factor) + 1;
  918. X    if (g->bitmap2.bits) free(g->bitmap2.bits);
  919. X    alloc_bitmap(&g->bitmap2);
  920. X    old_ptr = (BMUNIT *) g->bitmap.bits;
  921. X    new_ptr = (BMUNIT *) g->bitmap2.bits;
  922. X    shrunk_bytes_wide = g->bitmap2.bytes_wide;
  923. X    rows_left = g->bitmap.h;
  924. X    bzero((char *) new_ptr, shrunk_bytes_wide * shrunk_height);
  925. X    while (rows_left) {
  926. X        if (rows > rows_left) rows = rows_left;
  927. X        cols_left = g->bitmap.w;
  928. X#ifndef    MSBITFIRST
  929. X        m = (1 << 0);
  930. X#else
  931. X        m = (1 << (BITS_PER_BMUNIT-1));
  932. X#endif
  933. X        cp = new_ptr;
  934. X        cols = init_cols;
  935. X        while (cols_left) {
  936. X        if (cols > cols_left) cols = cols_left;
  937. X        if (sample(old_ptr, g->bitmap.bytes_wide,
  938. X            g->bitmap.w - cols_left, cols, rows) >= min_sample)
  939. X            *cp |= m;
  940. X#ifndef    MSBITFIRST
  941. X        if (m == (BMUNIT)(1 << (BITS_PER_BMUNIT-1))) {
  942. X            m = (1 << 0);
  943. X            ++cp;
  944. X        }
  945. X        else m <<= 1;
  946. X#else
  947. X        if (m == (1 << 0)) {
  948. X            m = (1 << (BITS_PER_BMUNIT-1));
  949. X            ++cp;
  950. X        }
  951. X        else m >>= 1;
  952. X#endif
  953. X        cols_left -= cols;
  954. X        cols = shrink_factor;
  955. X        }
  956. X        *((char **) &new_ptr) += shrunk_bytes_wide;
  957. X        *((char **) &old_ptr) += rows * g->bitmap.bytes_wide;
  958. X        rows_left -= rows;
  959. X        rows = shrink_factor;
  960. X    }
  961. X    g->y2 = g->y / shrink_factor;
  962. X    if (debug & DBG_BITMAP)
  963. X        print_bitmap(&g->bitmap2);
  964. X}
  965. X
  966. X/*
  967. X *    Find font #n.
  968. X */
  969. X
  970. Xstatic    void
  971. Xchange_font(n)
  972. X    unsigned long n;
  973. X{
  974. X    register struct tn *tnp;
  975. X
  976. X    currinf.fontp = NULL;
  977. X    for (tnp = currinf.tn_head; tnp != NULL; tnp = tnp->next)
  978. X        if (tnp->TeXnumber == n) {
  979. X        currinf.fontp = tnp->fontp;
  980. X        if (!(currinf.fontp->flags & FONT_LOADED))
  981. X            load_font(currinf.fontp);
  982. X        break;
  983. X        }
  984. X    if (currinf.fontp == NULL) oops("Non-existent font #%d", n);
  985. X    maxchar = currinf.fontp->maxchar;
  986. X    currinf.set_char_p = currinf.fontp->set_char_p;
  987. X}
  988. X
  989. X
  990. X/*
  991. X *    Open a font file.
  992. X */
  993. X
  994. Xstatic    void
  995. Xopen_font_file(fontp)
  996. X    struct font *fontp;
  997. X{
  998. X    if (fontp->file == NULL) {
  999. X        fontp->file = xfopen(fontp->filename);
  1000. X        if (fontp->file == NULL)
  1001. X        oops("Font file disappeared:  %s", fontp->filename);
  1002. X    }
  1003. X}
  1004. X
  1005. Xlong
  1006. Xset_char(ch)
  1007. X    ubyte ch;
  1008. X{
  1009. X    register struct glyph *g;
  1010. X
  1011. X    if (ch > maxchar) realloc_font(currinf.fontp, ch);
  1012. X    if ((g = &currinf.fontp->glyph[ch])->bitmap.bits == NULL) {
  1013. X        if (g->addr == 0) {
  1014. X        if (!hush_chars)
  1015. X            Fprintf(stderr, "Character %d not defined in font %s\n", ch,
  1016. X            currinf.fontp->fontname);
  1017. X        g->addr = -1;
  1018. X        return 0L;
  1019. X        }
  1020. X        if (g->addr == -1) return 0L; /* previously flagged missing char */
  1021. X        open_font_file(currinf.fontp);
  1022. X        Fseek(currinf.fontp->file, g->addr, 0);
  1023. X        (*currinf.fontp->read_char)(currinf.fontp, ch);
  1024. X        if (debug & DBG_BITMAP) print_char(ch, g);
  1025. X        currinf.fontp->timestamp = ++current_timestamp;
  1026. X    }
  1027. X
  1028. X    if (shrink_factor == 1)
  1029. X        put_bitmap(&g->bitmap, PXL_H - g->x, PXL_V - g->y);
  1030. X    else {
  1031. X        if (g->bitmap2.bits == NULL) {
  1032. X        shrink_glyph(g);
  1033. X        }
  1034. X        put_bitmap(&g->bitmap2, PXL_H - g->x2, PXL_V - g->y2);
  1035. X    }
  1036. X    return g->dvi_adv;
  1037. X}
  1038. X
  1039. X
  1040. Xstatic    long
  1041. Xset_first_font_char(ch)
  1042. X    ubyte    ch;
  1043. X{
  1044. X    currinf.fontp = currinf.fontp->first_font;
  1045. X    if (currinf.fontp == NULL) oops("Non-existent font");
  1046. X    if (!(currinf.fontp->flags & FONT_LOADED))
  1047. X        load_font(currinf.fontp);
  1048. X    maxchar = currinf.fontp->maxchar;
  1049. X    currinf.set_char_p = currinf.fontp->set_char_p;
  1050. X    return (*currinf.set_char_p)(ch);
  1051. X}
  1052. X
  1053. X
  1054. Xlong
  1055. Xset_vf_char(ch)
  1056. X    ubyte    ch;
  1057. X{
  1058. X    register struct macro *m;
  1059. X    struct drawinf    oldinfo;
  1060. X    static    ubyte    c;
  1061. X
  1062. X    if (ch > maxchar) realloc_virtual_font(currinf.fontp, ch);
  1063. X    if ((m = &currinf.fontp->macro[ch])->pos == NULL) {
  1064. X        if (!hush_chars)
  1065. X        Fprintf(stderr, "Character %d not defined in font %s\n", ch,
  1066. X            currinf.fontp->fontname);
  1067. X        m->pos = m->end = &c;
  1068. X        return 0L;
  1069. X    }
  1070. X    oldinfo = currinf;
  1071. X    WW = XX = YY = ZZ = 0;
  1072. X    currinf.tn_head = currinf.fontp->vf_chain;
  1073. X    currinf.set_char_p = set_first_font_char;
  1074. X    currinf.pos = m->pos;
  1075. X    currinf.end = m->end;
  1076. X    currinf.virtual = True;
  1077. X    draw_part(current_frame, currinf.fontp->dimconv);
  1078. X    if (currinf.pos != currinf.end + 1)
  1079. X        oops("Virtual character macro does not end correctly.");
  1080. X    currinf = oldinfo;
  1081. X    return m->dvi_adv;
  1082. X}
  1083. X
  1084. X/*ARGSUSED*/
  1085. Xstatic    long
  1086. Xset_no_char(ch)
  1087. X    ubyte    ch;
  1088. X{
  1089. X    oops("Dvi file or vf macro sets character of unknown font.");
  1090. X    /* NOTREACHED */
  1091. X}
  1092. X
  1093. X/*
  1094. X *    Set rule.  Arguments are coordinates of lower left corner.
  1095. X */
  1096. X
  1097. Xstatic    void
  1098. Xset_rule(h, w)
  1099. X    int h, w;
  1100. X{
  1101. X    put_rectangle(PXL_H, PXL_V - h + 1, w, h, False);
  1102. X}
  1103. X
  1104. Xstatic    void
  1105. Xput_border(w, h)
  1106. X    int w, h;
  1107. X{
  1108. X    put_rectangle(0, 0, w, 1, True);    /* top */
  1109. X    put_rectangle(w, 0, 1, h, True);    /* right */
  1110. X    put_rectangle(1, h, w, 1, True);    /* bottom */
  1111. X    put_rectangle(0, 1, 1, h, True);    /* left */
  1112. X}
  1113. X
  1114. Xstatic    void
  1115. Xspecial(nbytes)
  1116. X    long    nbytes;
  1117. X{
  1118. X    static    char    *cmd    = NULL;
  1119. X    static    long    cmdlen    = -1;
  1120. X    char    *p;
  1121. X
  1122. X    if (cmdlen < nbytes) {
  1123. X        if (cmd) free(cmd);
  1124. X        cmd = xmalloc((unsigned) nbytes + 1, "special");
  1125. X        cmdlen = nbytes;
  1126. X    }
  1127. X    p = cmd;
  1128. X    for (;;) {
  1129. X        int i = currinf.end - currinf.pos;
  1130. X
  1131. X        if (i > nbytes) i = nbytes;
  1132. X        bcopy((_Xconst char *) currinf.pos, p, i);
  1133. X        currinf.pos += i;
  1134. X        p += i;
  1135. X        nbytes -= i;
  1136. X        if (nbytes == 0) break;
  1137. X        (void) xxone();
  1138. X        --(currinf.pos);
  1139. X    }
  1140. X    *p = '\0';
  1141. X    applicationDoSpecial(cmd);
  1142. X}
  1143. X
  1144. X#define    xspell_conv(n)    spell_conv0(n, current_dimconv)
  1145. X
  1146. Xstatic    void
  1147. Xdraw_part(minframe, current_dimconv)
  1148. X    struct frame    *minframe;
  1149. X    double        current_dimconv;
  1150. X{
  1151. X    ubyte ch;
  1152. X
  1153. X    for (;;) {
  1154. X        ch = xone();
  1155. X        if (debug & DBG_DVI)
  1156. X        print_dvi(ch);
  1157. X        if (ch <= SETCHAR0 + 127) {
  1158. X        long a = (*currinf.set_char_p)(ch);
  1159. X        DVI_H += a;
  1160. X        } else if (FNTNUM0 <= ch && ch <= FNTNUM0 + 63) {
  1161. X        change_font((unsigned long) (ch - FNTNUM0));
  1162. X        } else {
  1163. X        long a, b;
  1164. X        ubyte ch1;
  1165. X
  1166. X        switch (ch) {
  1167. X            case SET1:
  1168. X            case PUT1:
  1169. X            ch1 = xone();
  1170. X            a = (*currinf.set_char_p)(ch1);
  1171. X            if (ch != PUT1) DVI_H += a;
  1172. X            break;
  1173. X
  1174. X            case SETRULE:
  1175. X            /* Be careful, dvicopy outputs rules with
  1176. X               height = 0x80000000.  We don't want any
  1177. X               SIGFPE here. */
  1178. X            a = xsfour();
  1179. X            b = xspell_conv(xsfour());
  1180. X            if (a > 0 && b > 0)
  1181. X                set_rule(pixel_round(xspell_conv(a)),
  1182. X                pixel_round(b));
  1183. X            DVI_H += b;
  1184. X            break;
  1185. X
  1186. X            case PUTRULE:
  1187. X            a = xspell_conv(xsfour());
  1188. X            b = xspell_conv(xsfour());
  1189. X            if (a > 0  &&  b > 0)
  1190. X                set_rule(pixel_round(a), pixel_round(b));
  1191. X            break;
  1192. X
  1193. X            case NOP:
  1194. X            break;
  1195. X
  1196. X            case BOP:
  1197. X            xskip((long) 11 * 4);
  1198. X            DVI_H = OFFSET_X;
  1199. X            DVI_V = OFFSET_Y;
  1200. X            PXL_V = pixel_conv(DVI_V);
  1201. X            WW = XX = YY = ZZ = 0;
  1202. X            break;
  1203. X
  1204. X            case EOP:
  1205. X            if (current_frame != minframe)
  1206. X                oops("Stack not empty at EOP");
  1207. X            return;
  1208. X
  1209. X            case PUSH:
  1210. X            if (current_frame->next == NULL) {
  1211. X                struct frame *newp = (struct frame *)
  1212. X                xmalloc(sizeof(struct frame), "stack frame");
  1213. X                current_frame->next = newp;
  1214. X                newp->prev = current_frame;
  1215. X                newp->next = NULL;
  1216. X            }
  1217. X            current_frame = current_frame->next;
  1218. X            current_frame->data = currinf.data;
  1219. X            break;
  1220. X
  1221. X            case POP:
  1222. X            if (current_frame == minframe)
  1223. X                oops("More POPs than PUSHes");
  1224. X            currinf.data = current_frame->data;
  1225. X            current_frame = current_frame->prev;
  1226. X            break;
  1227. X
  1228. X            case RIGHT1:
  1229. X            case RIGHT2:
  1230. X            case RIGHT3:
  1231. X            case RIGHT4:
  1232. X            DVI_H += xspell_conv(xsnum(ch - RIGHT1 + 1));
  1233. X            break;
  1234. X
  1235. X            case X1:
  1236. X            case X2:
  1237. X            case X3:
  1238. X            case X4:
  1239. X            XX = xspell_conv(xsnum(ch - X0));
  1240. X            case X0:
  1241. X            DVI_H += XX;
  1242. X            break;
  1243. X
  1244. X            case W1:
  1245. X            case W2:
  1246. X            case W3:
  1247. X            case W4:
  1248. X            WW = xspell_conv(xsnum(ch - W0));
  1249. X            case W0:
  1250. X            DVI_H += WW;
  1251. X            break;
  1252. X
  1253. X            case Y1:
  1254. X            case Y2:
  1255. X            case Y3:
  1256. X            case Y4:
  1257. X            YY = xspell_conv(xsnum(ch - Y0));
  1258. X            case Y0:
  1259. X            DVI_V += YY;
  1260. X            PXL_V = pixel_conv(DVI_V);
  1261. X            break;
  1262. X
  1263. X            case Z1:
  1264. X            case Z2:
  1265. X            case Z3:
  1266. X            case Z4:
  1267. X            ZZ = xspell_conv(xsnum(ch - Z0));
  1268. X            case Z0:
  1269. X            DVI_V += ZZ;
  1270. X            PXL_V = pixel_conv(DVI_V);
  1271. X            break;
  1272. X
  1273. X            case DOWN1:
  1274. X            case DOWN2:
  1275. X            case DOWN3:
  1276. X            case DOWN4:
  1277. X            DVI_V += xspell_conv(xsnum(ch - DOWN1 + 1));
  1278. X            PXL_V = pixel_conv(DVI_V);
  1279. X            break;
  1280. X
  1281. X            case FNT1:
  1282. X            case FNT2:
  1283. X            case FNT3:
  1284. X            case FNT4:
  1285. X            change_font(xnum(ch - FNT1 + 1));
  1286. X            break;
  1287. X
  1288. X            case XXX1:
  1289. X            case XXX2:
  1290. X            case XXX3:
  1291. X            case XXX4:
  1292. X            a = xnum(ch - XXX1 + 1);
  1293. X            if (a > 0)
  1294. X                special(a);
  1295. X            break;
  1296. X
  1297. X            case FNTDEF1:
  1298. X            case FNTDEF2:
  1299. X            case FNTDEF3:
  1300. X            case FNTDEF4:
  1301. X            xskip((long) (12 + ch - FNTDEF1 + 1));
  1302. X            xskip((long) xone() + (long) xone());
  1303. X            break;
  1304. X
  1305. X            case PRE:
  1306. X            oops("Shouldn't happen: PRE encountered.");
  1307. X            break;
  1308. X
  1309. X            case POST:
  1310. X            oops("Shouldn't happen: POST encountered.");
  1311. X            break;
  1312. X
  1313. X            case POSTPOST:
  1314. X            oops("Unexpected POSTPOST encountered.");
  1315. X            break;
  1316. X
  1317. X            default:
  1318. X            oops("Unknown op-code %d", ch);
  1319. X        } /* end switch*/
  1320. X        } /* end else (ch not a SETCHAR or FNTNUM) */
  1321. X    } /* end for */
  1322. X}
  1323. X
  1324. X#undef    xspell_conv
  1325. X
  1326. Xvoid
  1327. Xdraw_page()
  1328. X{
  1329. X    /* Check for changes in dvi file. */
  1330. X    if (!check_dvi_file()) return;
  1331. X
  1332. X    put_border(ROUNDUP(unshrunk_paper_w, shrink_factor) + 1,
  1333. X        ROUNDUP(unshrunk_paper_h, shrink_factor) + 1);
  1334. X
  1335. X    (void) lseek(fileno(dvi_file), page_offset[current_page], SEEK_SET);
  1336. X
  1337. X    bzero((char *) &currinf.data, sizeof(currinf.data));
  1338. X    currinf.set_char_p = set_no_char;
  1339. X    currinf.tn_head = tn_head;
  1340. X    currinf.pos = currinf.end = dvi_buffer;
  1341. X    currinf.virtual = False;
  1342. X    draw_part(current_frame = &frame0, dimconv);
  1343. X}
  1344. END_OF_FILE
  1345. if test 16733 -ne `wc -c <'dvi_draw.c'`; then
  1346.     echo shar: \"'dvi_draw.c'\" unpacked with wrong size!
  1347. fi
  1348. # end of 'dvi_draw.c'
  1349. fi
  1350. if test -f 'dvi_init.c' -a "${1}" != "-c" ; then 
  1351.   echo shar: Will not clobber existing file \"'dvi_init.c'\"
  1352. else
  1353. echo shar: Extracting \"'dvi_init.c'\" \(14903 characters\)
  1354. sed "s/^X//" >'dvi_init.c' <<'END_OF_FILE'
  1355. X/*
  1356. X * DVI previewer for X.
  1357. X *
  1358. X * Eric Cooper, CMU, September 1985.
  1359. X *
  1360. X * Code derived from dvi-imagen.c.
  1361. X *
  1362. X * Modification history:
  1363. X * 1/1986    Modified for X.10 by Bob Scheifler, MIT LCS.
  1364. X * 7/1988    Modified for X.11 by Mark Eichin, MIT
  1365. X * 12/1988    Added 'R' option, toolkit, magnifying glass
  1366. X *            --Paul Vojta, UC Berkeley.
  1367. X * 2/1989    Added tpic support    --Jeffrey Lee, U of Toronto
  1368. X * 4/1989    Modified for System V by Donald Richardson, Clarkson Univ.
  1369. X * 3/1990    Added VMS support    --Scott Allendorf, U of Iowa
  1370. X *
  1371. X *    Compilation options:
  1372. X *    SYSV    compile for System V
  1373. X *    VMS    compile for VMS
  1374. X *    X10    compile for X10
  1375. X *    NOTOOL    compile without toolkit (X11 only)
  1376. X *    BUTTONS    compile with buttons on the side of the window (needs toolkit)
  1377. X *    MSBITFIRST    store bitmaps internally with most significant bit first
  1378. X *    BMSHORT    store bitmaps in shorts instead of bytes
  1379. X *    BMLONG    store bitmaps in longs instead of bytes
  1380. X *    ALTFONT    default for -altfont option
  1381. X *    A4    use European size paper
  1382. X */
  1383. X
  1384. X#include "xdvi.h"
  1385. X#include "dvi.h"
  1386. X#include <sys/stat.h>
  1387. X
  1388. X#ifdef    sun
  1389. Xextern    char    *sprintf();
  1390. X#endif
  1391. X
  1392. X#ifndef    X_NOT_STDC_ENV
  1393. X#include <stdlib.h>
  1394. X#else
  1395. Xchar    *realloc();
  1396. Xvolatile void    exit();
  1397. X#endif
  1398. X#if    defined(macII) && !defined(__STDC__) /* stdlib.h doesn't define these */
  1399. Xchar    *realloc();
  1400. X#endif /* macII */
  1401. X
  1402. X#define    PK_PRE        247
  1403. X#define    PK_ID        89
  1404. X#define    PK_MAGIC    (PK_PRE << 8) + PK_ID
  1405. X#define    GF_PRE        247
  1406. X#define    GF_ID        131
  1407. X#define    GF_MAGIC    (GF_PRE << 8) + GF_ID
  1408. X#define    VF_PRE        247
  1409. X#define    VF_ID_BYTE    202
  1410. X#define    VF_MAGIC    (VF_PRE << 8) + VF_ID_BYTE
  1411. X#define    PXL_MAGIC1    0
  1412. X#define    PXL_MAGIC2    1001
  1413. X
  1414. X#define    dvi_oops(str)    longjmp(dvi_env, (int) str);
  1415. X
  1416. Xstatic    struct stat fstatbuf;
  1417. X
  1418. Xstatic    Boolean    font_not_found;
  1419. X
  1420. X/*
  1421. X * DVI preamble and postamble information.
  1422. X */
  1423. Xstatic    char    job_id[300];
  1424. Xstatic    long    numerator, denominator, magnification;
  1425. X
  1426. X/*
  1427. X * Offset in DVI file of last page, set in read_postamble().
  1428. X */
  1429. Xstatic    long    last_page_offset;
  1430. X
  1431. X
  1432. X/*
  1433. X *    free_vf_chain frees the vf_chain structure.
  1434. X */
  1435. X
  1436. Xstatic    void
  1437. Xfree_vf_chain(tnp)
  1438. X    struct tn *tnp;
  1439. X{
  1440. X    while (tnp != NULL) {
  1441. X        register struct tn *tnp1 = tnp->next;
  1442. X        free((char *) tnp);
  1443. X        tnp = tnp1;
  1444. X    }
  1445. X}
  1446. X
  1447. X
  1448. X/*
  1449. X *    Release all shrunken bitmaps for all fonts.
  1450. X */
  1451. X
  1452. Xvoid
  1453. Xreset_fonts()
  1454. X{
  1455. X    register struct font *f;
  1456. X    register struct glyph *g;
  1457. X
  1458. X    for (f = font_head; f != NULL; f = f->next)
  1459. X        for (g = f->glyph; g <= f->glyph + f->maxchar; ++g)
  1460. X        if (g->bitmap2.bits) {
  1461. X            free(g->bitmap2.bits);
  1462. X            g->bitmap2.bits = NULL;
  1463. X        }
  1464. X}
  1465. X
  1466. X/*
  1467. X *    realloc_font allocates the font structure to contain (newsize + 1)
  1468. X *    characters.
  1469. X */
  1470. X
  1471. Xvoid
  1472. Xrealloc_font(fontp, newsize)
  1473. X    struct font *fontp;
  1474. X    ubyte    newsize;
  1475. X{
  1476. X    struct glyph *glyph;
  1477. X
  1478. X    glyph = fontp->glyph = (struct glyph *) realloc((char *) fontp->glyph,
  1479. X        ((unsigned int) newsize + 1) * sizeof(struct glyph));
  1480. X    if (glyph == NULL) oops("! Cannot reallocate space for glyph array.");
  1481. X    if (newsize > fontp->maxchar)
  1482. X        bzero((char *) (glyph + fontp->maxchar + 1),
  1483. X        (int) (newsize - fontp->maxchar) * sizeof(struct glyph));
  1484. X    maxchar = fontp->maxchar = newsize;
  1485. X}
  1486. X
  1487. X
  1488. X/*
  1489. X *    realloc_virtual_font does the same thing for virtual fonts.
  1490. X */
  1491. X
  1492. Xvoid
  1493. Xrealloc_virtual_font(fontp, newsize)
  1494. X    struct font *fontp;
  1495. X    ubyte    newsize;
  1496. X{
  1497. X    struct macro *macro;
  1498. X
  1499. X    macro = fontp->macro = (struct macro *) realloc((char *) fontp->macro,
  1500. X        ((unsigned int) newsize + 1) * sizeof(struct macro));
  1501. X    if (macro == NULL) oops("! Cannot reallocate space for macro array.");
  1502. X    if (newsize > fontp->maxchar)
  1503. X        bzero((char *) (macro + fontp->maxchar + 1),
  1504. X        (int) (newsize - fontp->maxchar) * sizeof(struct macro));
  1505. X    maxchar = fontp->maxchar = newsize;
  1506. X}
  1507. X
  1508. X
  1509. X/*
  1510. X *    load_font locates the raster file and reads the index of characters,
  1511. X *    plus whatever other preprocessing is done (depending on the format).
  1512. X */
  1513. X
  1514. Xvoid
  1515. Xload_font(fontp)
  1516. X    struct font *fontp;
  1517. X{
  1518. X    float    fsize    = fontp->fsize;
  1519. X    int    dpi    = fsize + 0.5;
  1520. X    char    *font_found;
  1521. X    int    size_found;
  1522. X    int    magic;
  1523. X
  1524. X    fontp->flags |= FONT_LOADED;
  1525. X    fontp->file = font_open(fontp->fontname, &font_found,
  1526. X        (WIDEARG(float, double)) fsize, &size_found, &fontp->filename);
  1527. X    if (fontp->file == NULL) {
  1528. X        Fprintf(stderr, "Can't find font %s.\n", fontp->fontname);
  1529. X        font_not_found = True;
  1530. X        return;
  1531. X    }
  1532. X    --n_files_left;
  1533. X    if (font_found != NULL) {
  1534. X        Fprintf(stderr,
  1535. X            "Can't find font %s; using %s instead at %d dpi\n",
  1536. X            fontp->fontname, font_found, dpi);
  1537. X        free(fontp->fontname);
  1538. X        fontp->fontname = font_found;
  1539. X    }
  1540. X    else if (size_found > (int) (5 * 1.002 * fsize + 0.5) ||
  1541. X        size_found < (int) (5 * 0.998 * fsize + 0.5))
  1542. X        Fprintf(stderr,
  1543. X        "Can't find font %s at %d dpi; using %d dpi instead.\n",
  1544. X        fontp->fontname, dpi, (size_found + 2) / 5);
  1545. X    fontp->fsize = (float) size_found / 5;
  1546. X    fontp->timestamp = ++current_timestamp;
  1547. X    fontp->maxchar = maxchar = 255;
  1548. X    fontp->set_char_p = set_char;
  1549. X    magic = two(fontp->file);
  1550. X#ifdef    USE_PK
  1551. X    if (magic == PK_MAGIC) read_PK_index(fontp);
  1552. X    else
  1553. X#endif
  1554. X#ifdef    USE_GF
  1555. X        if (magic == GF_MAGIC) read_GF_index(fontp);
  1556. X    else
  1557. X#endif
  1558. X        if (magic == VF_MAGIC) read_VF_index(fontp);
  1559. X    else
  1560. X#ifdef    USE_PXL
  1561. X        if (magic == PXL_MAGIC1 && two(fontp->file) == PXL_MAGIC2)
  1562. X        read_PXL_index(fontp);
  1563. X    else
  1564. X#endif
  1565. X        oops("Cannot recognize format for font file %s", fontp->filename);
  1566. X
  1567. X    if (fontp->flags & FONT_VIRTUAL) {
  1568. X        while (maxchar > 0 && fontp->macro[maxchar].pos == NULL) --maxchar;
  1569. X        if (maxchar < 255) realloc_virtual_font(fontp, maxchar);
  1570. X    }
  1571. X    else {
  1572. X        while (maxchar > 0 && fontp->glyph[maxchar].addr == 0) --maxchar;
  1573. X        if (maxchar < 255) realloc_font(fontp, maxchar);
  1574. X    }
  1575. X}
  1576. X
  1577. X
  1578. X/*
  1579. X *    reuse_font recursively sets the flags for font structures being reused.
  1580. X */
  1581. X
  1582. Xstatic    void
  1583. Xreuse_font(fontp)
  1584. X    struct font *fontp;
  1585. X{
  1586. X    struct tn *tnp;
  1587. X
  1588. X    if (fontp->flags & FONT_IN_USE) return;
  1589. X
  1590. X    fontp->flags |= FONT_IN_USE;
  1591. X    if (list_fonts)
  1592. X        Printf("(reusing) %s at %d dpi\n", fontp->fontname,
  1593. X        (int) (fontp->fsize + 0.5));
  1594. X    if (fontp->flags & FONT_VIRTUAL)
  1595. X        for (tnp = fontp->vf_chain; tnp != NULL; tnp = tnp->next)
  1596. X        reuse_font(tnp->fontp);
  1597. X}
  1598. X
  1599. X
  1600. X/*
  1601. X *      define_font reads the rest of the fntdef command and then reads in
  1602. X *      the specified pixel file, adding it to the global linked-list holding
  1603. X *      all of the fonts used in the job.
  1604. X */
  1605. Xvoid
  1606. Xdefine_font(file, cmnd, vfparent, tn_headpp)
  1607. X    FILE        *file;
  1608. X    WIDEARG(ubyte,unsigned int) cmnd;
  1609. X    struct font    *vfparent;    /* vf parent of this font, or NULL */
  1610. X    struct tn    **tn_headpp;    /* addr of head of list of TeXnumbers */
  1611. X{
  1612. X    register struct tn *tnp;
  1613. X    struct font *fontp;
  1614. X    float    fsize;
  1615. X    double    scale_dimconv;
  1616. X    int scale;
  1617. X    int design;
  1618. X    int len;
  1619. X    char *fontname;
  1620. X    int size;
  1621. X
  1622. X    tnp = (struct tn *) xmalloc((unsigned) sizeof(struct tn),
  1623. X        "TeXnumber structure");
  1624. X    tnp->next = *tn_headpp;
  1625. X    *tn_headpp = tnp;
  1626. X    tnp->TeXnumber = num(file, (ubyte) cmnd - FNTDEF1 + 1);
  1627. X    (void) four(file);    /* checksum */
  1628. X    scale = four(file);
  1629. X    design = four(file);
  1630. X    len = one(file) + one(file);
  1631. X    fontname = xmalloc((unsigned) len + 1, "font name");
  1632. X    Fread(fontname, sizeof(char), len, file);
  1633. X    fontname[len] = '\0';
  1634. X    if(debug & DBG_PK)
  1635. X        Printf("Define font \"%s\" scale=%d design=%d\n",
  1636. X        fontname, scale, design);
  1637. X    if (vfparent == NULL) {
  1638. X        fsize = 0.001 * scale / design * magnification * pixels_per_inch;
  1639. X        scale_dimconv = dimconv;
  1640. X    }
  1641. X    else {
  1642. X        /*
  1643. X         *    The scaled size is given in units of vfparent->scale * 2 ** -20
  1644. X         *    SPELL units, so we convert it into SPELL units by multiplying by
  1645. X         *        vfparent->dimconv.
  1646. X         *    The design size is given in units of 2 ** -20 pt, so we convert
  1647. X         *    into SPELL units by multiplying by
  1648. X         *        (pixels_per_inch * 2**16) / (72.27 * 2**20).
  1649. X         */
  1650. X        fsize = (0.001 * 72.27 * (1<<4)) * vfparent->dimconv * scale
  1651. X        / design * magnification;
  1652. X        scale_dimconv = vfparent->dimconv;
  1653. X    }
  1654. X    size = 5 * fsize + 0.5;
  1655. X    /*
  1656. X     * reuse font if possible
  1657. X     */
  1658. X    for (fontp = font_head;; fontp = fontp->next) {
  1659. X        if (fontp == NULL) {        /* if font doesn't exist yet */
  1660. X        if (list_fonts)
  1661. X            Printf("%s at %d dpi\n", fontname, (int) (fsize + 0.5));
  1662. X        fontp = (struct font *) xmalloc((unsigned) sizeof(struct font),
  1663. X            "font structure");
  1664. X        fontp->next = font_head;
  1665. X        font_head = fontp;
  1666. X        fontp->dimconv = scale * scale_dimconv / (1<<20);
  1667. X        fontp->fontname = fontname;
  1668. X        fontp->fsize = fsize;
  1669. X        fontp->flags = FONT_IN_USE;
  1670. X        if (vfparent == NULL) load_font(fontp);
  1671. X        break;
  1672. X        }
  1673. X        if (strcmp(fontname, fontp->fontname) == 0
  1674. X            && size == (int) (5 * fontp->fsize + 0.5)) {
  1675. X            /* if font already in use */
  1676. X        reuse_font(fontp);
  1677. X        free(fontname);
  1678. X        break;
  1679. X        }
  1680. X    }
  1681. X    tnp->fontp = fontp;
  1682. X}
  1683. X
  1684. X
  1685. X/*
  1686. X *      process_preamble reads the information in the preamble and stores
  1687. X *      it into global variables for later use.
  1688. X */
  1689. Xstatic    void
  1690. Xprocess_preamble()
  1691. X{
  1692. X    ubyte   k;
  1693. X
  1694. X    if (one(dvi_file) != PRE)
  1695. X        dvi_oops("DVI file doesn't start with preamble");
  1696. X    if (one(dvi_file) != 2)
  1697. X        dvi_oops("Wrong version of DVI output for this program");
  1698. X    numerator     = four(dvi_file);
  1699. X    denominator   = four(dvi_file);
  1700. X    magnification = four(dvi_file);
  1701. X    dimconv = (((double) numerator * magnification)
  1702. X        / ((double) denominator * 1000.));
  1703. X    dimconv = dimconv * (((long) pixels_per_inch)<<16) / 254000;
  1704. X    specialConv = pixels_per_inch * magnification / 1000000.0;
  1705. X    k = one(dvi_file);
  1706. X    Fread(job_id, sizeof(char), (int) k, dvi_file);
  1707. X    job_id[k] = '\0';
  1708. X}
  1709. X
  1710. X/*
  1711. X *      find_postamble locates the beginning of the postamble
  1712. X *    and leaves the file ready to start reading at that location.
  1713. X */
  1714. X#define    TMPSIZ    516    /* 4 trailer bytes + 512 junk bytes allowed */
  1715. Xstatic    void
  1716. Xfind_postamble()
  1717. X{
  1718. X    long    pos;
  1719. X    ubyte    temp[TMPSIZ];
  1720. X    ubyte    *p;
  1721. X    ubyte    *p1;
  1722. X    ubyte    byte;
  1723. X
  1724. X    Fseek(dvi_file, (long) 0, 2);
  1725. X    pos = ftell(dvi_file) - TMPSIZ;
  1726. X    if (pos < 0) pos = 0;
  1727. X    Fseek(dvi_file, pos, 0);
  1728. X    p = temp + fread((char *) temp, sizeof(char), TMPSIZ, dvi_file);
  1729. X    for (;;) {
  1730. X        p1 = p;
  1731. X        while (p1 > temp && *(--p1) != TRAILER) ;
  1732. X        p = p1;
  1733. X        while (p > temp && *(--p) == TRAILER) ;
  1734. X        if (p <= p1 - 4) break;    /* found 4 TRAILER bytes */
  1735. X        if (p <= temp) dvi_oops("DVI file corrupted");
  1736. X    }
  1737. X    pos += p - temp;
  1738. X    byte = *p;
  1739. X    while (byte == TRAILER) {
  1740. X        Fseek(dvi_file, --pos, 0);
  1741. X        byte = one(dvi_file);
  1742. X    }
  1743. X    if (byte != 2)
  1744. X        dvi_oops("Wrong version of DVI output for this program");
  1745. X    Fseek(dvi_file, pos - 4, 0);
  1746. X    Fseek(dvi_file, sfour(dvi_file), 0);
  1747. X}
  1748. X
  1749. X
  1750. X/*
  1751. X *      read_postamble reads the information in the postamble,
  1752. X *    storing it into global variables.
  1753. X *      It also takes care of reading in all of the pixel files for the fonts
  1754. X *      used in the job.
  1755. X */
  1756. Xstatic    void
  1757. Xread_postamble()
  1758. X{
  1759. X    ubyte   cmnd;
  1760. X    struct font    *fontp;
  1761. X    struct font    **fontpp;
  1762. X
  1763. X    if (one(dvi_file) != POST)
  1764. X        dvi_oops("Postamble doesn't begin with POST");
  1765. X    last_page_offset = four(dvi_file);
  1766. X    if (numerator != four(dvi_file)
  1767. X        || denominator != four(dvi_file)
  1768. X        || magnification != four(dvi_file))
  1769. X        dvi_oops("Postamble doesn't match preamble");
  1770. X        /* read largest box height and width */
  1771. X    unshrunk_page_h = (spell_conv(sfour(dvi_file)) >> 16) + offset_y;
  1772. X    if (unshrunk_page_h < unshrunk_paper_h)
  1773. X        unshrunk_page_h = unshrunk_paper_h;
  1774. X    unshrunk_page_w = (spell_conv(sfour(dvi_file)) >> 16) + offset_x;
  1775. X    if (unshrunk_page_w < unshrunk_paper_w)
  1776. X        unshrunk_page_w = unshrunk_paper_w;
  1777. X    (void) two(dvi_file);    /* max stack size */
  1778. X    total_pages = two(dvi_file);
  1779. X    font_not_found = False;
  1780. X    while ((cmnd = one(dvi_file)) >= FNTDEF1 && cmnd <= FNTDEF4)
  1781. X        define_font(dvi_file, cmnd, (struct font *) NULL, &tn_head);
  1782. X    if (cmnd != POSTPOST)
  1783. X        dvi_oops("Non-fntdef command found in postamble");
  1784. X    if (font_not_found)
  1785. X        dvi_oops("Not all pixel files were found");
  1786. X    /*
  1787. X     * free up fonts no longer in use
  1788. X     */
  1789. X    fontpp = &font_head;
  1790. X    while ((fontp = *fontpp) != NULL)
  1791. X        if (fontp->flags & FONT_IN_USE)
  1792. X        fontpp = &fontp->next;
  1793. X        else {
  1794. X        if (debug & DBG_PK)
  1795. X            Printf("Discarding font \"%s\" at %d dpi\n",
  1796. X            fontp->fontname, (int) (fontp->fsize + 0.5));
  1797. X        *fontpp = fontp->next;        /* remove from list */
  1798. X        free(fontp->fontname);
  1799. X        if (fontp->flags & FONT_LOADED) {
  1800. X            if (fontp->file != NULL) {
  1801. X            Fclose(fontp->file);
  1802. X            ++n_files_left;
  1803. X            }
  1804. X            free(fontp->filename);
  1805. X            if (fontp->flags & FONT_VIRTUAL) {
  1806. X            register struct macro *m;
  1807. X
  1808. X            for (m = fontp->macro;
  1809. X                m <= fontp->macro + fontp->maxchar; ++m)
  1810. X                if (m->free_me) free((char *) m->pos);
  1811. X            free((char *) fontp->macro);
  1812. X            free_vf_chain(fontp->vf_chain);
  1813. X            }
  1814. X            else {
  1815. X            register struct glyph *g;
  1816. X
  1817. X            for (g = fontp->glyph;
  1818. X                g <= fontp->glyph + fontp->maxchar; ++g) {
  1819. X                if (g->bitmap.bits != NULL) free(g->bitmap.bits);
  1820. X                if (g->bitmap2.bits != NULL) free(g->bitmap2.bits);
  1821. X            }
  1822. X            free((char *) fontp->glyph);
  1823. X            }
  1824. X            free((char *) fontp);
  1825. X        }
  1826. X        }
  1827. X}
  1828. X
  1829. Xstatic    void
  1830. Xprepare_pages()
  1831. X{
  1832. X    int i;
  1833. X
  1834. X    page_offset = (long *) xmalloc((unsigned) total_pages * sizeof(long),
  1835. X        "page directory");
  1836. X    i = total_pages;
  1837. X    page_offset[--i] = last_page_offset;
  1838. X    Fseek(dvi_file, last_page_offset, 0);
  1839. X    /*
  1840. X     * Follow back pointers through pages in the DVI file,
  1841. X     * storing the offsets in the page_offset table.
  1842. X     */
  1843. X    while (i > 0) {
  1844. X        Fseek(dvi_file, (long) (1+4+(9*4)), 1);
  1845. X        Fseek(dvi_file, page_offset[--i] = four(dvi_file), 0);
  1846. X    }
  1847. X}
  1848. X
  1849. Xvoid
  1850. Xinit_page()
  1851. X{
  1852. X    page_w = ROUNDUP(unshrunk_page_w, mane.shrinkfactor) + 2;
  1853. X    page_h = ROUNDUP(unshrunk_page_h, mane.shrinkfactor) + 2;
  1854. X}
  1855. X
  1856. X/*
  1857. X *    init_dvi_file is the main subroutine for reading the startup information
  1858. X *    from the dvi file.
  1859. X */
  1860. X
  1861. Xstatic    void
  1862. Xinit_dvi_file()
  1863. X{
  1864. X    (void) fstat(fileno(dvi_file), &fstatbuf);
  1865. X    dvi_time = fstatbuf.st_mtime;
  1866. X    process_preamble();
  1867. X    find_postamble();
  1868. X    read_postamble();
  1869. X    prepare_pages();
  1870. X    init_page();
  1871. X    if (current_page >= total_pages) current_page = total_pages - 1;
  1872. X    hush_spec_now = hush_spec;
  1873. X}
  1874. X
  1875. X/**
  1876. X **    open_dvi_file opens the dvi file and calls init_dvi_file() to
  1877. X **    initialize it.
  1878. X **/
  1879. X
  1880. Xvoid
  1881. Xopen_dvi_file()
  1882. X{
  1883. X    char *errmsg;
  1884. X
  1885. X    if ((dvi_file = fopen(dvi_name, OPEN_MODE)) == NULL) {
  1886. X        int n = strlen(dvi_name);
  1887. X        char *file = dvi_name;
  1888. X
  1889. X        if (strcmp(dvi_name + n - sizeof(".dvi") + 1, ".dvi") == 0) {
  1890. X        perror(dvi_name);
  1891. X        exit(1);
  1892. X        }
  1893. X        dvi_name = xmalloc((unsigned) n + sizeof(".dvi"), "dvi file name");
  1894. X        Sprintf(dvi_name, "%s.dvi", file);
  1895. X        if ((dvi_file = fopen(dvi_name, OPEN_MODE)) == NULL) {
  1896. X        perror(dvi_name);
  1897. X        exit(1);
  1898. X        }
  1899. X    }
  1900. X
  1901. X    if (errmsg = (char *) setjmp(dvi_env)) oops(errmsg);
  1902. X    init_dvi_file();
  1903. X}
  1904. X
  1905. X/**
  1906. X **    Check for changes in dvi file.
  1907. X **/
  1908. X
  1909. XBoolean
  1910. Xcheck_dvi_file()
  1911. X{
  1912. X    struct font *fontp;
  1913. X
  1914. X    if (dvi_file == NULL || fstat(fileno(dvi_file), &fstatbuf) != 0
  1915. X        || fstatbuf.st_mtime != dvi_time) {
  1916. X        if (dvi_file) Fclose(dvi_file);
  1917. X        free((char *) page_offset);
  1918. X        dvi_file = fopen(dvi_name, OPEN_MODE);
  1919. X        if (dvi_file == NULL)
  1920. X            dvi_oops("Cannot reopen dvi file.");
  1921. X        if (list_fonts) Putchar('\n');
  1922. X        free_vf_chain(tn_head);
  1923. X        tn_head = NULL;
  1924. X        for (fontp = font_head; fontp != NULL; fontp = fontp->next)
  1925. X            fontp->flags &= ~FONT_IN_USE;
  1926. X        init_dvi_file();
  1927. X        redraw_page();
  1928. X        return False;
  1929. X    }
  1930. X    return True;
  1931. X}
  1932. END_OF_FILE
  1933. if test 14903 -ne `wc -c <'dvi_init.c'`; then
  1934.     echo shar: \"'dvi_init.c'\" unpacked with wrong size!
  1935. fi
  1936. # end of 'dvi_init.c'
  1937. fi
  1938. echo shar: End of shell archive.
  1939. exit 0
  1940. -- 
  1941. --
  1942. Molecular Simulations, Inc.            mail: dcmartin@msi.com
  1943. 796 N. Pastoria Avenue                uucp: uunet!dcmartin
  1944. Sunnyvale, California 94086            at&t: 408/522-9236
  1945.