#include "post_mus.h"

setties (stave) {		/* set ties for given stave */
    struct staves  *stavep = stavev + stave;
    int     s_first_bar = stavep -> s_first_bar,
            s_last_bar = stavep -> s_last_bar;
    struct bars *barp = barv + s_first_bar;
    int     bar,
            note,
            max = 0,
            in_tie = 0;
    struct notes   *notep;
    int     n_tie_start = barp -> b_first_note;
 /* for all bars on stave */
    for (bar = s_first_bar; bar < s_last_bar; bar++, barp++) {
	notep = notev + barp -> b_first_note;
    /* for each note in bar */
	for (note = barp -> b_first_note; note < barp -> b_last_note; note++, notep++) {
	    notep -> n_tie_height = 0;
	/* start of tie */
	    if (notep -> n_tie_start > 0) {
		in_tie += notep -> n_tie_start;
		n_tie_start = note;
		max = 0;
	    }
	/* any note */
	    if (max < notep -> n_maxy + in_tie * TIEHT) {
		max = notep -> n_maxy + in_tie * TIEHT;
	    }
	/* end of tie */
	    if (notep -> n_tie_end > 0) {
		settieht (n_tie_start, note + 1, max);
		in_tie -= notep -> n_tie_end;
		n_tie_start = max = 0;
	    }
	}			/* end for all notes */
    }				/* end for all bars */
    if (n_tie_start) {
	settieht (n_tie_start, note, max);
	notev[n_tie_start].n_tie_height = max;
    }
}

settieht (from, to, ht) {
    struct notes   *notep = notev + from;
    int     i;
    if (set_monitor > 3)
	fprintf (stderr,
		"set tie stave %d from %d to %d ht %d\n", stave, from, to, ht);
    for (i = from; i < to; i++, notep++) {
	notep -> n_tie_height = ht + TIEHT;
    }
}

settxhts (stave) {
    struct staves  *stavep = stavev + stave;
    int     s_first_bar = stavep -> s_first_bar,
            s_last_bar = stavep -> s_last_bar;
    struct bars *barp = barv + s_first_bar;
    int     bar,
            note,
            staff;
    int     over[NSTAFFS],
            under[NSTAFFS];
    int     overtxh[NSTAFFS],
            undertxh[NSTAFFS];
    int     maxy[NSTAFFS],
            miny[NSTAFFS];
    int     h;
    int     notemaxy,
            noteminy;
    struct notes   *notep;

    for (staff = 0; staff < n_staffs; staff++) {
	maxy[staff] = 18 + i_staff_gap / 2;/* lowest max */
	miny[staff] = -18 - i_staff_gap / 2;
	over[staff] = 14;	/* nearest text */
	under[staff] = -14;	/* nearest text */
	overtxh[staff] = undertxh[staff] = 0;
    }
    maxy[0] += i_stave_gap / 2;
    miny[n_staffs - 1] -= i_stave_gap / 2;

    if (s_first_bar == 1 && barv[0].b_last_note > barv[0].b_first_note) {
	s_first_bar = 0;
    }

 /* for all bars on stave */
    for (bar = s_first_bar, barp = barv + bar;
	    bar < s_last_bar;
	    bar++, barp++) {
    /* for each note in bar */
	for (note = barp -> b_first_note, notep = notev + note;
		note < barp -> b_last_note;
		note++, notep++) {
	    staff = notep -> n_staff;
	    notemaxy = notep -> n_y_wrt_staff + 3;
	    noteminy = notep -> n_y_wrt_staff - 3;
	    if ( notep->stickup > 0 ) /* stick up */
		notemaxy += atom[STICK].height - 3;
	    else if ( notep->stickup < 0 )
		noteminy -= atom[STICK].height - 3;
	    if (notep -> n_tnote) {
		h = textheight (notep -> n_tnote);
		if ( notep->stickup > 0 ) { /* stick up */
		    noteminy -= h;
		} else if ( notep->stickup < 0 ){ /* stick down */
		    notemaxy += h;
		} else { /* no stick */
		    if ( notep->n_y_wrt_staff > 0 )
			noteminy -= h;
		    else
			notemaxy += h;
		}
	    }
	    if (notep -> n_tover) {
		h = textheight (notep -> n_tover);
		if ( notep->n_yover < notemaxy )
		    notep->n_yover = notemaxy;
		notemaxy = notep -> n_yover + h;
		if (over[staff] < notep -> n_yover)
		    over[staff] = notep -> n_yover;
		if (overtxh[staff] < h)
		    overtxh[staff] = h;
	    }
	    if (notep -> n_tunder) {
		h = textheight (notep -> n_tunder);
		if ( notep->n_yunder > noteminy )
		    notep->n_yunder = noteminy;
		noteminy = notep -> n_yunder - h;
		if (under[staff] > notep -> n_yunder)
		    under[staff] = notep -> n_yunder;
		if (undertxh[staff] < h)
		    undertxh[staff] = h;
	    }
	    if (maxy[staff] < notemaxy)
		maxy[staff] = notemaxy;
	    if (miny[staff] > noteminy)
		miny[staff] = noteminy;
	}
    }
    for (bar = s_first_bar, barp = barv + bar;
	bar < s_last_bar;
	bar++, barp++) {
    /* for each note in bar */
	for (note = barp -> b_first_note, notep = notev + note;
		note < barp -> b_last_note;
		note++, notep++) {
	    staff = notep -> n_staff;
	    if (notep -> n_tover)
		notep -> n_yover = over[staff];
	    if (notep -> n_tunder)
		notep -> n_yunder = under[staff];
	}
    }

    for (staff = 0; staff < n_staffs; staff++) {
	if (maxy[staff] < over[staff] + overtxh[staff])
	    maxy[staff] = over[staff] + overtxh[staff];
	if (miny[staff] > under[staff] - undertxh[staff])
	    miny[staff] = under[staff] - undertxh[staff];
	staffv[stave][staff].sf_maxy = maxy[staff];
	staffv[stave][staff].sf_miny = miny[staff];
    }

    if (set_monitor == 10) {
	fprintf (stderr,
		"Stave %d:\n", stave);
	for (staff = 0; staff < n_staffs; staff++)
	    fprintf (stderr, "Staff %d: y[%d,%d], txty[%d,%d], txth[%d,%d]\n",
		    staff,
		    miny[staff], maxy[staff],
		    under[staff], over[staff],
		    undertxh[staff], overtxh[staff]);
	fprintf (stderr, "\n");
    }

}
