#include	<stdio.h>
#include	<string.h>
#include	<ctype.h>
#include	<math.h>
#include	<setjmp.h>

#ifdef	__STDC__
#define		PP(x)	x
#else
#define		PP(x)	()
#endif

#define		false	0
#define		true	1

#define TylVersion      "This is TeXtyl, Version 1.30"
/* for dvi-commands */

#define	DEFTEXFONTS	"/n/l/u/kin/lib/tex/tfm"

#define SET1            128
#define PUT1            133
#define PUTRULE         137
#define NOP             138
#define PUSH            141
#define POP             142
#define RIGHTLEFT       143
#define DOWNUP          157
#define FONTDEF         244
#define USEFONT         236

#define USESTDAREA      0
    /* flag to use the 'standard' area to find .tfm files */

/* some conversions and numbers */

#define TWO16           65536   /* 2 ^ 16 */
#define TWO20           1048576   /* 2 ^ 20 */
#define TWO23           8388608
#define TWO24           16777216
#define TWO27           134217728
#define TWO31           2147483647   /* 2^31 - 1 */

#define SPPERPT         65536   /* scaled points per printers point */
#define SPPERMM         186468   /* scaled pts per millimetre */

#define RADTODEG        57.29577952   /* degrees per radian */
#define DEGTORAD        0.0174532925   /* radians per degree */
#define PI              3.141592654

#define BIGREAL         1.0e30

#define MAXCTLPTS       63   /* max number of control points */
#define MAXCTLPTSp3     66   /* max control points + 3 */
#define ARRLIMIT        100   /* limit for strings and other arrays */
#define MAXSPLINESEGS   480   /* max number of spline segments */
#define MAXTBDs         50   /* max number of Fonts-to-be-Defined */
#define MAXVECLENsp     262144
/*  Normal maximum length of longest
 *  vector-font character in scaled points
 */

#define OURFONTFLAG     256   /* our special 'byte' value flag */
/* Music Font dependent constants */

#define DOTCHAR         127   /* ascii number of char that is a dot */

#define QNOTEGHUS       18.0
    /* MF: Global Horizontal Units for a Quarternote */
#define QNOTEGVUS       16.0
    /* MF: Global Vertical units for a quarternote */
#define GBMGHUS         12.0   /* MF: horizontal units for a grace beam */
#define GBMGVUS         9.0

#define BMSTART         0
#define BMEND           69   /* indices for start/end of the beam chars */
#define LOBM1           0   /* indices for the regular beam chars that */
#define HIBM1           34   /*   are 1 quarternote long, and */
#define LOBM1p5         35   /*   for those that are 1.5 quarternotes long */
#define HIBM1p5         69

#define GBMSTART        70
#define GBMEND          105   /* indices for the grace beams */
#define LOGBMp5         70   /* indices for grace beam chars that */
#define HIGBMp5         87   /* are 0.5 grace quarternote long, and */
#define LOGBMp66        88   /* 0.66 grace quarternotes long */
#define HIGBMp66        105

#define LoVThick        1   /* Bounds for Vector char thicknesses */
#define HiVThick        13

#define SizVFontTable   39
/* size of the Vector Font Table */
/* 3*HiVThick */
#define SizMFontTable   18   /* size of the Music Font Table */
#define MAXLABELFONTS   5
#define SizLFontTable   MAXLABELFONTS   /* size of the Label Font Table */

#define TFMSIZE         8000   /* size of TFM array to hold .tfm file info */

/* Numeric names for the TeXtyl primitives */
#define Aline           1   /* should be first */
#define Aspline         2
#define Attspline       3
#define Abeam           4
#define Atieslur        5
#define Aarc            6
#define Alabel          7
#define Afigure         8   /* should be last one */

#define MAXFONTS        60   /* number of TeX fonts to keep track of */
#define STACKSIZE       50   /* size of stack for pushes and pops */
#define AREALENGTH      19   /* see also "sysdependent" proc for this value*/

#define ERRSIGNAL       '?'
#define ERRNOTBAD       0
#define ERRBAD          1
#define ERRREALBAD      2

typedef int int32;
typedef int ScaledPts;
typedef int ControlPoints[MAXCTLPTSp3 + 1][2];
typedef int VecIndex;
typedef char VThickness;
typedef VThickness ThickAryType[MAXSPLINESEGS + 1];
typedef enum {
  VKCirc, VKVert, VKHort
} VectKind;
typedef enum {
  regular, grace
} BeamKind;
typedef enum {
  BSPL, INTBSPL, CATROM, CARD
} SplineKind;
typedef enum {
  solid, dotted, dashed, dotdash
} LineStyle;

typedef char charstring[ARRLIMIT];

typedef struct Item {
  struct Item *nextitem;
  int32 BBlx, BBby, BBrx, BBty;   /* Bounding box */
  int itemthick, itemvec;
  int itempatt;
  int kind;
  union {
    struct {
      int32 lx1, ly1, lx2, ly2;
    } U1;
    struct {
      int spltype;
      int sclosed;
      int32 dosmarks, nsplknots;
      ControlPoints spts;
    } U2;
    struct {
      int tspltype;
      int tclosed;
      int32 dottmarks, nttknots;
      ControlPoints ttpts;
      ThickAryType ttarry;
    } U3;
    struct {
      int32 bx1, by1, bx2, by2, staf;
      int bkind;
    } U4;
    struct {
      int32 ntknots;
      int minth, maxth;
      ControlPoints tspts;
    } U5;
    struct {
      int32 acentx, acenty, aradius, firstang, lastang, narcknots;
      ControlPoints arcpts;
    } U6;
    struct {
      int32 labx, laby, fontstyle;
      charstring labeltext;
    } U7;
    struct {
      double figtheta, fsx, fsy;
      int32 fdx, fdy, preWid, preHt, postWid, postHt, depthnumber;
      struct Figure *body;
    } U8;
  } UU;
} Item;

typedef struct Figure {
  Item *things;
} Figure;

typedef struct {
  int32 num;
  char name[32];
  int32 checksum, scaledsize, designsize, space, bc, ec;
  ScaledPts widths[128];
} fontinfo;

/* label fonts info */

typedef struct {
  int32 DesSize;
  charstring FontName;
  int32 Cksum;
  int Isdefined;
  int32 DVIFontNum, internalnumber, spacewidth;
} LabFontInfRec;