#include "post_mus.h"

float   scale_ht;
char   *splines = "spline";
char   *text_sharp = "#";
char   *text_flat = "\\db\\u";
char   *text_natural = "=";

putrck () {
    putatom (DDOT);
}

putckr () {
    int     x = x_now,
            y = y_now;
    absmove (x + atom[KNLINE].a_width + 1, y);
    putatom (DDOT);
}

putrckr () {
    int     x = x_now,
            y = y_now;
    putatom (DDOT);
    absmove (x + atom[DDOT].a_width + atom[NKNLINE].a_width, y);
    putatom (DDOT);
}

put_sop_clef () {
    printf ("move up 6; # soprano clef\n");
    putatom (ACLEF);
}

put_ten_clef () {
    printf ("move down 12; # tenor clef\n");
    putatom (ACLEF);
}

struct atoms    atom[] = {
/* pic commands, 4 args as pairs, height, width, postmove,
			1 for vertical centre, proc to execute */

/* SHARP sharp	     */
    "move to %d,%d; line down 10; move right 2 up 0.5; line up 10; # sharp\n\
move right 2 down 2.5; line left 6 down 1;\
move down 0.5; line right 6 up 1;\n\
move down 4; line left  6 down 1;\
move down 0.5; line right 6 up 1;\n\
move up 2 right 1; right;",
    X, 2, Y, 6, 0, 0, 0, 0,
    10, 8, 8, 0, 0,

/* FLAT flat		   */
    "move to %d,%d.5; line down 10; # flat\n\
spline right 6 up 4 then left 2 up 2 then left 4 down 2\n\
spline right 3.5 up 1.5 then right 2 down 2 then left 5.5 down 3.5\n\
spline right 5 up 3 then left 2 up 2 then left 3 down 1\n\
move right 6 down 1\n",
    X, 0, Y, 7, 0, 0, 0, 0,
    10, 8, 8, 0, 0,

/* NATURAL natural     */
    "move to %d,%d; line down 8; # natural\n\
line right 4 up 1; move up 1; line left 4 down 1; move up 4 right 4;\n\
line left 4 down 1; move up 1; line right 4 up 1;\n\
line down 8; move up 5 right 1; right\n",
    X, 0, Y, 6, 0, 0, 0, 0,
    10, 6, 6, 0, 0,

/* HOLLOW hollow note */
    "move to %d,%d; right; ellipse ht 5 wid 7; # hollow\n\
move left 0.5; left; ellipse ht 5 wid 6;\n\
move right 0.5; right; ellipse ht 5 wid 5;",
    X, 0, Y, 1, 0, 0, 0, 0,
    5, 7, 7, 0, 0,

/* SOLID solid note */
    "move to %d,%d; right; ellipse ht 4 wid 6.0; # solid\n\
move left 0.5; left; ellipse ht 3 wid 4.5;\n\
move right 0.5; right; ellipse ht 2 wid 3.0;\n\
move left 0.5; left; ellipse ht 1 wid 1.5;\n",
    X, 0, Y, 1, 0, 0, 0, 0,
    5, 6, 6, 0, 0,

/* barline BARLINE */
    0,
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 2, 2, 1, 0,

/* double line DBARLINE */
    0,
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 5, 5, 1, 0,

/* REPEAT repeat */
    0,
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 11, 11, 1, putrck,

/* DOT dot */
    "move right 1.5; circle radius 0.5; move left 1.5; right;circle radius 1; # dot\n",
    0, 0, 0, 0, 0, 0, 0, 0,
    1, 3, 0, 0, 0,

/* stick unused except for its length (i.e. height) */
    "stick",
    0, 0, 0, 0, 0, 0, 0, 0,
    HALFSTAVE + 5, 0, 0, 0, 0,

/* timesig unused except for width */
    "box \"T\"",
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 24, 0, 0, 0,

/* treble clef */
    "move to %d,%d; # treble clef\n\
spline left 3 then down 6 then right 9 then left 9 up 42 then right 12 \\\n\
then left 18 down 33 then right 18 then up 12 then left 9 \\\n\
then down 6 then right 3;\n\
move right 15 up 6\n",
    X, 6, Y, -14, 0, 0, 0, 0,
    FULLSTAVE, 18, 1, 1, 0,

/* top hook    */
    "move to %d,%d; line right 4 down 4; # tophook\n\
move left 4 up 3; line down 4 right 4; line up 2;\n\
line up 4 left 4; line down 4; right\n",
    X, 0, Y, -1, 0, 0, 0, 0,
    4, 4, 4, 0, 0,

/* bottom hook */
    "move to %d,%d; line right 4 up 4; # bottom hook\n\
move left 4 down 3; line right 4 up 4; line down 2;\n\
line down 4 left 4; line up 4; right\n",
    X, 0, Y, 1, 0, 0, 0, 0,
    4, 4, 4, 0, 0,

/* bass clef */
    "move to %d,%d; # bass clef\n\
spline left 3 then up 6 then right 12 then down 12 then left 12 down 8;\n\
move right 15 up 16;\n\
up; circle radius 1; move down 0.5; circle radius 0.5;\n\
move down 5; circle radius 0.5; move down 0.5; up; circle radius 1;\
move down 2 right 2\n",
    X, 6, Y, 6, 0, 0, 0, 0,
    FULLSTAVE, 18, 1, 1, 0,

/* ledger line */
    "move left 2; line right 10; move left 8 down 6; # ledger line\n",
    0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,

/* thin and thick vertical line for ends */
    0,
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 7, 7, 1, 0,

/* double dot DDOT */
    "move right 2; move up 2; circle radius 1; move down 0.5; circle radius 0.5; # double dot\n\
move down 5; circle radius 0.5; move down 0.5; up; circle radius 1;\
move up 2 right 2\n",
    0, 0, 0, 0, 0, 0, 0, 0,
    8, 5, 5, 0, 0,

/* breve */
    "moveto %d,%d; right; circle radius 2.5; move left 2.5; right; circle radius 2.5; # breve\n",
    X, 0, Y, 0, 0, 0, 0, 0,
    5, 8, 0, 0, 0,

/* updot */
    "move up 2.5; move right 1.5; \
circle radius 0.5; move left 1.25; right;circle radius 1;\
move down 2.5; right",
    0, 0, 0, 0, 0, 0, 0, 0,
    1, 3, 0, 0, 0,

/* breve rest below middle line */
    "move to %d,%d; # breve rest\n\
line right 8; move up 0.5; line left 8; move up 0.5;\n\
line right 8; move up 0.5; line left 8; move up 0.5;\n\
line right 8;\n",
    X, 0, Y, -4, 0, 0, 0, 0,
    8, 8, 0, 1, 0,

/* SBRREST semibreve rest below 2nd line */
    "move to %d,%d.5; # semibreve rest\n\
line right 8; move up 0.5; line left 8; move up 0.5;\n\
line right 8; move up 0.5; line left 8; move up 0.5;\n\
line right 8;\n",
    X, 0, Y, 2, 0, 0, 0, 0,
    10, 10, 0, 1, 0,

/* MINREST minim rest above middle line */
    "move to %d.5, %d; # minim rest\n\
line right 8; move up 0.5; line left 8; move up 0.5;\n\
line right 8; move up 0.5; line left 8; move up 0.5;\n\
line right 8;\n",
    X, 0, Y, 2, 0, 0, 0, 0,
    10, 10, 0, 1, 0,

/* CROREST crochet rest */
    "move to %d,%d; # crochet rest\n\
spline right 6 down 3 then left 6 down 6 then right 6 down 3;\n\
spline left 6 then down 6 then right 6;",
    X, 0, Y, 9, 0, 0, 0, 0,
    6, 6, 0, 1, 0,

/* QUAREST quaver rest */
    "move to %d,%d; # quaver rest\n\
spline right 3 down 5.0 then right 3 up 5.0;\n\
spline left 3 down 6.0 then left 3 up 6.0;\n\
spline right 3 down 7.0 then right 3 up 7.0;\n\
move left 3 down 9\n\
line right 3 up 9; move down 3\n",
    X, 0, Y, 3, 0, 0, 0, 0,
    6, 6, 6, 1, 0,

/* semi-quaver rest */
    "move to %d,%d; # semi-qu rest\n\
spline right 3 down 5 then right 3 up 5;\n\
spline left 3 down 6 then left 3 up 6;\n\
spline right 3 down 7 then right 3 up 8;\n\
move left 8 down 6;\n\
spline down 5 right 3 then right 3 up 5;\n\
spline down 6 left 3 then left 3 up 6;\n\
spline down 7 right 3 then right 3 up 7;\n\
move left 3 down 9;\n\
line right 5 up 15; move down 6\n",
    X, 2, Y, 6, 0, 0, 0, 0,
    12, 10, 10, 1, 0,

/* demi-semi-quaver rest */
    "move to %d,%d; # demi-semi-qu rest\n\
spline right 3 down 5 then right 3 up 5;\n\
spline left 3 down 6 then left 3 up 6;\n\
spline right 3 down 7 then right 3 up 7;\n\
move left 8 down 6;\n\
spline right 3 down 5 then right 3 up 5;\n\
spline left 3 down 6 then left 3 up 6;\n\
spline right 3 down 7 then right 3 up 7;\n\
move left 8 down 6;\n\
spline right 3 down 5 then right 3 up 5;\n\
spline left 3 down 6 then left 3 up 6;\n\
spline right 3 down 7 then right 3 up 7;\n\
move left 3 down 9;\n\
line right 7 up 21; move down 9\n",
    X, 4, Y, 9, 0, 0, 0, 0,
    18, 12, 12, 1, 0,

/* semibreve note */
    "move to %d,%d; right; ellipse ht 5 wid 7; # semibreve\n\
move left 0.5; left; ellipse ht 5 wid 6;\n\
move right 0.5; right; ellipse ht 5 wid 5;",
    X, 0, Y, 0, 0, 0, 0, 0,
    5, 7, 7, 0, 0,

/* soprano clef */
    "",
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 22, 22, 1, put_sop_clef,

/* alto clef */
    "move to %d,%d; # alto clef\n\
line down 24; move right 0.5; line up 24; move right 0.5;\n\
line down 24; move right 3; line up 24;\n\
move right 6 down 3;\n\
spline left 3 then up 3 then right 9 then down 9 then left 6 then up 3;\n\
line down 6 left 6; line right 6 down 6;\n\
spline up 3 then right 6 then down 9 then left 9 then up 3 then right 3;\n\
move up 9 right 6;\n",
    X, 2, Y, 12, 0, 0, 0, 0,
    FULLSTAVE, 18, 1, 0, 0,

/* tenor clef */
    0,
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 22, 1, 1, put_ten_clef,

/* thick vertical line for ends */
    "move right 2; move down 12; \
	line up 24; move right 0.5; line down 24; move right 0.5;\
	line up 24; move right 0.5; line down 24; move right 0.5;\
	line up 24; move down 12;",
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 4, 4, 1, 0,

/* barline thick thin */
    0,
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 7, 7, 1, 0,

/* barline thin thick thin */
    0,
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 10, 10, 1, 0,

/* barline rept start |: */
    0,
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 11, 11, 1, putckr,

/* barline repeat both :||: */
    0,
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 18, 18, 1, putrckr,

/* hashed barline */
    0,
    0, 0, 0, 0, 0, 0, 0, 0,
    FULLSTAVE, 1, 1, 1, 0,

};


struct clefs    clef[] = {
/* pitch offset of middle line, atom no, character */
     /* dummy   */ 0, 0, 't',
     /* treble  */ 43, TCLEF, 't',
     /* bass	   */ 31, BCLEF, 'b',
     /* soprano */ 41, SCLEF, 's',
     /* altno   */ 37, ACLEF, 'a',
     /* tenor   */ 35, NCLEF, 'n',
};

struct notes    notev[NNOTES];	/* the notes */

struct bars barv[NBARS];	/* the bars */

struct staves   stavev[NSTAVES],
               *stavep;		/* the staves */

struct pieces   piecev;		/* the piece */

struct parts    partv[NPARTS],
               *partp;		/* the parts */


int     put_monitor = 0;
int     set_monitor = 0;
int     get_monitor = 0;
int     x_now,
        y_now;

char   *textp,
        textv[MAXTEXT];
int     atom_monitor = -1,
        up_down_border = 1;
float   totalheight = 0.0;

zaptext () {
    char   *p = textv,
           *q = 0,
            c1,
            c2;
    while (c1 = *p, p <= textp) {
	if (c1 == '\\' && (c2 = *(p + 1)) == '(') {
	    c1 = *(p + 2);
	    c2 = *(p + 3);
/* found "\(sh" etc */
	    if (c1 == 's' && c2 == 'h') {
		if (!q)
		    q = p;
		p += 3;
		*p = '#';
	    }
	    else
		if (c1 == 'f' && c2 == 't') {
		    if (!q)
			q = p;
		    p += 3;
		    *p = 'b';
		}
	}
	if (q) {
	    *q++ = *p;
	    if (*p == 0)
		q = 0;
	}
	p++;
    }
}
