1 /*
2  * Music generator.
3  *
4  * This file is part of abcm2ps.
5  *
6  * Copyright (C) 1998-2017 Jean-François Moine
7  * Adapted from abc2ps, Copyright (C) 1996,1997 Michael Methfessel
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  */
14 
15 #include <string.h>
16 #include <stdlib.h>
17 #include <ctype.h>
18 
19 #include "abcm2ps.h"
20 
21 struct STAFF_S staff_tb[MAXSTAFF];	/* staff table */
22 struct SYMBOL *tsnext;		/* next line when cut */
23 float realwidth;		/* real staff width while generating */
24 
25 static int insert_meter;	/* insert time signature (1) and indent 1st line (2) */
26 static float beta_last;		/* for last short short line.. */
27 
28 #define AT_LEAST(a,b)  do { float tmp = b; if(a<tmp) a=tmp; } while (0)
29 
30 /* width of notes indexed by log2(note_length) */
31 float space_tb[NFLAGS_SZ] = {
32 	7, 10, 14.15, 20, 28.3,
33 	40,				/* crotchet */
34 	56.6, 80, 113, 150
35 };
36 // width of note heads indexed by s->head
37 float hw_tb[] = {4.5, 5, 6, 8};
38 static int smallest_duration;
39 
40 /* upper and lower space needed by rests */
41 static char rest_sp[NFLAGS_SZ][2] = {
42 	{18, 18},
43 	{12, 18},
44 	{12, 12},
45 	{8, 12},
46 	{6, 8},
47 	{10, 10},			/* crotchet */
48 	{6, 4},
49 	{10, 0},
50 	{10, 4},
51 	{10, 10}
52 };
53 
54 /* set the head of the notes */
set_heads(struct SYMBOL * s)55 static float set_heads(struct SYMBOL *s)
56 {
57 	struct note *note;
58 	int i, m, n;
59 	char *p, *q, *r;
60 	float w, wmax;
61 	static float dx_tb[4] = {
62 		7, 8, 10, 13.3
63 	};
64 
65 	n = s->nhd;
66 	wmax = -1;
67 	for (m = 0; m <= n; m++) {
68 		note = &s->u.note.notes[m];
69 		p = note->head;			/* list of heads from parsing */
70 		if (!p)
71 			continue;
72 		i = s->head;
73 		for (;;) {		// search the head for the duration
74 			q = strchr(p, ',');
75 			if (!q)
76 				break;
77 			if (--i < 0)
78 				break;
79 			p = q + 1;
80 		}
81 		if (!q)
82 			q = p + strlen(p);
83 		r = strchr(p, '/');
84 		if (r && r < q) {	// search the head for the stem direction
85 			if (s->stem >= 0)
86 				q = r;
87 			else
88 				p = r + 1;
89 		}
90 		r = strchr(p, ':');		// width separator
91 		if (r && r < q) {
92 			q = r;
93 			sscanf(r, ":%f", &w);
94 			if (w > wmax)
95 				wmax = w;
96 		}
97 		note->head = p;
98 		note->hlen = q - p;
99 	}
100 	if (wmax < 0) {
101 		wmax = dx_tb[s->head];
102 		if (s->dur >= BASE_LEN * 2 && s->head == H_OVAL)
103 			wmax = 13.8;
104 	}
105 	s->u.note.sdx = wmax / 2;		// stem offset
106 	return wmax;
107 }
108 
109 /* -- decide whether to shift heads to other side of stem on chords -- */
110 /* and set the head of the notes */
111 /* this routine is called only once per tune for normal notes
112  * it is called on setting symbol width for grace notes */
set_head_shift(struct SYMBOL * s)113 static void set_head_shift(struct SYMBOL *s)
114 {
115 	int i, i1, i2, n, sig, d, shift, ps;
116 	float dx, dx_max, dx_head;
117 //	unsigned char ax_tb[MAXHD], ac_tb[MAXHD];
118 	/* distance for no overlap - index: [prev acc][cur acc] */
119 //	static char dt_tb[4][4] = {
120 //		{5, 5, 5, 5},		/* dble sharp */
121 //		{5, 6, 6, 6},		/* sharp */
122 //		{5, 6, 5, 6},		/* natural */
123 //		{5, 5, 5, 5}		/* flat */
124 //	};
125 
126 	/* set the heads of the notes with mapping */
127 	dx_head = set_heads(s) + 2;
128 
129 	n = s->nhd;
130 	if (n == 0)
131 		return;			// single note
132 
133 	/* set the head shifts */
134 	dx = dx_head * 0.78;
135 	if (s->flags & ABC_F_GRACE)
136 		dx *= 0.5;
137 	sig = s->stem;
138 	if (sig >= 0) {
139 		i1 = 1;
140 		i2 = n + 1;
141 		ps = s->pits[0];
142 	} else {
143 		dx = -dx;
144 		i1 = n - 1;
145 		i2 = -1;
146 		ps = s->pits[n];
147 	}
148 	shift = 0;
149 	dx_max = 0;
150 	for (i = i1; i != i2; i += sig) {
151 		d = s->pits[i] - ps;
152 		ps = s->pits[i];
153 		if (d == 0) {
154 			if (shift) {		/* unison on shifted note */
155 				float new_dx = s->u.note.notes[i].shhd =
156 					s->u.note.notes[i - sig].shhd + dx;
157 				if (dx_max < new_dx)
158 					dx_max = new_dx;
159 				continue;
160 			}
161 			if (i + sig != i2	/* second after unison */
162 //fixme: should handle many unisons after second
163 			 && ps + sig == s->pits[i + sig]) {
164 				s->u.note.notes[i].shhd = -dx;
165 				if (dx_max < -dx)
166 					dx_max = -dx;
167 				continue;
168 			}
169 		}
170 		if (d < 0)
171 			d = -d;
172 		if (d > 3 || (d >= 2 && s->head < H_SQUARE)) {
173 			shift = 0;
174 		} else {
175 			shift = !shift;
176 			if (shift) {
177 				s->u.note.notes[i].shhd = dx;
178 				if (dx_max < dx)
179 					dx_max = dx;
180 			}
181 		}
182 	}
183 	s->xmx = dx_max;				/* shift the dots */
184 }
185 
186 // set the accidental shifts for a set of chords
acc_shift(struct note * notes[],int n,float dx_head)187 static void acc_shift(struct note *notes[], int n, float dx_head)
188 {
189 	int i, i1, ps, p1, acc;
190 	float dx, dx1;
191 
192 	// set the shifts from the head shifts
193 	for (i = n - 1; --i >= 0; ) {	// (no shift on top)
194 		dx = notes[i]->shhd;
195 		if (dx == 0 || dx > 0)
196 			continue;
197 		dx = dx_head - dx;
198 		ps = notes[i]->pit;
199 		for (i1 = n; --i1 >= 0; ) {
200 			if (!notes[i1]->acc)
201 				continue;
202 			p1 = notes[i1]->pit;
203 			if (p1 < ps - 3)
204 				break;
205 			if (p1 > ps + 3)
206 				continue;
207 			if (notes[i1]->shac < dx)
208 				notes[i1]->shac = dx;
209 		}
210 	}
211 	for (i = n; --i >= 0; ) {		// from top to bottom
212 		acc = notes[i]->acc;
213 
214 		if (!acc)
215 			continue;
216 		dx = notes[i]->shac;
217 		if (dx == 0) {
218 			dx = notes[i]->shhd;
219 			if (dx < 0)
220 				dx = dx_head - dx;
221 			else
222 				dx = dx_head;
223 		}
224 		ps = notes[i]->pit;
225 		for (i1 = n; --i1 > i; ) {
226 			if (!notes[i1]->acc)
227 				continue;
228 			p1 = notes[i1]->pit;
229 			if (p1 >= ps + 4) {	// pitch far enough
230 				if (p1 > ps + 4) // if more than a fifth
231 					continue;
232 				switch (acc) {
233 				case A_NULL:
234 				case A_FT:
235 				case A_DF:
236 					continue;
237 				}
238 				switch (notes[i1]->acc) {
239 				case A_NULL:
240 				case A_FT:
241 				case A_DF:
242 					continue;
243 				}
244 			}
245 			if (dx > notes[i1]->shac - 6) {
246 				dx1 = notes[i1]->shac + 7;
247 				if (dx1 > dx)
248 					dx = dx1;
249 			}
250 		}
251 		notes[i]->shac = dx;
252 	}
253 }
254 
255 /* set the horizontal shift of accidentals */
256 /* this routine is called only once per tune */
set_acc_shft(void)257 static void set_acc_shft(void)
258 {
259 	struct SYMBOL *s, *s2;
260 	int i, staff, t, acc, n, nx;
261 	float dx_head;
262 	struct note *notes[MAXHD * 4];	// (max = 4 voices per staff)
263 	struct note *nt;
264 
265 	s = tsfirst;
266 	while (s) {
267 		if (s->abc_type != ABC_T_NOTE
268 		 || (s->flags & ABC_F_INVIS)) {
269 			s = s->ts_next;
270 			continue;
271 		}
272 		staff = s->staff;
273 		t = s->time;
274 		acc = 0;
275 		for (s2 = s; s2; s2 = s2->ts_next) {
276 			if (s2->time != t
277 			 || s2->abc_type != ABC_T_NOTE
278 			 || s2->staff != staff)
279 				break;
280 			if (acc)
281 				continue;
282 			for (i = 0; i <= s2->nhd; i++) {
283 				if (s2->u.note.notes[i].acc) {
284 					acc = 1;
285 					continue;
286 				}
287 			}
288 		}
289 		if (!acc) {
290 			s = s2;
291 			continue;
292 		}
293 
294 		dx_head = set_heads(s) + 2;
295 		n = 0;
296 		for ( ; s != s2; s = s->ts_next) {
297 			for (i = 0; i <= s->nhd; i++)
298 				notes[n++] = &s->u.note.notes[i];
299 		}
300 
301 		// sort the notes
302 		for (;;) {
303 			nx = 0;
304 			for (i = 1; i < n; i++) {
305 				if (notes[i]->pit >= notes[i - 1]->pit)
306 					continue;
307 				nt = notes[i];
308 				notes[i] = notes[i - 1];
309 				notes[i - 1] = nt;
310 				nx++;
311 			}
312 			if (nx == 0)
313 				break;
314 		}
315 		acc_shift(notes, n, dx_head);
316 	}
317 }
318 
319 /* -- unlink a symbol -- */
unlksym(struct SYMBOL * s)320 void unlksym(struct SYMBOL *s)
321 {
322 //	if (!s->next) {
323 //		if (s->extra) {
324 //			s->type = FMTCHG;
325 //			s->aux = -1;
326 //			return;
327 //		}
328 //	} else {
329 	if (s->next) {
330 		s->next->prev = s->prev;
331 //		if (s->extra) {
332 //			struct SYMBOL *g;
333 //
334 //			g = s->next->extra;
335 //			if (!g) {
336 //				s->next->extra = s->extra;
337 //			} else {
338 //				for (; g->next; g = g->next)
339 //					;
340 //				g->next = s->extra;
341 //			}
342 //		}
343 	}
344 	if (s->prev)
345 		s->prev->next = s->next;
346 	else
347 		voice_tb[s->voice].sym = s->next;
348 	if (s->ts_next) {
349 		if (s->extra) {
350 			struct SYMBOL *g;
351 
352 			g = s->ts_next->extra;
353 			if (!g) {
354 				s->ts_next->extra = s->extra;
355 			} else {
356 				for (; g->next; g = g->next)
357 					;
358 				g->next = s->extra;
359 			}
360 		}
361 		if ((s->sflags & S_SEQST)
362 		 && !(s->ts_next->sflags & S_SEQST)) {
363 			s->ts_next->sflags |= S_SEQST;
364 			s->ts_next->shrink = s->shrink;
365 			s->ts_next->space = s->space;
366 		}
367 		if (s->sflags & S_NEW_SY)
368 			s->ts_next->sflags |= S_NEW_SY;
369 		s->ts_next->ts_prev = s->ts_prev;
370 	}
371 	if (s->ts_prev)
372 		s->ts_prev->ts_next = s->ts_next;
373 	if (tsfirst == s)
374 		tsfirst = s->ts_next;
375 	if (tsnext == s)
376 		tsnext = s->ts_next;
377 }
378 
379 /* -- check if voice combine may occur -- */
may_combine(struct SYMBOL * s)380 static int may_combine(struct SYMBOL *s)
381 {
382 	struct SYMBOL *s2;
383 	int nhd2;
384 
385 	s2 = s->ts_next;
386 	if (!s2 || s2->type != NOTEREST)
387 		return 0;
388 	if (s2->voice == s->voice
389 	 || s2->staff != s->staff
390 	 || s2->time != s->time
391 	 || s2->dur != s->dur)
392 		return 0;
393 	if (s->combine <= 0
394 	 && s2->abc_type != s->abc_type)
395 		return 0;
396 	if (s->u.note.dc.n + s2->u.note.dc.n >= MAXDC)
397 		return 0;
398 //fixme: should check the double decorations
399 	if (s->gch && s2->gch)
400 		return 0;
401 	if (s->abc_type == ABC_T_REST) {
402 		if (s2->abc_type  == ABC_T_REST
403 		 && (s->flags & ABC_F_INVIS) && !(s2->flags & ABC_F_INVIS))
404 			return 0;
405 		return 1;
406 	}
407 	if (s2->ly
408 	 || (s2->sflags & (S_SL1 | S_SL2))
409 	 || s2->u.note.slur_st != 0
410 	 || s2->u.note.slur_end != 0)
411 		return 0;
412 	if ((s2->sflags ^ s->sflags) & (S_BEAM_ST | S_BEAM_END))
413 		return 0;
414 	nhd2 = s2->nhd;
415 	if (s->nhd + nhd2 + 1 >= MAXHD)
416 		return 0;
417 	if (s->combine <= 1
418 	 && s->pits[0] <= s2->pits[nhd2] + 1)
419 		return 0;
420 	return 1;
421 }
422 
423 /* -- combine 2 voices -- */
do_combine(struct SYMBOL * s)424 static void do_combine(struct SYMBOL *s)
425 {
426 	struct SYMBOL *s2;
427 	int i, m, nhd, nhd2, type;
428 
429 again:
430 	nhd = s->nhd;
431 	s2 = s->ts_next;
432 	s2->extra = NULL;
433 	if (s->abc_type != s2->abc_type) {	/* if note and rest */
434 		if (s2->abc_type != ABC_T_REST) {
435 			s2 = s;
436 			s = s2->ts_next;
437 		}
438 		goto delsym2;
439 	}
440 	if (s->abc_type == ABC_T_REST) {
441 		if ((s->flags & ABC_F_INVIS)
442 		 && !(s2->flags & ABC_F_INVIS))
443 			s->flags &= ~ABC_F_INVIS;
444 		goto delsym2;
445 	}
446 
447 	/* combine the voices */
448 	nhd2 = s2->nhd + 1;
449 	memcpy(&s->u.note.notes[nhd + 1],
450 		s2->u.note.notes,
451 		sizeof s->u.note.notes[0] * nhd2);
452 	memcpy(&s->pits[nhd + 1], s2->pits,
453 		sizeof s->pits[0] * (nhd2 + 1));
454 	s->sflags |= s2->sflags & (S_SL1 | S_SL2 | S_TI1);
455 
456 	nhd += nhd2;
457 	s->nhd = nhd;
458 
459 	sort_pitch(s);			/* sort the notes by pitch */
460 
461 	if (s->combine >= 3) {		// remove unison heads
462 		for (m = nhd; m > 0; m--) {
463 			if (s->u.note.notes[m].pit == s->u.note.notes[m - 1].pit
464 			 && s->u.note.notes[m].acc == s->u.note.notes[m - 1].acc) {
465 				memmove(&s->u.note.notes[m - 1],
466 					&s->u.note.notes[m],
467 					sizeof s->u.note.notes[0]);
468 				memmove(&s->pits[m - 1], &s->pits[m],
469 					sizeof s->pits[0]);
470 				s->nhd = --nhd;
471 			}
472 		}
473 	}
474 
475 	s->ymx = 3 * (s->pits[nhd] - 18) + 4;
476 	s->ymn = 3 * (s->pits[0] - 18) - 4;
477 
478 	/* force the tie directions */
479 	type = s->u.note.notes[0].ti1;
480 	if ((type & 0x0f) == SL_AUTO)
481 		s->u.note.notes[0].ti1 = SL_BELOW | (type & ~SL_DOTTED);
482 	type = s->u.note.notes[nhd].ti1;
483 	if ((type & 0x0f) == SL_AUTO)
484 		s->u.note.notes[nhd].ti1 = SL_ABOVE | (type & ~SL_DOTTED);
485 delsym2:
486 	if (s2->text && !s->text) {
487 		s->text = s2->text;
488 		s->gch = s2->gch;
489 	}
490 	if (s2->u.note.dc.n > 0) {	// update the added decorations
491 		int n;
492 
493 		for (i = 0; i < s2->u.note.dc.n; i++) {
494 			if (s2->u.note.dc.tm[i].m >= 0)
495 				s2->u.note.dc.tm[i].m += nhd + 1;
496 		}
497 		n = s->u.note.dc.n;
498 		memcpy(&s->u.note.dc.tm[n],
499 			s2->u.note.dc.tm,
500 			sizeof s->u.note.dc.tm[0] * s2->u.note.dc.n);
501 		s->u.note.dc.n += s2->u.note.dc.n;
502 	}
503 	unlksym(s2);			/* remove the next symbol */
504 
505 	/* there may be more voices */
506 	if (!(s->sflags & S_IN_TUPLET) && may_combine(s))
507 		goto again;
508 }
509 
510 /* -- try to combine voices */
combine_voices(void)511 static void combine_voices(void)
512 {
513 	struct SYMBOL *s, *s2, *g;
514 	int i, r;
515 
516 	for (s = tsfirst; s->ts_next; s = s->ts_next) {
517 		if (s->combine < 0)
518 			continue;
519 		if (s->combine == 0
520 		 && s->abc_type != ABC_T_REST)
521 			continue;
522 		if (s->sflags & S_IN_TUPLET) {
523 			g = s->extra;
524 			if (!g)
525 				continue;	/* tuplet already treated */
526 			r = 0;
527 			for ( ; g; g = g->next) {
528 				if (g->type == TUPLET
529 				 && g->u.tuplet.r_plet > r)
530 					r = g->u.tuplet.r_plet;
531 			}
532 			if (r == 0)
533 				continue;
534 			i = r;
535 			for (s2 = s; s2; s2 = s2->next) {
536 				if (!s2->ts_next)
537 					break;
538 				if (s2->type != NOTEREST)
539 					continue;
540 				if (!may_combine(s2))
541 					break;
542 				if (--i <= 0)
543 					break;
544 			}
545 			if (i > 0)
546 				continue;
547 			for (s2 = s; /*s2*/; s2 = s2->next) {
548 				if (s2->type != NOTEREST)
549 					continue;
550 				do_combine(s2);
551 				if (--r <= 0)
552 					break;
553 			}
554 			continue;
555 
556 		}
557 		if (s->type != NOTEREST)
558 			continue;
559 
560 		if (s->abc_type == ABC_T_NOTE) {
561 			if (!(s->sflags & S_BEAM_ST))
562 				continue;
563 			if (s->sflags & S_BEAM_END) {
564 				if (may_combine(s))
565 					do_combine(s);
566 				continue;
567 			}
568 		} else {
569 			if (may_combine(s))
570 				do_combine(s);
571 			continue;
572 		}
573 		s2 = s;
574 		for (;;) {
575 			if (!may_combine(s2)) {
576 				s2 = NULL;
577 				break;
578 			}
579 //fixme: may have rests in beam
580 			if (s2->sflags & S_BEAM_END)
581 				break;
582 			do {
583 				s2 = s2->next;
584 			} while (s2->type != NOTEREST);
585 		}
586 		if (!s2)
587 			continue;
588 		s2 = s;
589 		for (;;) {
590 			do_combine(s2);
591 //fixme: may have rests in beam
592 			if (s2->sflags & S_BEAM_END)
593 				break;
594 			do {
595 				s2 = s2->next;
596 			} while (s2->type != NOTEREST);
597 		}
598 	}
599 }
600 
601 /* -- insert a clef change (treble or bass) before a symbol -- */
insert_clef(struct SYMBOL * s,int clef_type,int clef_line)602 static struct SYMBOL *insert_clef(struct SYMBOL *s,
603 				int clef_type,
604 				int clef_line)
605 {
606 	struct VOICE_S *p_voice;
607 	struct SYMBOL *new_s;
608 	int staff;
609 
610 	staff = s->staff;
611 
612 	/* don't insert the clef between two bars */
613 	if (s->type == BAR && s->prev && s->prev->type == BAR
614 /*	 && s->time == s->prev->time */
615 			)
616 		s = s->prev;
617 
618 	/* create the symbol */
619 	p_voice = &voice_tb[s->voice];
620 	p_voice->last_sym = s->prev;
621 	if (!p_voice->last_sym)
622 		p_voice->sym = NULL;
623 	p_voice->time = s->time;
624 	new_s = sym_add(p_voice, CLEF);
625 	new_s->next = s;
626 	s->prev = new_s;
627 
628 	new_s->u.clef.type = clef_type;
629 	new_s->u.clef.line = clef_line;
630 	new_s->staff = staff;
631 	new_s->aux = 1;			/* small clef */
632 	new_s->sflags &= ~S_SECOND;
633 
634 	/* link in time */
635 	while (!(s->sflags & S_SEQST))
636 		s = s->ts_prev;
637 //	if (!s->ts_prev || s->ts_prev->type != CLEF)
638 	if (s->ts_prev->type != CLEF)
639 		new_s->sflags |= S_SEQST;
640 	new_s->ts_prev = s->ts_prev;
641 //	if (new_s->ts_prev)
642 		new_s->ts_prev->ts_next = new_s;
643 //	else
644 //		tsfirst = new_s;
645 	new_s->ts_next = s;
646 	s->ts_prev = new_s;
647 	return new_s;
648 }
649 
650 /* -- set the staff of the floating voices -- */
651 /* this function is called only once per tune */
set_float(void)652 static void set_float(void)
653 {
654 	struct VOICE_S *p_voice;
655 	int staff, staff_chg;
656 	struct SYMBOL *s, *s1;
657 
658 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
659 		if (!p_voice->floating)
660 			continue;
661 		staff_chg = 0;
662 		staff = p_voice->staff;
663 		for (s = p_voice->sym; s; s = s->next) {
664 			signed char up, down;
665 
666 			if (!s->dur) {
667 				if (staff_chg)
668 					s->staff++;
669 				continue;
670 			}
671 			if (!(s->sflags & S_FLOATING)) {
672 				staff_chg = 0;
673 				continue;
674 			}
675 			if (s->pits[0] >= 19) {		/* F */
676 				staff_chg = 0;
677 				continue;
678 			}
679 			if (s->pits[s->nhd] <= 12) {	/* F, */
680 				staff_chg = 1;
681 				s->staff++;
682 				continue;
683 			}
684 			up = 127;
685 			for (s1 = s->ts_prev; s1; s1 = s1->ts_prev) {
686 				if (s1->staff != staff
687 				 || s1->voice == s->voice)
688 					break;
689 #if 1
690 /*fixme:test again*/
691 				if (s1->abc_type == ABC_T_NOTE)
692 #endif
693 				    if (s1->pits[0] < up)
694 					up = s1->pits[0];
695 			}
696 			if (up == 127) {
697 				if (staff_chg)
698 					s->staff++;
699 				continue;
700 			}
701 			if (s->pits[s->nhd] > up - 3) {
702 				staff_chg = 0;
703 				continue;
704 			}
705 			down = -127;
706 			for (s1 = s->ts_next; s1; s1 = s1->ts_next) {
707 				if (s1->staff != staff + 1
708 				 || s1->voice == s->voice)
709 					break;
710 #if 1
711 /*fixme:test again*/
712 				if (s1->abc_type == ABC_T_NOTE)
713 #endif
714 				    if (s1->pits[s1->nhd] > down)
715 					down = s1->pits[s1->nhd];
716 			}
717 			if (down == -127) {
718 				if (staff_chg)
719 					s->staff++;
720 				continue;
721 			}
722 			if (s->pits[0] < down + 3) {
723 				staff_chg = 1;
724 				s->staff++;
725 				continue;
726 			}
727 			up -= s->pits[s->nhd];
728 			down = s->pits[0] - down;
729 			if (!staff_chg) {
730 				if (up < down + 3)
731 					continue;
732 				staff_chg = 1;
733 			} else {
734 				if (up < down - 3) {
735 					staff_chg = 0;
736 					continue;
737 				}
738 			}
739 			s->staff++;
740 		}
741 	}
742 }
743 
744 /* -- set the x offset of the grace notes -- */
set_graceoffs(struct SYMBOL * s)745 static float set_graceoffs(struct SYMBOL *s)
746 {
747 	struct SYMBOL *g, *next;
748 	int m;
749 	float xx, dx, gspleft, gspinside, gspright;
750 	struct note *notes[MAXHD];
751 
752 	gspleft = (cfmt.gracespace >> 16) * 0.1;
753 	gspinside = ((cfmt.gracespace >> 8) & 0xff) * 0.1;
754 	gspright = (cfmt.gracespace & 0xff) * 0.1;
755 	xx = 0;
756 	for (g = s->extra; ; g = g->next) {
757 		if (g->type == NOTEREST)
758 			break;
759 	}
760 	g->sflags |= S_BEAM_ST;
761 	for ( ; ; g = g->next) {
762 		if (g->type != NOTEREST) {
763 			if (!g->next)
764 				break;
765 			continue;
766 		}
767 		set_head_shift(g);
768 		for (m = 0; m <= g->nhd; m++)
769 			notes[m] = &g->u.note.notes[m];
770 		acc_shift(notes, g->nhd + 1, 7);
771 		dx = 0;
772 		for (m = g->nhd; m >= 0; m--) {
773 			if (g->u.note.notes[m].shac > dx)
774 				dx = g->u.note.notes[m].shac;
775 		}
776 		xx += dx;
777 		g->x = xx;
778 
779 		if (g->nflags <= 0)
780 			g->sflags |= S_BEAM_ST | S_BEAM_END;
781 		next = g->next;
782 		if (!next) {
783 			g->sflags |= S_BEAM_END;
784 			break;
785 		}
786 		if (next->nflags <= 0 || (next->flags & ABC_F_SPACE))
787 			g->sflags |= S_BEAM_END;
788 		if (g->sflags & S_BEAM_END) {
789 			next->sflags |= S_BEAM_ST;
790 			xx += gspinside / 4;
791 		}
792 		if (g->nflags <= 0)
793 			xx += gspinside / 4;
794 		if (g->y > next->y + 8)
795 			xx -= 1.5;
796 		xx += gspinside;
797 	}
798 
799 	xx += gspleft + gspright;
800 	next = s->next;
801 	if (next
802 	 && next->abc_type == ABC_T_NOTE) {	/* if before a note */
803 		if (g->y >= 3 * (next->pits[next->nhd] - 18))
804 			xx -= 1;		/* above, a bit closer */
805 		else if ((g->sflags & S_BEAM_ST)
806 		      && g->y < 3 * (next->pits[0] - 18) - 7)
807 			xx += 2;	/* below with flag, a bit further */
808 	}
809 
810 	/* return the whole width */
811 	return xx;
812 }
813 
814 /* -- compute the width needed by the guitar chords / annotations -- */
gchord_width(struct SYMBOL * s,float wlnote,float wlw)815 static float gchord_width(struct SYMBOL *s,
816 			  float wlnote,
817 			  float wlw)
818 {
819 	struct SYMBOL *s2;
820 	struct gch *gch;
821 	int ix;
822 	float lspc, rspc, w, alspc, arspc;
823 
824 	lspc = rspc = alspc = arspc = 0;
825 	for (ix = 0, gch = s->gch; ix < MAXGCH; ix++, gch++) {
826 		if (gch->type == '\0')
827 			break;
828 		switch (gch->type) {
829 		default: {		/* default = above */
830 			float wl;
831 
832 			wl = -gch->x;
833 			if (wl > lspc)
834 				lspc = wl;
835 			w = gch->w + 2 - wl;
836 			if (w > rspc)
837 				rspc = w;
838 			break;
839 		    }
840 		case '<':		/* left */
841 			w = gch->w + wlnote;
842 			if (w > alspc)
843 				alspc = w;
844 			break;
845 		case '>':		/* right */
846 			w = gch->w + s->wr;
847 			if (w > arspc)
848 				arspc = w;
849 			break;
850 		}
851 	}
852 
853 	/* adjust width for no clash */
854 	s2 = s->prev;
855 	if (s2) {
856 		if (s2->gch) {
857 			for (s2 = s->ts_prev; ; s2 = s2->ts_prev) {
858 				if (s2 == s->prev) {
859 					AT_LEAST(wlw, lspc);
860 					break;
861 				}
862 				if (s2->sflags & S_SEQST)
863 					lspc -= s2->shrink;
864 			}
865 		}
866 		if (alspc != 0)
867 			AT_LEAST(wlw, alspc);
868 	}
869 	s2 = s->next;
870 	if (s2) {
871 		if (s2->gch) {
872 			for (s2 = s->ts_next; ; s2 = s2->ts_next) {
873 				if (s2 == s->next) {
874 					AT_LEAST(s->wr, rspc);
875 					break;
876 				}
877 				if (s2->sflags & S_SEQST)
878 					rspc -= 8;
879 			}
880 		}
881 		if (arspc != 0)
882 			AT_LEAST(s->wr, arspc);
883 	}
884 	return wlw;
885 }
886 
887 /* -- set the width needed by the lyrics -- */
ly_width(struct SYMBOL * s,float wlw)888 static float ly_width(struct SYMBOL *s, float wlw)
889 {
890 	struct SYMBOL *k;
891 	struct lyrics *ly = s->ly;
892 	struct lyl *lyl;
893 	struct tblt_s *tblt;
894 	float align, xx, w;
895 	int i;
896 
897 	/* check if the lyrics contain tablature definition */
898 	for (i = 0; i < 2; i++) {
899 		tblt = voice_tb[s->voice].tblts[i];
900 		if (!tblt)
901 			continue;
902 		if (tblt->pitch == 0) {		/* yes, no width */
903 			for (i = 0; i < MAXLY; i++) {
904 				if ((lyl = ly->lyl[i]) == NULL)
905 					continue;
906 				lyl->s = 0;
907 			}
908 			return wlw;
909 		}
910 	}
911 
912 	align = 0;
913 	for (i = 0; i < MAXLY; i++) {
914 		float swfac, shift;
915 		char *p;
916 
917 		lyl = ly->lyl[i];
918 		if (!lyl)
919 			continue;
920 		p = lyl->t;
921 		w = lyl->w;
922 		swfac = lyl->f->swfac;
923 		xx = w + 2 * cwid(' ') * swfac;
924 		if (s->type == GRACE) {			// %%graceword
925 			shift = s->wl;
926 		} else if ((isdigit((unsigned char) *p) && strlen(p) > 2)
927 		 || p[1] == ':'
928 		 || *p == '(' || *p == ')') {
929 			float sz;
930 
931 			if (*p == '(') {
932 				sz = cwid((unsigned char) *p);
933 			} else {
934 				sz = 0;
935 				while (*p != '\0') {
936 /*fixme: KO when '\ooo'*/
937 					if (*p == '\\') {
938 						p++;
939 						continue;
940 					}
941 					sz += cwid((unsigned char) *p);
942 					if (*p == ' ')
943 						break;
944 					p++;
945 				}
946 			}
947 			sz *= swfac;
948 			shift = (w - sz + 2 * cwid(' ') * swfac)
949 				* VOCPRE;
950 			if (shift > 20)
951 				shift = 20;
952 			shift += sz;
953 			if (isdigit((unsigned char) lyl->t[0])) {
954 				if (shift > align)
955 					align = shift;
956 			}
957 		} else if (*p == LY_HYPH || *p == LY_UNDER) {
958 			shift = 0;
959 		} else {
960 			shift = xx * VOCPRE;
961 			if (shift > 20)
962 				shift = 20;
963 		}
964 		lyl->s = shift;
965 		AT_LEAST(wlw, shift);
966 		xx -= shift;
967 		shift = 2 * cwid(' ') * swfac;
968 		for (k = s->next; k; k = k->next) {
969 			switch (k->type) {
970 			case NOTEREST:
971 				if (!k->ly
972 				 || !k->ly->lyl[i])
973 					xx -= 9;
974 				else if (k->ly->lyl[i]->t[0] == LY_HYPH
975 				      || k->ly->lyl[i]->t[0] == LY_UNDER)
976 					xx -= shift;
977 				else
978 					break;
979 				if (xx <= 0)
980 					break;
981 				continue;
982 			case CLEF:
983 			case TIMESIG:
984 			case KEYSIG:
985 				xx -= 10;
986 				continue;
987 			default:
988 				xx -= 5;
989 				break;
990 			}
991 			break;
992 		}
993 		if (xx > s->wr)
994 			s->wr = xx;
995 		}
996 	if (align > 0) {
997 		for (i = 0; i < MAXLY; i++) {
998 			if ((lyl = ly->lyl[i]) == 0)
999 				continue;
1000 			if (isdigit((unsigned char) lyl->t[0]))
1001 				lyl->s = align;
1002 		}
1003 	}
1004 	return wlw;
1005 }
1006 
1007 /* -- set the width of a symbol -- */
1008 /* This routine sets the minimal left and right widths wl,wr
1009  * so that successive symbols are still separated when
1010  * no extra glue is put between them */
set_width(struct SYMBOL * s)1011 static void set_width(struct SYMBOL *s)
1012 {
1013 	struct SYMBOL *s2;
1014 	int i, m;
1015 	float xx, w, wlnote, wlw;
1016 
1017 	switch (s->type) {
1018 	case NOTEREST:
1019 
1020 		/* set the note widths */
1021 		s->wr = wlnote = hw_tb[s->head];
1022 
1023 		/* room for shifted heads and accidental signs */
1024 		if (s->xmx > 0)
1025 			s->wr += s->xmx + 4;
1026 		s2 = s->prev;
1027 		if (s2) {
1028 			switch (s2->type) {
1029 			case BAR:
1030 			case CLEF:
1031 			case KEYSIG:
1032 			case TIMESIG:
1033 				wlnote += 3;
1034 				break;
1035 			}
1036 		}
1037 		for (m = 0; m <= s->nhd; m++) {
1038 			xx = s->u.note.notes[m].shhd;
1039 			if (xx < 0)
1040 				AT_LEAST(wlnote, -xx + 5);
1041 			if (s->u.note.notes[m].acc) {
1042 				AT_LEAST(wlnote, s->u.note.notes[m].shac
1043 					 + ((s->u.note.notes[m].acc & 0xf8)
1044 					    ? 6.5 : 4.5));
1045 			}
1046 		}
1047 		if (s2) {
1048 			switch (s2->type) {
1049 			case BAR:
1050 			case CLEF:
1051 			case KEYSIG:
1052 			case TIMESIG:
1053 				wlnote -= 3;
1054 				break;
1055 			}
1056 		}
1057 
1058 		/* room for the decorations */
1059 		if (s->u.note.dc.n != 0)
1060 			wlnote += deco_width(s);
1061 
1062 		/* space for flag if stem goes up on standalone note */
1063 		if ((s->sflags & (S_BEAM_ST | S_BEAM_END)) == (S_BEAM_ST | S_BEAM_END)
1064 		 && s->stem > 0 && s->nflags > 0)
1065 			AT_LEAST(s->wr, s->xmx + 9);	// was 12, then removed, then back again
1066 
1067 		/* leave room for dots and set their offset */
1068 		if (s->dots > 0) {
1069 			switch (s->head) {
1070 			case H_SQUARE:
1071 			case H_OVAL:
1072 				s->xmx += 2;
1073 				break;
1074 			case H_EMPTY:
1075 				s->xmx += 1;
1076 				break;
1077 			}
1078 			AT_LEAST(s->wr, s->xmx + 12);
1079 			if (s->dots >= 2)
1080 				s->wr += 3.5 * (s->dots - 1);
1081 		}
1082 
1083 		/* if a tremolo on 2 notes, have space for the small beam(s) */
1084 		if ((s->sflags & (S_TREM2 | S_BEAM_END)) == (S_TREM2 | S_BEAM_END))
1085 			AT_LEAST(wlnote, 20);
1086 
1087 		wlw = wlnote;
1088 
1089 		if (s2) {
1090 			switch (s2->type) {
1091 			case NOTEREST:	/* extra space when up stem - down stem */
1092 				if (s2->abc_type == ABC_T_REST)
1093 					break;
1094 				if (s2->stem > 0 && s->stem < 0)
1095 					AT_LEAST(wlw, 7);
1096 
1097 				/* make sure helper lines don't overlap */
1098 				if ((s->y > 27 && s2->y > 27)
1099 				 || (s->y < -3 && s2->y < -3))
1100 					AT_LEAST(wlw, 6);
1101 
1102 				/* have ties wide enough */
1103 				if (s2->sflags & S_TI1)
1104 					AT_LEAST(wlw, 14);
1105 				break;
1106 			case CLEF:		/* extra space at start of line */
1107 				if ((s2->sflags & S_SECOND)
1108 				 || s2->aux)
1109 					break;
1110 				wlw += 8;
1111 				break;
1112 			case KEYSIG:
1113 /*			case TIMESIG:	*/
1114 				wlw += 4;
1115 				break;
1116 			}
1117 		}
1118 
1119 		/* leave room for guitar chord and annotations */
1120 		if (s->gch)
1121 			wlw = gchord_width(s, wlnote, wlw);
1122 
1123 		/* leave room for vocals under note */
1124 		/* related to draw_lyrics() */
1125 		if (s->ly)
1126 			wlw = ly_width(s, wlw);
1127 
1128 		/* if preceeded by a grace note sequence, adjust */
1129 		if (s2 && s2->type == GRACE)
1130 			s->wl = wlnote - 4.5;
1131 		else
1132 			s->wl = wlw;
1133 		break;
1134 	case SPACE:
1135 		xx = s->u.note.notes[0].shhd * 0.5;
1136 		s->wr = xx;
1137 		if (s->gch)
1138 			xx = gchord_width(s, xx, xx);
1139 		if (s->u.note.dc.n != 0)
1140 			xx += deco_width(s);
1141 		s->wl = xx;
1142 		break;
1143 	case BAR:
1144 		if (s->sflags & S_NOREPBRA)
1145 			break;
1146 		if (!(s->flags & ABC_F_INVIS)) {
1147 			int bar_type;
1148 
1149 			bar_type = s->u.bar.type;
1150 			switch (bar_type) {
1151 			case B_BAR:
1152 				w = 5 + 3;
1153 				break;
1154 			case (B_BAR << 4) + B_COL:
1155 			case (B_COL << 4) + B_BAR:
1156 				w = 5 + 3 + 3 + 5;
1157 				break;
1158 			case (B_COL << 4) + B_COL:
1159 				w = 5 + 5 + 3 + 3 + 3 + 5;
1160 				break;
1161 			default:
1162 				w = 5;
1163 				for (;;) {
1164 					switch (bar_type & 0x0f) {
1165 					case B_OBRA:
1166 					case B_CBRA:
1167 						w += 3;
1168 						break;
1169 					case B_COL:
1170 						w += 2;
1171 						break;
1172 					}
1173 					bar_type >>= 4;
1174 					if (bar_type == 0)
1175 						break;
1176 					w += 3;
1177 				}
1178 				break;
1179 			}
1180 			s->wl = w;
1181 			if (s->next
1182 			 && s->next->type != TIMESIG)
1183 				s->wr = 8;
1184 			else
1185 				s->wr = 5;
1186 //			s->shhd[0] = (w - 5) * -0.5;
1187 		}
1188 		if (s->u.bar.dc.n != 0)
1189 			s->wl += deco_width(s);
1190 
1191 		/* have room for the repeat numbers / guitar chord */
1192 		if (s->gch
1193 		 && strlen(s->text) < 4)
1194 			s->wl = gchord_width(s, s->wl, s->wl);
1195 		break;
1196 	case CLEF:
1197 		/* shift the clef to the left - see draw_symbols() */
1198 //		if (!(s->flags & ABC_F_INVIS)) {
1199 			s->wl = 12 + 10;
1200 			s->wr = (s->aux ? 10 : 12) - 10;
1201 //		}
1202 		break;
1203 	case KEYSIG: {
1204 		int n1, n2, esp;
1205 
1206 		s->wl = 3;
1207 		esp = 4;
1208 		if (s->u.key.nacc == 0) {
1209 			n1 = s->u.key.sf;	/* new key sig */
1210 			if (cfmt.cancelkey || n1 == 0)
1211 				n2 = s->aux;	/* old key */
1212 			else
1213 				n2 = 0;
1214 			if (n1 * n2 >= 0) {	/* if no natural */
1215 				if (n1 < 0)
1216 					n1 = -n1;
1217 				if (n2 < 0)
1218 					n2 = -n2;
1219 				if (n2 > n1)
1220 					n1 = n2;
1221 			} else {
1222 				n1 -= n2;
1223 				if (n1 < 0)
1224 					n1 = -n1;
1225 				esp += 3;	/* see extra space in draw_keysig() */
1226 			}
1227 		} else {
1228 			int last_acc;
1229 
1230 			n1 = n2 = s->u.key.nacc;
1231 			last_acc = s->u.key.accs[0];
1232 			for (i = 1; i < n2; i++) {
1233 				if (s->u.key.pits[i] > s->u.key.pits[i - 1] + 6
1234 				 || s->u.key.pits[i] < s->u.key.pits[i - 1] - 6)
1235 					n1--;		/* octave */
1236 				else if (s->u.key.accs[i] != last_acc)
1237 					esp += 3;
1238 				last_acc = s->u.key.accs[i];
1239 			}
1240 		}
1241 		s->wr = 5.5 * n1 + esp;
1242 		break;
1243 	    }
1244 	case TIMESIG:
1245 		/* !!tied to draw_timesig()!! */
1246 		w = 0;
1247 		for (i = 0; i < s->u.meter.nmeter; i++) {
1248 			int l;
1249 
1250 			l = sizeof s->u.meter.meter[i].top;
1251 			if (s->u.meter.meter[i].top[l - 1] == '\0') {
1252 				l = strlen(s->u.meter.meter[i].top);
1253 				if (s->u.meter.meter[i].top[1] == '|'
1254 				 || s->u.meter.meter[i].top[1] == '.')
1255 					l--;		/* 'C|' */
1256 			}
1257 			if (s->u.meter.meter[i].bot[0] != '\0') {
1258 				int l2;
1259 
1260 				l2 = sizeof s->u.meter.meter[i].bot;
1261 				if (s->u.meter.meter[i].bot[l2 - 1] == '\0')
1262 					l2 = strlen(s->u.meter.meter[i].bot);
1263 				if (l2 > l)
1264 					l = l2;
1265 			}
1266 			w += 6.5 * l;
1267 		}
1268 		s->wl = w;
1269 		s->wr = w + 7;
1270 		break;
1271 	case MREST:
1272 		s->wl = 40 / 2 + 16;
1273 		s->wr = 40 / 2 + 16;
1274 		break;
1275 	case GRACE:
1276 		s->wl = set_graceoffs(s);
1277 		if (s->ly)
1278 			ly_width(s, 0);
1279 		break;
1280 	case STBRK:
1281 		if (s->next && s->next->type == CLEF) {
1282 			s->wr = 2;
1283 			s->next->aux = 0;	/* big clef */
1284 		} else {
1285 			s->wr = 8;
1286 		}
1287 		s->wl = s->xmx;
1288 		break;
1289 #if 0
1290 	case TEMPO:
1291 	case PART:
1292 	case TUPLET:
1293 	case CUSTOS:
1294 #endif
1295 	case FMTCHG:		/* no space */
1296 		break;
1297 	default:
1298 		bug("Cannot set width for symbol", 1);
1299 	}
1300 }
1301 
1302 /* -- set the natural space -- */
set_space(struct SYMBOL * s)1303 static float set_space(struct SYMBOL *s)
1304 {
1305 	struct SYMBOL *s2;
1306 	int i, len, l, stemdir, prev_time;
1307 	float space;
1308 
1309 //fixme: s->ts_prev never NULL ?
1310 //	prev_time = !s->ts_prev ? s->time : s->ts_prev->time;
1311 	prev_time = s->ts_prev->time;
1312 	len = s->time - prev_time;		/* time skip */
1313 	if (len == 0) {
1314 		switch (s->type) {
1315 		case MREST:
1316 			return s->wl + 16;
1317 /*fixme:do same thing at start of line*/
1318 		case NOTEREST:
1319 			if (s->ts_prev->type == BAR) {
1320 				i = 2;
1321 				if (s->nflags < -2)
1322 					i = 0;
1323 				return space_tb[i];
1324 			}
1325 			break;
1326 		}
1327 		return 0;
1328 	}
1329 	if (s->ts_prev->type == MREST)
1330 		return s->ts_prev->wr + 16
1331 				+ 3;		// (bar wl=5 wr=8)
1332 	if (smallest_duration >= MINIM) {
1333 		if (smallest_duration >= SEMIBREVE)
1334 			len /= 4;
1335 		else
1336 			len /= 2;
1337 	}
1338 	if (len >= CROTCHET) {
1339 		if (len < MINIM)
1340 			i = 5;
1341 		else if (len < SEMIBREVE)
1342 			i = 6;
1343 		else if (len < BREVE)
1344 			i = 7;
1345 		else if (len < BREVE * 2)
1346 			i = 8;
1347 		else
1348 			i = 9;
1349 	} else {
1350 		if (len >= QUAVER)
1351 			i = 4;
1352 		else if (len >= SEMIQUAVER)
1353 			i = 3;
1354 		else if (len >= SEMIQUAVER / 2)
1355 			i = 2;
1356 		else if (len >= SEMIQUAVER / 4)
1357 			i = 1;
1358 		else
1359 			i = 0;
1360 	}
1361 	l = len - ((SEMIQUAVER / 8) << i);
1362 	space = space_tb[i];
1363 	if (l != 0) {
1364 		if (l < 0) {
1365 			space = space_tb[0] * len / (SEMIQUAVER / 8);
1366 		} else {
1367 			if (i >= 9)
1368 				i = 8;
1369 			space += (space_tb[i + 1] - space_tb[i])
1370 				* l / len;
1371 		}
1372 	}
1373 	if (s->dur == 0) {
1374 		if (s->type == BAR) {
1375 			if (s->u.bar.type & 0xf0)
1376 				space *= 0.8;	/* complex bar */
1377 			else
1378 				space *= 0.7;
1379 		}
1380 		return space;
1381 	}
1382 
1383 	/* reduce spacing within a beam */
1384 	if (!(s->sflags & S_BEAM_ST))
1385 		space *= fnnp;
1386 
1387 	/* decrease spacing when stem down followed by stem up */
1388 /*fixme:to be done later, after x computed in sym_glue*/
1389 	if (s->abc_type == ABC_T_NOTE && s->nflags >= -1
1390 	 && s->stem > 0) {
1391 		stemdir = 1;
1392 		for (s2 = s->ts_prev;
1393 		     s2 && s2->time == prev_time;
1394 		     s2 = s2->ts_prev) {
1395 			if (s2->nflags < -1 || s2->stem > 0) {
1396 				stemdir = 0;
1397 				break;
1398 			}
1399 		}
1400 		if (stemdir) {
1401 			for (s2 = s->ts_next;
1402 			     s2 && s2->time == s->time;
1403 			     s2 = s2->ts_next) {
1404 				if (s2->nflags < -1 || s2->stem < 0) {
1405 					stemdir = 0;
1406 					break;
1407 				}
1408 			}
1409 			if (stemdir)
1410 				space *= 0.9;
1411 		}
1412 	}
1413 	return space;
1414 }
1415 
1416 /* -- set the width and space of all symbols -- */
1417 /* this function is called once for the whole tune
1418  * then, once per music line up to the first sequence */
set_allsymwidth(struct SYMBOL * last_s)1419 static void set_allsymwidth(struct SYMBOL *last_s)
1420 {
1421 #if 1
1422 //	float space;
1423 	float new_val, maxx;
1424 	struct SYMBOL *s = tsfirst, *s2;
1425 	float xa = 0;
1426 	float xl[MAXSTAFF];
1427 
1428 	memset(xl, 0, sizeof xl);
1429 
1430 	/* loop on all symbols */
1431 	while (1) {
1432 		maxx = xa;
1433 		s2 = s;
1434 //		space = 0;
1435 		do {
1436 			set_width(s);
1437 			new_val = xl[s->staff] + s->wl;
1438 			if (new_val > maxx)
1439 				maxx = new_val;
1440 
1441 //			if (s->ts_prev) {
1442 //				new_val = set_space(s);
1443 //				if (space < new_val)
1444 //					space = new_val;
1445 //			}
1446 
1447 			s = s->ts_next;
1448 		} while (s != last_s && (s->sflags & S_SEQST) == 0);
1449 
1450 		/* set the spaces at start of sequence */
1451 		s2->shrink = maxx - xa;
1452 		if (s2->ts_prev)
1453 			s2->space = set_space(s2);
1454 
1455 		if (s2->shrink == 0 && s2->space == 0 && s2->type == CLEF) {
1456 			s2->sflags &= ~S_SEQST;		/* no space */
1457 			s2->time = s2->ts_prev->time;
1458 		}
1459 		if (s == last_s)
1460 			return;
1461 
1462 		// update the min left space per staff
1463 		xa = maxx;
1464 		s = s2;
1465 		do {
1466 			if (xl[s->staff] < xa + s->wr)
1467 				xl[s->staff] = xa + s->wr;
1468 			s = s->ts_next;
1469 		} while ((s->sflags & S_SEQST) == 0);
1470 	}
1471 	// not reached
1472 #else
1473 	struct VOICE_S *p_voice;
1474 	struct SYMBOL *s, *s2, *s3;
1475 	struct tblt_s *tblt;
1476 	int i;
1477 	float new_val, shrink, space;
1478 
1479 	/* set the space of the starting symbols */
1480 	new_val = 0;
1481 	s = tsfirst;
1482 	for (;;) {
1483 		set_width(s);
1484 		if (new_val < s->wl)
1485 			new_val = s->wl;
1486 		s = s->ts_next;
1487 		if (s == last_s || (s->sflags & S_SEQST))
1488 			break;
1489 	}
1490 	tsfirst->shrink = new_val;
1491 
1492 	/* loop on all remaining symbols */
1493 	while (s != last_s) {
1494 		s2 = s;
1495 		shrink = space = 0;
1496 		do {
1497 			int ymx1, ymn1, ymx2, ymn2;
1498 			float wl;
1499 
1500 			/* set the minimum space before and after the symbol */
1501 			set_width(s2);
1502 
1503 			/* calculate the minimum space before the symbol,
1504 			 * looping in the previous time sequence */
1505 			if (s2->type == BAR) {
1506 				ymx1 = 50;
1507 				ymn1 = -50;
1508 			} else {
1509 				ymx1 = s2->ymx;
1510 				ymn1 = s2->ymn;
1511 			}
1512 			wl = s2->wl;
1513 			new_val = 0;
1514 			for (s3 = s->ts_prev; s3; s3 = s3->ts_prev) {
1515 				if (new_val < s3->wr
1516 				 && s3->type == NOTEREST
1517 				 && s2->type == NOTEREST)
1518 					new_val = s3->wr;
1519 				if (s3->staff == s2->staff
1520 				 && (!(s3->flags & ABC_F_INVIS)
1521 				  || s3->voice == s2->voice)
1522 				 && new_val < s3->wr + wl) {
1523 					switch (s3->type) {
1524 					case NOTEREST:
1525 						if (s2->type == NOTEREST) {
1526 							new_val = s3->wr + wl;
1527 							break;
1528 						}
1529 						/* fall thru */
1530 					default:
1531 						ymx2 = s3->ymx;
1532 						ymn2 = s3->ymn;
1533 						if (ymn1 > ymx2
1534 						 || ymx1 < ymn2)
1535 							break;
1536 						/* fall thru */
1537 					case SPACE:
1538 					case BAR:
1539 					case CLEF:
1540 					case TIMESIG:
1541 					case KEYSIG:
1542 						new_val = s3->wr + wl;
1543 						break;
1544 					}
1545 				}
1546 				if (s3->sflags & S_SEQST) {
1547 					if (new_val != 0)
1548 						break;
1549 					wl -= s3->shrink;
1550 					if (wl < 0)
1551 						break;
1552 				}
1553 			}
1554 			if (shrink < new_val)
1555 				shrink = new_val;
1556 			new_val = set_space(s2);
1557 			if (space < new_val)
1558 				space = new_val;
1559 			if ((s2 = s2->ts_next) == last_s)
1560 				break;
1561 		} while (!(s2->sflags & S_SEQST));
1562 
1563 		/* set the spaces at start of sequence */
1564 		if (shrink == 0 && space == 0 && s->type == CLEF) {
1565 			s->sflags &= ~S_SEQST;		/* no space */
1566 			s->time = s->ts_prev->time;
1567 		} else {
1568 			s->shrink = shrink;
1569 			s->space = space;
1570 		}
1571 		s = s2;
1572 	}
1573 
1574 	/* have room for the tablature header */
1575 	space = 0;
1576 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
1577 		for (i = 0; i < 2; i++) {
1578 			if ((tblt = p_voice->tblts[i]) == NULL)
1579 				continue;
1580 			if (tblt->wh > space)
1581 				space = tblt->wh;
1582 		}
1583 	}
1584 	if (space == 0)
1585 		return;
1586 	shrink = 0;
1587 	for (s = tsfirst; s != last_s; s = s->ts_next) {
1588 		if (s->shrink != 0)
1589 			shrink += s->shrink;
1590 		if (s->abc_type == ABC_T_NOTE)
1591 			break;
1592 	}
1593 	if (s != last_s && shrink < space) {
1594 		while (!(s->sflags & S_SEQST))
1595 			s = s->ts_prev;
1596 		s->shrink += space - shrink;
1597 	}
1598 #endif
1599 }
1600 
1601 /* change a symbol into a rest */
to_rest(struct SYMBOL * s)1602 static void to_rest(struct SYMBOL *s)
1603 {
1604 	s->type = NOTEREST;
1605 	s->abc_type = ABC_T_REST;
1606 	s->sflags &= S_NL | S_SEQST;
1607 	s->doty = -1;
1608 	s->u.note.dc.n = 0;
1609 	s->gch = NULL;
1610 	s->extra = NULL;
1611 	s->u.note.slur_st = s->u.note.slur_end = 0;
1612 /*fixme: should set many parameters for set_width*/
1613 //	set_width(s);
1614 }
1615 
1616 /* -- set the repeat sequences / measures -- */
set_repeat(struct SYMBOL * g,struct SYMBOL * s)1617 static void set_repeat(struct SYMBOL *g,	/* repeat format */
1618 			struct SYMBOL *s)	/* first note */
1619 {
1620 	struct SYMBOL *s2, *s3;
1621 	int i, j, n, dur, staff, voice;
1622 
1623 	staff = s->staff;
1624 	voice = s->voice;
1625 
1626 	/* treat the sequence repeat */
1627 	if ((n = g->doty) < 0) {		/* number of notes / measures */
1628 		n = -n;
1629 		i = n;				/* number of notes to repeat */
1630 		for (s3 = s->prev; s3; s3 = s3->prev) {
1631 			if (s3->dur == 0) {
1632 				if (s3->type == BAR) {
1633 					error(0, s3, "Bar in sequence to repeat");
1634 					goto delrep;
1635 				}
1636 				continue;
1637 			}
1638 			if (--i <= 0)
1639 				break;
1640 		}
1641 		if (!s3) {
1642 			error(0, s, "Not enough symbols to repeat");
1643 			goto delrep;
1644 		}
1645 		dur = s->time - s3->time;
1646 
1647 		i = g->nohdi1 * n;	/* number of notes/rests to repeat */
1648 		for (s2 = s; s2; s2 = s2->next) {
1649 			if (s2->dur == 0) {
1650 				if (s2->type == BAR) {
1651 					error(0, s2, "Bar in repeat sequence");
1652 					goto delrep;
1653 				}
1654 				continue;
1655 			}
1656 			if (--i <= 0)
1657 				break;
1658 		}
1659 		if (!s2
1660 		 || !s2->next) {		/* should have some symbol */
1661 			error(0, s, "Not enough symbols after repeat sequence");
1662 			goto delrep;
1663 		}
1664 		for (s2 = s->prev; s2 != s3; s2 = s2->prev) {
1665 			if (s2->abc_type == ABC_T_NOTE) {
1666 				s2->sflags |= S_BEAM_END;
1667 				break;
1668 			}
1669 		}
1670 		for (j = g->nohdi1; --j >= 0; ) {
1671 			i = n;			/* number of notes/rests */
1672 			if (s->dur != 0)
1673 				i--;
1674 			s2 = s->ts_next;
1675 			while (i > 0) {
1676 				if (s2->staff == staff) {
1677 					s2->extra = NULL;
1678 					unlksym(s2);
1679 					if (s2->voice == voice
1680 					 && s2->dur)
1681 						i--;
1682 				}
1683 				s2 = s2->ts_next;
1684 			}
1685 			to_rest(s);
1686 			s->dur = s->u.note.notes[0].len = dur;
1687 //			s->sflags |= S_REPEAT | S_BEAM_ST;
1688 			s->sflags |= S_REPEAT;
1689 			set_width(s);
1690 			if (s->sflags & S_SEQST)
1691 				s->space = set_space(s);
1692 			s->head = H_SQUARE;
1693 			for (s = s2; s; s = s->ts_next) {
1694 				if (s->staff == staff
1695 				 && s->voice == voice
1696 				 && s->dur)
1697 					break;
1698 			}
1699 		}
1700 		goto delrep;			/* done */
1701 	}
1702 
1703 	/* check the measure repeat */
1704 	i = n;				/* number of measures to repeat */
1705 	for (s2 = s->prev->prev ; s2; s2 = s2->prev) {
1706 		if (s2->type == BAR
1707 		 || s2->time == tsfirst->time) {
1708 			if (--i <= 0)
1709 				break;
1710 		}
1711 	}
1712 	if (!s2) {
1713 		error(0, s, "Not enough measures to repeat");
1714 		goto delrep;
1715 	}
1716 
1717 	dur = s->time - s2->time;	/* repeat duration */
1718 
1719 	if (n == 1)
1720 		i = g->nohdi1;		/* repeat number */
1721 	else
1722 		i = n;			/* check only 2 measures */
1723 	for (s2 = s; s2; s2 = s2->next) {
1724 		if (s2->type == BAR) {
1725 			if (--i <= 0)
1726 				break;
1727 		}
1728 	}
1729 	if (!s2) {
1730 		error(0, s, "Not enough bars after repeat measure");
1731 		goto delrep;
1732 	}
1733 
1734 	/* if many 'repeat 2 measures'
1735 	 * insert a new %%repeat after the next bar */
1736 	i = g->nohdi1;		/* repeat number */
1737 	if (n == 2 && i > 1) {
1738 		s2 = s2->next;
1739 		if (!s2) {
1740 			error(0, s, "Not enough bars after repeat measure");
1741 			goto delrep;
1742 		}
1743 		g->nohdi1 = 1;
1744 		s = (struct SYMBOL *) getarena(sizeof *s);
1745 		memcpy(s, g, sizeof *s);
1746 		s->next = s2->extra;
1747 		if (s->next)
1748 			s->next->prev = s;
1749 		s->prev = NULL;
1750 		s2->extra = s;
1751 		s->nohdi1 = --i;
1752 	}
1753 
1754 	/* replace */
1755 	dur /= n;
1756 	if (n == 2) {			/* repeat 2 measures (once) */
1757 		s3 = s;
1758 		for (s2 = s->ts_next; ;s2 = s2->ts_next) {
1759 			if (s2->staff != staff)
1760 				continue;
1761 			if (s2->voice == voice
1762 			 && s2->type == BAR)
1763 				break;
1764 			s2->extra = NULL;
1765 			unlksym(s2);
1766 		}
1767 		to_rest(s3);
1768 		s3->dur = s3->u.note.notes[0].len = dur;
1769 		s3->flags = ABC_F_INVIS;
1770 		if (s3->sflags & S_SEQST)
1771 			s3->space = set_space(s3);
1772 		s2->u.bar.len = 2;
1773 		if (s2->sflags & S_SEQST)
1774 			s2->space = set_space(s2);
1775 		s3 = s2->next;
1776 		for (s2 = s3->ts_next; ;s2 = s2->ts_next) {
1777 			if (s2->staff != staff)
1778 				continue;
1779 			if (s2->voice == voice
1780 			 && s2->type == BAR)
1781 				break;
1782 			s2->extra = NULL;
1783 			unlksym(s2);
1784 		}
1785 		to_rest(s3);
1786 		s3->dur = s3->u.note.notes[0].len = dur;
1787 		s3->flags = ABC_F_INVIS;
1788 		set_width(s3);
1789 		if (s3->sflags & S_SEQST)
1790 			s3->space = set_space(s3);
1791 		if (s2->sflags & S_SEQST)
1792 			s2->space = set_space(s2);
1793 		return;
1794 	}
1795 
1796 	/* repeat 1 measure */
1797 	s3 = s;
1798 	for (j = g->nohdi1; --j >= 0; ) {
1799 		for (s2 = s3->ts_next; ; s2 = s2->ts_next) {
1800 			if (s2->staff != staff)
1801 				continue;
1802 			if (s2->voice == voice
1803 			 && s2->type == BAR)
1804 				break;
1805 			s2->extra = NULL;
1806 			unlksym(s2);
1807 		}
1808 		to_rest(s3);
1809 		s3->dur = s3->u.note.notes[0].len = dur;
1810 //		s3->sflags |= S_REPEAT | S_BEAM_ST;
1811 		s3->sflags |= S_REPEAT;
1812 		if (s3->sflags & S_SEQST)
1813 			s3->space = set_space(s3);
1814 		if (s2->sflags & S_SEQST)
1815 			s2->space = set_space(s2);
1816 		if (g->nohdi1 == 1) {
1817 			s3->doty = 1;
1818 			break;
1819 		}
1820 		s3->doty = g->nohdi1 - j + 1; /* number to print above the repeat rest */
1821 		s3 = s2->next;
1822 	}
1823 	return;
1824 
1825 delrep:					/* remove the %%repeat */
1826 	g->aux = -1;
1827 }
1828 
1829 /* add a custos before the symbol of the next line */
custos_add(struct SYMBOL * s)1830 static void custos_add(struct SYMBOL *s)
1831 {
1832 	struct VOICE_S *p_voice;
1833 	struct SYMBOL *new_s, *s2;
1834 	int i;
1835 
1836 	s2 = s;
1837 	for (;;) {
1838 		if (!s2)
1839 			return;
1840 		if (s2->abc_type == ABC_T_NOTE)
1841 			break;
1842 		s2 = s2->next;
1843 	}
1844 
1845 	p_voice = &voice_tb[s->voice];
1846 	p_voice->last_sym = s->prev;
1847 	if (!p_voice->last_sym)
1848 		p_voice->sym = NULL;
1849 	p_voice->time = s->time;
1850 	new_s = sym_add(p_voice, CUSTOS);
1851 	new_s->next = s;
1852 	s->prev = new_s;
1853 	new_s->ts_prev = s->ts_prev;
1854 	new_s->ts_prev->ts_next = new_s;
1855 	new_s->ts_next = s;
1856 	s->ts_prev = new_s;
1857 
1858 	new_s->sflags |= S_SEQST;
1859 	new_s->wl = 8;
1860 	new_s->wr = 4;
1861 	new_s->shrink = s->shrink;
1862 	if (new_s->shrink < 8 + 4)
1863 		new_s->shrink = 8 + 4;
1864 	new_s->space = s2->space;
1865 
1866 	new_s->nhd = s2->nhd;
1867 	for (i = 0; i <= new_s->nhd; i++) {
1868 //		new_s->u.note.notes[i].pit = s->u.note.notes[i].pit;
1869 		new_s->pits[i] = s2->pits[i];
1870 		new_s->u.note.notes[i].len = CROTCHET;
1871 	}
1872 	new_s->flags = ABC_F_STEMLESS;
1873 }
1874 
1875 /* -- define the beginning of a new music line -- */
set_nl(struct SYMBOL * s)1876 static struct SYMBOL *set_nl(struct SYMBOL *s)
1877 {
1878 	struct SYMBOL *s2, *extra, *new_sy;
1879 	struct VOICE_S *p_voice;
1880 	int done;
1881 
1882 	/* if explicit EOLN, cut on the next symbol */
1883 	if ((s->sflags & S_EOLN) && !cfmt.keywarn && !cfmt.timewarn) {
1884 		s = s->next;
1885 		if (!s)
1886 			return s;
1887 
1888 		while (!(s->sflags & S_SEQST))
1889 			s = s->ts_prev;
1890 		goto setnl;
1891 	}
1892 
1893 	/* if normal symbol, cut here */
1894 	switch (s->type) {
1895 	case CLEF:
1896 	case BAR:
1897 		break;
1898 	case KEYSIG:
1899 		if (cfmt.keywarn && !s->u.key.empty)
1900 			break;
1901 		goto normal;
1902 	case TIMESIG:
1903 		if (cfmt.timewarn)
1904 			break;
1905 		goto normal;
1906 	case GRACE:			/* don't cut on a grace note */
1907 		s = s->next;
1908 		if (!s)
1909 			return s;
1910 		/* fall thru */
1911 	default:
1912 normal:
1913 		/* cut on the next symbol */
1914 		s = s->next;
1915 		if (!s)
1916 			return s;
1917 		while (!(s->sflags & S_SEQST))
1918 			s = s->ts_prev;
1919 		goto setnl;
1920 	}
1921 
1922 	/* go back to handle the staff breaks at end of line */
1923 	for (; s; s = s->ts_prev) {
1924 		if (!(s->sflags & S_SEQST))
1925 			continue;
1926 		switch (s->type) {
1927 		case CLEF:
1928 		case KEYSIG:
1929 		case TIMESIG:
1930 			continue;
1931 		}
1932 		break;
1933 	}
1934 	done = 0;
1935 	new_sy = extra = NULL;
1936 	for ( ; ; s = s->ts_next) {
1937 		if (!s)
1938 			return s;
1939 		if (s->sflags & S_NEW_SY)
1940 			new_sy = s;
1941 		if (!(s->sflags & S_SEQST))
1942 			continue;
1943 		if (done < 0)
1944 			break;
1945 		switch (s->type) {
1946 		case BAR:
1947 			if (done)
1948 				goto cut_here;
1949 			done = 1;
1950 			break;
1951 		case STBRK:
1952 			if (s->doty == 0) {	/* if not forced */
1953 				unlksym(s);	/* remove */
1954 				break;
1955 			}
1956 			done = -1;	/* keep the next symbols on the next line */
1957 			break;
1958 		case TIMESIG:
1959 			if (!cfmt.timewarn)
1960 				goto cut_here;
1961 			break;
1962 		case CLEF:
1963 			if (done)
1964 				goto cut_here;
1965 			break;
1966 		case KEYSIG:
1967 			if (!cfmt.keywarn || s->u.key.empty)
1968 				goto cut_here;
1969 			break;
1970 		default:
1971 			if (!done || (s->prev && s->prev->type == GRACE))
1972 				break;
1973 			goto cut_here;
1974 		}
1975 		if (s->extra) {
1976 			if (!extra)
1977 				extra = s;
1978 			else
1979 				error(0, s, "abcm2ps problem: "
1980 					"Extra symbol may be misplaced");
1981 		}
1982 	}
1983 cut_here:
1984 	if (extra			/* extra symbol(s) to be moved */
1985 	 && extra != s) {
1986 		s2 = extra->extra;
1987 		while (s2->next)
1988 			s2 = s2->next;
1989 		s2->next = s->extra;
1990 		s->extra = extra->extra;
1991 		extra->extra = NULL;
1992 	}
1993 	if (new_sy && s != new_sy) {
1994 		new_sy->sflags &= ~S_NEW_SY;
1995 		s->sflags |= S_NEW_SY;
1996 	}
1997 setnl:
1998 	if (cfmt.custos && !first_voice->next) {
1999 		custos_add(s);
2000 	} else {
2001 		s2 = s->ts_prev;
2002 		switch (s2->type) {
2003 		case BAR:
2004 		case FMTCHG:
2005 		case CLEF:
2006 		case KEYSIG:
2007 		case TIMESIG:
2008 			break;
2009 		default:			/* add an extra symbol at eol */
2010 			p_voice = &voice_tb[s2->voice];
2011 			p_voice->last_sym = s2;
2012 			p_voice->time = s->time;
2013 			s2 = s2->next;
2014 			extra = sym_add(p_voice, FMTCHG);
2015 			extra->next = s2;
2016 			if (s2)
2017 				s2->prev = extra;
2018 			extra->ts_prev = extra->prev;
2019 			extra->ts_prev->ts_next = extra;
2020 			extra->ts_next = s;
2021 			s->ts_prev = extra;
2022 			extra->aux = -1;
2023 			extra->sflags |= S_SEQST;
2024 			extra->wl = 6;
2025 //fixme: wr/shrink/space are not needed
2026 			extra->wr = 6;
2027 //			extra->shrink = extra->prev->wr + 6;
2028 //			extra->space = extra->prev->space;
2029 			extra->shrink = s->shrink;
2030 			extra->space = s->space;
2031 			if (s->x != 0)
2032 				extra->x = s->x - 1;
2033 // {	/* auto break */
2034 //				for (s2 = s->ts_next; ; s2 = s2->ts_next) {
2035 //					if (s2->x != 0) {
2036 //						extra->x = s2->x - 1;
2037 //						break;
2038 //					}
2039 //				}
2040 //			}
2041 			break;
2042 		}
2043 	}
2044 	s->sflags |= S_NL;
2045 	return s;
2046 }
2047 
2048 /* -- search where to cut the lines according to the staff width -- */
set_lines(struct SYMBOL * first,struct SYMBOL * last,float lwidth,float indent)2049 static struct SYMBOL *set_lines(struct SYMBOL *first,	/* first symbol */
2050 				struct SYMBOL *last,	/* last symbol / 0 */
2051 				float lwidth,		/* w - (clef & key sig) */
2052 				float indent)		/* for start of tune */
2053 {
2054 	struct SYMBOL *s, *s2, *s3;
2055 	float x, xmin, xmax, wwidth, shrink, space;
2056 	int nlines, beam, bar_time;
2057 
2058 	/* calculate the whole size of the piece of tune */
2059 	wwidth = indent;
2060 	for (s = first; s != last; s = s->ts_next) {
2061 		if (!(s->sflags & S_SEQST))
2062 			continue;
2063 		s->x = wwidth;
2064 		shrink = s->shrink;
2065 		if ((space = s->space) < shrink)
2066 			wwidth += shrink;
2067 		else
2068 			wwidth += shrink * cfmt.maxshrink
2069 				+ space * (1 - cfmt.maxshrink);
2070 	}
2071 
2072 	/* loop on cutting the tune into music lines */
2073 	s = first;
2074 	for (;;) {
2075 		nlines = wwidth / lwidth + 0.999;
2076 		if (nlines <= 1) {
2077 			if (last)
2078 				last = set_nl(last);
2079 			return last;
2080 		}
2081 
2082 		/* try to cut on a measure bar */
2083 		s2 = first = s;
2084 		xmin = s->x + wwidth / nlines * cfmt.breaklimit;
2085 		xmax = s->x + lwidth;
2086 		for ( ; s != last; s = s->ts_next) {
2087 			x = s->x;
2088 			if (x == 0)
2089 				continue;
2090 			if (x > xmax)
2091 				break;
2092 			if (s->type != BAR)
2093 				continue;
2094 			if (x > xmin)
2095 				goto cut_here;
2096 			s2 = s;			// keep the last bar
2097 		}
2098 
2099 		/* if a bar, cut here */
2100 		if (s == last)
2101 			return last;
2102 		if (s->type == BAR)
2103 			goto cut_here;
2104 
2105 		bar_time = s2->time;
2106 
2107 		/* try to avoid to cut a beam */
2108 		beam = s2->type == NOTEREST &&
2109 				(s2->sflags & (S_BEAM_ST | S_BEAM_END)) == 0 ? 1 : 0;
2110 		s = s2;				/* restart from start or last bar */
2111 		s2 = s3 = NULL;
2112 		xmax -= 6;			// a FORMAT will be added
2113 		for ( ; s != last; s = s->ts_next) {
2114 			if ((s->sflags & (S_BEAM_ST | S_BEAM_END))
2115 						== S_BEAM_ST) {
2116 				beam++;
2117 				continue;
2118 			}
2119 			if ((s->sflags & (S_BEAM_ST | S_BEAM_END))
2120 						== S_BEAM_END)
2121 				beam--;
2122 			x = s->x;
2123 			if (x < xmin)
2124 				continue;
2125 //--fixme
2126 //			if (x + 2 * s->shrink >= xmax)
2127 			if (x + s->shrink >= xmax)
2128 				break;
2129 			if (beam != 0)
2130 				continue;
2131 			s2 = s;
2132 			if ((s->time - bar_time) % (CROTCHET / 2) == 0)
2133 				s3 = s;
2134 		}
2135 		if (s3)
2136 			s2 = s3;
2137 		if (s2)
2138 			s = s2;
2139 		while (s->x == 0 || s->x + s->shrink * 2 >= xmax)
2140 			s = s->ts_prev;
2141 cut_here:
2142 		if (s->sflags & S_NL) {		/* already set here - advance */
2143 			error(0, s, "Line split problem - "
2144 					"adjust maxshrink and/or breaklimit");
2145 			nlines = 2;
2146 			for (s = s->ts_next; s != last; s = s->ts_next) {
2147 				if (s->x == 0)
2148 					continue;
2149 				if (--nlines <= 0)
2150 					break;
2151 			}
2152 		}
2153 		s = set_nl(s);
2154 		if (!s
2155 		 || (last && s->time >= last->time))
2156 			break;
2157 		wwidth -= s->x - first->x;
2158 	}
2159 	return s;
2160 }
2161 
2162 /* -- cut the tune into music lines -- */
cut_tune(float lwidth,float indent)2163 static void cut_tune(float lwidth, float indent)
2164 {
2165 	struct VOICE_S *p_voice;
2166 	struct SYMBOL *s, *s2;
2167 	int i;
2168 	float xmin;
2169 
2170 	/* adjust the line width according to the starting clef
2171 	 * and key signature */
2172 /*fixme: may change in the tune*/
2173 #if 1
2174 	s = tsfirst;
2175 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
2176 		i = p_voice - voice_tb;
2177 		if (cursys->voice[i].range >= 0)
2178 			break;
2179 	}
2180 	lwidth -= 12 + 10			// clef.wl
2181 		+ 12 - 10			// clef.wr
2182 		+ 3				// key.wl
2183 		+ 3 + p_voice->key.sf * 5.5;	// key.wr
2184 #else
2185 	for (s = tsfirst; s; s = s->ts_next) {
2186 		if (s->shrink == 0)
2187 			continue;
2188 		if (s->type != CLEF && s->type != KEYSIG)
2189 			break;
2190 		lwidth -= s->shrink;
2191 	}
2192 #endif
2193 	if (cfmt.custos && !first_voice->next)
2194 		lwidth -= 12;
2195 	if (cfmt.continueall) {
2196 		set_lines(s, NULL, lwidth, indent);
2197 		return;
2198 	}
2199 
2200 	/* if asked, count the measures and set the EOLNs */
2201 	if ((i = cfmt.barsperstaff) != 0) {
2202 		s2 = s;
2203 		for ( ; s; s = s->ts_next) {
2204 			if (s->type != BAR
2205 			 || s->aux == 0)
2206 				continue;
2207 			if (--i > 0)
2208 				continue;
2209 			s->sflags |= S_EOLN;
2210 			i = cfmt.barsperstaff;
2211 		}
2212 		s = s2;
2213 	}
2214 
2215 	/* cut at explicit end of line, checking the line width */
2216 	xmin = indent;
2217 	s2 = s;
2218 	for ( ; s; s = s->ts_next) {
2219 		if (!(s->sflags & (S_SEQST | S_EOLN)))
2220 			continue;
2221 		xmin += s->shrink;
2222 		if (xmin > lwidth) {
2223 			if (cfmt.linewarn)
2224 				error(0, s, "Line overfull (%.0fpt of %.0fpt)",
2225 					xmin, lwidth);
2226 //			for (s = s->ts_next; s; s = s->ts_next) {
2227 			for ( ; s; s = s->ts_next) {
2228 				if (s->sflags & S_EOLN)
2229 					break;
2230 			}
2231 			s = s2 = set_lines(s2, s, lwidth, indent);
2232 			if (!s)
2233 				break;
2234 			xmin = s->shrink;
2235 			indent = 0;
2236 			continue;
2237 		}
2238 		if (!(s->sflags & S_EOLN))
2239 			continue;
2240 		s2 = set_nl(s);
2241 		s->sflags &= ~S_EOLN;
2242 		s = s2;
2243 		if (!s)
2244 			break;
2245 		xmin = s->shrink;
2246 		indent = 0;
2247 	}
2248 }
2249 
2250 /* -- set the y values of some symbols -- */
set_yval(struct SYMBOL * s)2251 static void set_yval(struct SYMBOL *s)
2252 {
2253 //fixme: staff_tb is not yet loaded
2254 //	int top, bot;
2255 //	top = staff_tb[s->staff].topbar;
2256 //	bot = staff_tb[s->staff].botbar;
2257 	switch (s->type) {
2258 	case CLEF:
2259 		if ((s->sflags & S_SECOND)
2260 		 || (s->flags & ABC_F_INVIS)) {
2261 //			s->ymx = s->ymn = (top + bot) / 2;
2262 			s->ymx = s->ymn = 12;
2263 			break;
2264 		}
2265 		s->y = (s->u.clef.line - 1) * 6;
2266 		switch (s->u.clef.type) {
2267 		default:			/* treble / perc */
2268 //			s->y = -2 * 6;
2269 //			s->ymx = 24 + 12;
2270 //			s->ymn = -9;
2271 			s->ymx = s->y + 28;
2272 			s->ymn = s->y - 14;
2273 			break;
2274 		case ALTO:
2275 //			s->y = -3 * 6;
2276 //			s->ymx = 24 + 2;
2277 //			s->ymn = -1;
2278 			s->ymx = s->y + 13;
2279 			s->ymn = s->y - 11;
2280 			break;
2281 		case BASS:
2282 //			s->y = -4 * 6;
2283 //			s->ymx = 24 + 2;
2284 //			s->ymn = 2;
2285 			s->ymx = s->y + 7;
2286 			s->ymn = s->y - 12;
2287 			break;
2288 		}
2289 		if (s->aux) {			// small clef
2290 			s->ymx -= 2;
2291 			s->ymn += 2;
2292 		}
2293 		if (s->ymx < 26)
2294 			s->ymx = 26;
2295 		if (s->ymn > -1)
2296 			s->ymn = -1;
2297 //		s->y += s->u.clef.line * 6;
2298 //		if (s->y > 0)
2299 //			s->ymx += s->y;
2300 //		else if (s->y < 0)
2301 //			s->ymn += s->y;
2302 		if (s->u.clef.octave > 0)
2303 			s->ymx += 9;
2304 		else if (s->u.clef.octave < 0)
2305 			s->ymn -= 9;
2306 		break;
2307 	case KEYSIG:
2308 		if (s->u.key.sf > 2)
2309 			s->ymx = 24 + 10;
2310 		else if (s->u.key.sf > 0)
2311 			s->ymx = 24 + 6;
2312 		else
2313 			s->ymx = 24 + 2;
2314 		s->ymn = -2;
2315 		break;
2316 	default:
2317 //		s->ymx = top + 2;
2318 		s->ymx = 24 + 2;
2319 		s->ymn = -2;
2320 		break;
2321 	}
2322 }
2323 
2324 // set the clefs (treble or bass) in a 'auto clef' sequence
2325 // return the starting clef type
set_auto_clef(int staff,struct SYMBOL * s_start,int clef_type_start)2326 static int set_auto_clef(int staff,
2327 			struct SYMBOL *s_start,
2328 			int clef_type_start)
2329 {
2330 	struct SYMBOL *s;
2331 	struct SYMBOL *s_last, *s_last_chg;
2332 	int clef_type, min, max, time;
2333 
2334 	/* get the max and min pitches in the sequence */
2335 	max = 12;				/* "F," */
2336 	min = 20;				/* "G" */
2337 	for (s = s_start; s; s = s->ts_next) {
2338 		if ((s->sflags & S_NEW_SY) && s != s_start)
2339 			break;
2340 		if (s->staff != staff)
2341 			continue;
2342 		if (s->abc_type != ABC_T_NOTE) {
2343 			if (s->type == CLEF) {
2344 				if (s->u.clef.type != AUTOCLEF)
2345 					break;
2346 				unlksym(s);
2347 			}
2348 			continue;
2349 		}
2350 		if (s->pits[0] < min)
2351 			min = s->pits[0];
2352 		else if (s->pits[s->nhd] > max)
2353 			max = s->pits[s->nhd];
2354 	}
2355 
2356 	if (min >= 19					/* upper than 'F' */
2357 	 || (min >= 13 && clef_type_start != BASS))	/* or 'G,' */
2358 		return TREBLE;
2359 	if (max <= 13					/* lower than 'G,' */
2360 	 || (max <= 19 && clef_type_start != TREBLE))	/* or 'F' */
2361 		return BASS;
2362 
2363 	/* set clef changes */
2364 	if (clef_type_start == AUTOCLEF) {
2365 		if ((max + min) / 2 >= 16)
2366 			clef_type_start = TREBLE;
2367 		else
2368 			clef_type_start = BASS;
2369 	}
2370 	clef_type = clef_type_start;
2371 	s_last = s;
2372 	s_last_chg = NULL;
2373 	for (s = s_start; s != s_last; s = s->ts_next) {
2374 		struct SYMBOL *s2, *s3;	//, *s4;
2375 
2376 		if ((s->sflags & S_NEW_SY) && s != s_start)
2377 			break;
2378 		if (s->staff != staff || s->abc_type != ABC_T_NOTE)
2379 			continue;
2380 
2381 		/* check if a clef change may occur */
2382 		time = s->time;
2383 		if (clef_type == TREBLE) {
2384 			if (s->pits[0] > 12		/* F, */
2385 			 || s->pits[s->nhd] > 20) {	/* G */
2386 				if (s->pits[0] > 20)
2387 					s_last_chg = s;
2388 				continue;
2389 			}
2390 			s2 = s->ts_prev;
2391 			if (s2
2392 			 && s2->time == time
2393 			 && s2->staff == staff
2394 			 && s2->abc_type == ABC_T_NOTE
2395 			 && s2->pits[0] >= 19)	/* F */
2396 				continue;
2397 			s2 = s->ts_next;
2398 			if (s2
2399 			 && s2->staff == staff
2400 			 && s2->time == time
2401 			 && s2->abc_type == ABC_T_NOTE
2402 			 && s2->pits[0] >= 19)	/* F */
2403 				continue;
2404 		} else {
2405 			if (s->pits[0] < 12		/* F, */
2406 			 || s->pits[s->nhd] < 20) {	/* G */
2407 				if (s->pits[s->nhd] < 12)
2408 					s_last_chg = s;
2409 				continue;
2410 			}
2411 			s2 = s->ts_prev;
2412 			if (s2
2413 			 && s2->time == time
2414 			 && s2->staff == staff
2415 			 && s2->abc_type == ABC_T_NOTE
2416 			 && s2->pits[0] <= 13)	/* G, */
2417 				continue;
2418 			s2 = s->ts_next;
2419 			if (s2
2420 			 && s2->staff == staff
2421 			 && s2->time == time
2422 			 && s2->abc_type == ABC_T_NOTE
2423 			 && s2->pits[0] <= 13)	/* G, */
2424 				continue;
2425 		}
2426 
2427 		/* if first change, change the starting clef */
2428 		if (!s_last_chg) {
2429 			clef_type = clef_type_start =
2430 					clef_type == TREBLE ? BASS : TREBLE;
2431 			s_last_chg = s;
2432 			continue;
2433 		}
2434 
2435 		/* go backwards and search where to insert a clef change */
2436 		s3 = s;
2437 		for (s2 = s->ts_prev; s2 != s_last_chg; s2 = s2->ts_prev) {
2438 			if (s2->staff != staff)
2439 				continue;
2440 			if (s2->type == BAR
2441 			 && s2->voice == s->voice) {
2442 				s3 = s2;
2443 				break;
2444 			}
2445 			if (s2->abc_type != ABC_T_NOTE)
2446 				continue;
2447 
2448 #if 0
2449 			/* exit loop if a clef change cannot occur */
2450 			if (clef_type == TREBLE) {
2451 				if (s2->pits[0] >= 19)		/* F */
2452 					break;
2453 			} else {
2454 				if (s2->pits[s2->nhd] <= 13)	/* G, */
2455 					break;
2456 			}
2457 #endif
2458 
2459 			/* have a 2nd choice on beam start */
2460 			if ((s2->sflags & S_BEAM_ST)
2461 			 && !voice_tb[s2->voice].second)
2462 				s3 = s2;
2463 		}
2464 
2465 		/* no change possible if no insert point */
2466 		if (s3->time == s_last_chg->time) {
2467 			s_last_chg = s;
2468 			continue;
2469 		}
2470 		s_last_chg = s;
2471 
2472 		/* insert a clef change */
2473 		clef_type = clef_type == TREBLE ? BASS : TREBLE;
2474 		s2 = insert_clef(s3, clef_type, clef_type == TREBLE ? 2 : 4);
2475 		s2->sflags |= S_CLEF_AUTO;
2476 //		s3->prev->staff = staff;
2477 	}
2478 	return clef_type_start;
2479 }
2480 
2481 /* set the auto clefs */
2482 /* this function is called once at start of tune generation */
2483 /*
2484  * global variables:
2485  *	- staff_tb[staff].clef = clefs at start of line (here, start of tune)
2486  *				(created here, updated on clef draw)
2487  *	- voice_tb[voice].clef = clefs at end of generation
2488  *				(created on voice creation, updated here)
2489  */
set_clefs(void)2490 static void set_clefs(void)
2491 {
2492 	struct SYSTEM *sy;
2493 	struct VOICE_S *p_voice;
2494 	struct SYMBOL *s, *s2, *g;
2495 	int staff, voice, pitch, new_type, new_line, old_lvl;
2496 	struct {
2497 		struct SYMBOL *clef;
2498 		short autoclef;
2499 		short mid;
2500 	} staff_clef[MAXSTAFF];
2501 
2502 	old_lvl = lvlarena(1);			// keep the staff clefs
2503 
2504 	// create the staff table
2505 	memset(staff_tb, 0, sizeof staff_tb);
2506 	for (staff = 0; staff <= nstaff; staff++) {
2507 		staff_clef[staff].clef = NULL;
2508 		staff_clef[staff].autoclef = 1;
2509 	}
2510 
2511 	// set the starting clefs of the staves
2512 	sy = cursys;
2513 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
2514 		voice = p_voice - voice_tb;
2515 		if (sy->voice[voice].range < 0)
2516 			continue;
2517 		staff = sy->voice[voice].staff;
2518 		if (!sy->voice[voice].second) {		// main voices
2519 			if (p_voice->stafflines)
2520 				sy->staff[staff].stafflines = p_voice->stafflines;
2521 			if (p_voice->staffscale != 0)
2522 				sy->staff[staff].staffscale = p_voice->staffscale;
2523 			if (sy->voice[voice].sep)
2524 				sy->staff[staff].sep = sy->voice[voice].sep;
2525 			if (sy->voice[voice].maxsep)
2526 				sy->staff[staff].maxsep = sy->voice[voice].maxsep;
2527 		}
2528 		s = p_voice->s_clef;
2529 		if (!sy->voice[voice].second
2530 		 && !(s->sflags & S_CLEF_AUTO))
2531 			staff_clef[staff].autoclef = 0;
2532 	}
2533 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
2534 		voice = p_voice - voice_tb;
2535 		if (sy->voice[voice].range < 0
2536 		 || sy->voice[voice].second)		// main voices
2537 			continue;
2538 		staff = sy->voice[voice].staff;
2539 		s = p_voice->s_clef;
2540 		if (staff_clef[staff].autoclef) {
2541 			s->u.clef.type = set_auto_clef(staff,
2542 							tsfirst,
2543 							s->u.clef.type);
2544 			s->u.clef.line =
2545 				s->u.clef.type == TREBLE ? 2 : 4;
2546 		}
2547 		staff_clef[staff].clef = staff_tb[staff].s_clef = s;
2548 	}
2549 	for (staff = 0; staff <= sy->nstaff; staff++)
2550 		staff_clef[staff].mid = (strlen(sy->staff[staff].stafflines) - 1) * 3;
2551 
2552 	for (s = tsfirst; s; s = s->ts_next) {
2553 		for (g = s->extra ; g; g = g->next) {
2554 			if (g->type == FMTCHG && g->aux == REPEAT) {
2555 				set_repeat(g, s);
2556 				break;
2557 			}
2558 		}
2559 
2560 		// handle %%staves
2561 		if (s->sflags & S_NEW_SY) {
2562 			sy = sy->next;
2563 			for (staff = 0; staff <= nstaff; staff++)
2564 				staff_clef[staff].autoclef = 1;
2565 			for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
2566 				voice = p_voice - voice_tb;
2567 				if (sy->voice[voice].range < 0)
2568 					continue;
2569 				staff = sy->voice[voice].staff;
2570 				if (!sy->voice[voice].second) {
2571 					if (p_voice->stafflines)
2572 						sy->staff[staff].stafflines =
2573 								p_voice->stafflines;
2574 					if (p_voice->staffscale != 0)
2575 						sy->staff[staff].staffscale =
2576 								p_voice->staffscale;
2577 					if (sy->voice[voice].sep)
2578 						sy->staff[staff].sep =
2579 								sy->voice[voice].sep;
2580 					if (sy->voice[voice].maxsep)
2581 						sy->staff[staff].maxsep =
2582 								sy->voice[voice].maxsep;
2583 				}
2584 				s2 = p_voice->s_clef;
2585 				if (!(s2->sflags & S_CLEF_AUTO))
2586 					staff_clef[staff].autoclef = 0;
2587 			}
2588 			for (staff = 0; staff <= sy->nstaff; staff++)
2589 				staff_clef[staff].mid =
2590 					(strlen(sy->staff[staff].stafflines) - 1) * 3;
2591 			for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
2592 				voice = p_voice - voice_tb;
2593 				if (sy->voice[voice].range < 0
2594 				 || sy->voice[voice].second)
2595 					continue;
2596 				staff = sy->voice[voice].staff;
2597 				s2 = p_voice->s_clef;
2598 				if (s2->sflags & S_CLEF_AUTO) {
2599 //fixme: the staff may have other voices with explicit clefs...
2600 //					if (!staff_clef[staff].autoclef)
2601 //						???
2602 					new_type = set_auto_clef(staff, s,
2603 						staff_clef[staff].clef ?
2604 							staff_clef[staff].clef->u.clef.type :
2605 							AUTOCLEF);
2606 					new_line = new_type == TREBLE ? 2 : 4;
2607 				} else {
2608 					new_type = s2->u.clef.type;
2609 					new_line = s2->u.clef.line;
2610 				}
2611 				if (!staff_clef[staff].clef) {	// new staff
2612 					if (s2->sflags & S_CLEF_AUTO) {
2613 						if (s2->u.clef.type != AUTOCLEF) {
2614 							p_voice->s_clef =
2615 								(struct SYMBOL *) getarena(sizeof *s);
2616 							memcpy(p_voice->s_clef,
2617 								s2,
2618 								sizeof *p_voice->s_clef);
2619 						}
2620 						p_voice->s_clef->u.clef.type = new_type;
2621 						p_voice->s_clef->u.clef.line = new_line;
2622 					}
2623 					staff_tb[staff].s_clef =
2624 						staff_clef[staff].clef = p_voice->s_clef;
2625 					continue;
2626 				}
2627 								// old staff
2628 				if (new_type == staff_clef[staff].clef->u.clef.type
2629 				 && new_line == staff_clef[staff].clef->u.clef.line)
2630 					continue;
2631 				g = s;
2632 				while (g->voice != voice)
2633 					g = g->ts_next;
2634 				if (g->type != CLEF) {
2635 					g = insert_clef(g, new_type, new_line);
2636 					if (s2->sflags & S_CLEF_AUTO)
2637 						g->sflags |= S_CLEF_AUTO;
2638 				}
2639 				staff_clef[staff].clef = p_voice->s_clef = g;
2640 			}
2641 		}
2642 		if (s->type != CLEF) {
2643 			s->mid = staff_clef[s->staff].mid;
2644 			continue;
2645 		}
2646 
2647 		if (s->u.clef.type == AUTOCLEF) {
2648 			s->u.clef.type = set_auto_clef(s->staff,
2649 						s->ts_next,
2650 						staff_clef[s->staff].clef->u.clef.type);
2651 			s->u.clef.line = s->u.clef.type == TREBLE ? 2 : 4;
2652 		}
2653 
2654 		p_voice = &voice_tb[s->voice];
2655 		p_voice->s_clef = s;
2656 		if (s->sflags & S_SECOND) {
2657 /*fixme:%%staves:can this happen?*/
2658 //			if (!s->prev)
2659 //				break;
2660 			unlksym(s);
2661 			continue;
2662 		}
2663 		staff = s->staff;
2664 // may have been inserted on %%staves
2665 //		if (s->sflags & S_CLEF_AUTO) {
2666 //			unlksym(s);
2667 //			continue;
2668 //		}
2669 
2670 		if (staff_tb[staff].s_clef) {
2671 			if (s->u.clef.type == staff_clef[staff].clef->u.clef.type
2672 			 && s->u.clef.line == staff_clef[staff].clef->u.clef.line
2673 			 && !(s->sflags & S_NEW_SY)) {
2674 //				unlksym(s);
2675 				continue;
2676 			}
2677 		} else {
2678 
2679 			// the voice moved to a new staff with a forced clef
2680 			 staff_tb[staff].s_clef = s;
2681 		}
2682 		staff_clef[staff].clef = s;
2683 	}
2684 
2685 	/* set a pitch to the symbols of voices with no note */
2686 	sy = cursys;
2687 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
2688 		voice = p_voice - voice_tb;
2689 		if (sy->voice[voice].range < 0)
2690 			continue;
2691 		s2 = p_voice->sym;
2692 		if (!s2 || s2->pits[0] != 127)
2693 			continue;
2694 		staff = sy->voice[voice].staff;
2695 		switch (staff_tb[staff].s_clef->u.clef.type) {
2696 		default:
2697 			pitch = 22;		/* 'B' */
2698 			break;
2699 		case ALTO:
2700 			pitch = 16;		/* 'C' */
2701 			break;
2702 		case BASS:
2703 			pitch = 10;		/* 'D,' */
2704 			break;
2705 		}
2706 		for (s = s2; s; s = s->next)
2707 			s->pits[0] = pitch;
2708 	}
2709 
2710 	lvlarena(old_lvl);
2711 }
2712 
2713 /* -- set the pitch of the notes according to the clefs -- */
2714 /* also treat the auto clefs and set the vertical offset of the symbols */
2715 /* this function is called only once per tune
2716  * then, once per music line up to the old sequence */
set_pitch(struct SYMBOL * last_s)2717 static void set_pitch(struct SYMBOL *last_s)
2718 {
2719 	struct SYMBOL *s, *g;
2720 	int staff, delta, dur;
2721 	signed char staff_delta[MAXSTAFF];
2722 	static const signed char delta_tb[4] = {
2723 		0 - 2 * 2,
2724 		6 - 3 * 2,
2725 		12 - 4 * 2,
2726 		0 - 3 * 2
2727 	};
2728 
2729 	for (staff = 0; staff <= nstaff; staff++) {
2730 		s = staff_tb[staff].s_clef;
2731 		staff_delta[staff] = delta_tb[s->u.clef.type] +
2732 				s->u.clef.line * 2 +
2733 				s->u.clef.transpose;
2734 	}
2735 
2736 	dur = BASE_LEN;
2737 	for (s = tsfirst; s != last_s; s = s->ts_next) {
2738 		int np, m;
2739 
2740 		staff = s->staff;
2741 		switch (s->type) {
2742 		case CLEF:
2743 			staff_delta[staff] = delta_tb[s->u.clef.type] +
2744 						s->u.clef.line * 2 +
2745 						s->u.clef.transpose;
2746 			set_yval(s);
2747 			break;
2748 		case GRACE:
2749 			for (g = s->extra; g; g = g->next) {
2750 				if (g->type != NOTEREST)
2751 					continue;
2752 				delta = staff_delta[g->staff];
2753 				if (delta != 0
2754 				 && voice_tb[s->voice].key.instr != K_DRUM) {
2755 					for (m = g->nhd; m >= 0; m--)
2756 						g->pits[m] += delta;
2757 				}
2758 				g->ymn = 3 * (g->pits[0] - 18) - 2;
2759 				g->ymx = 3 * (g->pits[g->nhd] - 18) + 2;
2760 			}
2761 			set_yval(s);
2762 			break;
2763 		case KEYSIG:
2764 			s->u.key.clef_delta =
2765 				staff_delta[staff]; /* keep the current clef */
2766 //			s->ymx = 24 + 10;
2767 //			s->ymn = -2;
2768 //			break;
2769 			/* fall thru */
2770 		default:
2771 			set_yval(s);
2772 			break;
2773 		case MREST:
2774 			if (s->flags & ABC_F_INVIS)
2775 				break;
2776 			s->ymx = 24 + 15;
2777 			s->ymn = -2;
2778 			break;
2779 		case NOTEREST:
2780 			if (s->abc_type != ABC_T_NOTE
2781 			 && !first_voice->next) {
2782 				s->y = 12;		/* rest single voice */
2783 				s->ymx = 24;
2784 				s->ymn = 0;
2785 				break;
2786 			}
2787 			np = s->nhd;
2788 			delta = staff_delta[staff];
2789 			if (delta != 0
2790 			 && voice_tb[s->voice].key.instr != K_DRUM) {
2791 				for (m = np; m >= 0; m--)
2792 					s->pits[m] += delta;
2793 			}
2794 			if (s->abc_type == ABC_T_NOTE) {
2795 				s->ymx = 3 * (s->pits[np] - 18) + 4;
2796 				s->ymn = 3 * (s->pits[0] - 18) - 4;
2797 			} else {
2798 				s->y = (s->pits[0] - 18) / 2 * 6;
2799 				s->ymx = s->y + rest_sp[5 - s->nflags][0];
2800 				s->ymn = s->y - rest_sp[5 - s->nflags][1];
2801 			}
2802 			if (s->dur < dur)
2803 				dur = s->dur;
2804 			break;
2805 		}
2806 	}
2807 	smallest_duration = dur;
2808 }
2809 
2810 /* -- set the stem direction when multi-voices -- */
2811 /* this function is called only once per tune */
set_stem_dir(void)2812 static void set_stem_dir(void)
2813 {
2814 	struct SYSTEM *sy;
2815 	struct SYMBOL *s, *t, *u;
2816 	int i, staff, nst, rvoice, voice;
2817 	struct {
2818 		int nvoice;
2819 		struct {
2820 			int voice;
2821 			short ymn;
2822 			short ymx;
2823 		} st[4];		/* (no more than 4 voices per staff) */
2824 	} stb[MAXSTAFF];
2825 	struct {
2826 		signed char st1, st2;	/* (a voice cannot be on more than 2 staves) */
2827 	} vtb[MAXVOICE];
2828 
2829 	s = tsfirst;
2830 	sy = cursys;
2831 	nst = sy->nstaff;
2832 	while (s) {
2833 		for (staff = nst; staff >= 0; staff--) {
2834 			stb[staff].nvoice = -1;
2835 			for (i = 4; --i >= 0; ) {
2836 				stb[staff].st[i].voice = -1;
2837 				stb[staff].st[i].ymx = 0;
2838 				stb[staff].st[i].ymn = 24;
2839 			}
2840 		}
2841 		for (i = 0; i < MAXVOICE; i++)
2842 			vtb[i].st1 = vtb[i].st2 = -1;
2843 
2844 		/* get the max/min offsets in the delta time */
2845 /*fixme: the stem height is not calculated yet*/
2846 		for (u = s; u; u = u->ts_next) {
2847 			if (u->type == BAR)
2848 				break;
2849 			if (u->sflags & S_NEW_SY) {
2850 				if (u != s)
2851 					break;
2852 				sy = sy->next;
2853 				for (staff = nst; staff <= sy->nstaff; staff++) {
2854 					stb[staff].nvoice = -1;
2855 					for (i = 4; --i >= 0; ) {
2856 						stb[staff].st[i].voice = -1;
2857 						stb[staff].st[i].ymx = 0;
2858 						stb[staff].st[i].ymn = 24;
2859 					}
2860 				}
2861 				nst = sy->nstaff;
2862 			}
2863 			if (u->type != NOTEREST
2864 			 || (u->flags & ABC_F_INVIS))
2865 				continue;
2866 			staff = u->staff;
2867 #if 1
2868 /*fixme:test*/
2869 if (staff > nst) {
2870 	bug("set_stem_dir(): bad staff number\n", 1);
2871 }
2872 #endif
2873 			voice = u->voice;
2874 			if (vtb[voice].st1 < 0) {
2875 				vtb[voice].st1 = staff;
2876 			} else if (vtb[voice].st1 != staff) {
2877 				if (staff > vtb[voice].st1) {
2878 					if (staff > vtb[voice].st2)
2879 						vtb[voice].st2 = staff;
2880 				} else {
2881 					if (vtb[voice].st1 > vtb[voice].st2)
2882 						vtb[voice].st2 = vtb[voice].st1;
2883 					vtb[voice].st1 = staff;
2884 				}
2885 			}
2886 			rvoice = sy->voice[voice].range;
2887 			for (i = stb[staff].nvoice; i >= 0; i--) {
2888 				if (stb[staff].st[i].voice == rvoice)
2889 					break;
2890 			}
2891 			if (i < 0) {
2892 				if (++stb[staff].nvoice >= 4)
2893 					bug("Too many voices per staff", 1);
2894 				for (i = 0; i < stb[staff].nvoice; i++) {
2895 					if (rvoice < stb[staff].st[i].voice) {
2896 						memmove(&stb[staff].st[i + 1],
2897 							&stb[staff].st[i],
2898 							sizeof stb[staff].st[i]
2899 								* (stb[staff].nvoice - i));
2900 						stb[staff].st[i].ymx = 0;
2901 						stb[staff].st[i].ymn = 24;
2902 						break;
2903 					}
2904 				}
2905 				stb[staff].st[i].voice = rvoice;
2906 			}
2907 
2908 			if (u->abc_type != ABC_T_NOTE)
2909 				continue;
2910 			if (u->ymx > stb[staff].st[i].ymx)
2911 				stb[staff].st[i].ymx = u->ymx;
2912 			if (u->ymn < stb[staff].st[i].ymn)
2913 				stb[staff].st[i].ymn = u->ymn;
2914 			if (u->sflags & S_XSTEM) {
2915 				if (u->ts_prev->staff != staff - 1
2916 				 || u->ts_prev->abc_type != ABC_T_NOTE) {
2917 					error(1, s, "Bad !xstem!");
2918 					u->sflags &= ~S_XSTEM;
2919 /*fixme:nflags KO*/
2920 				} else {
2921 					u->ts_prev->multi = 1;
2922 					u->multi = 1;
2923 					u->flags |= ABC_F_STEMLESS;
2924 				}
2925 			}
2926 		}
2927 
2928 		for ( ; s != u; s = s->ts_next) {
2929 			if (s->multi)
2930 				continue;
2931 			if (s->type != NOTEREST		/* if not note nor rest */
2932 			 && s->type != GRACE)
2933 				continue;
2934 			staff = s->staff;
2935 			voice = s->voice;
2936 //			if (!s->multi && vtb[voice].st2 >= 0) {
2937 			if (vtb[voice].st2 >= 0) {
2938 				if (staff == vtb[voice].st1)
2939 					s->multi = -1;
2940 				else if (staff == vtb[voice].st2)
2941 					s->multi = 1;
2942 				continue;
2943 			}
2944 			if (stb[staff].nvoice <= 0) { /* voice alone on the staff */
2945 //				if (s->multi)
2946 //					continue;
2947 /*fixme:could be done in set_float()*/
2948 				if (s->sflags & S_FLOATING) {
2949 					if (staff == voice_tb[voice].staff)
2950 						s->multi = -1;
2951 					else
2952 						s->multi = 1;
2953 				}
2954 				continue;
2955 			}
2956 			rvoice = sy->voice[voice].range;
2957 			for (i = stb[staff].nvoice; i >= 0; i--) {
2958 				if (stb[staff].st[i].voice == rvoice)
2959 					break;
2960 			}
2961 			if (i < 0)
2962 				continue;		/* voice ignored */
2963 			if (i == stb[staff].nvoice) {
2964 				s->multi = -1;	/* last voice */
2965 			} else {
2966 				s->multi = 1;	/* first voice(s) */
2967 
2968 				/* if 3 voices, and vertical space enough,
2969 				 * have stems down for the middle voice */
2970 				if (i != 0
2971 				 && i + 1 == stb[staff].nvoice) {
2972 					if (stb[staff].st[i].ymn - cfmt.stemheight
2973 					    > stb[staff].st[i + 1].ymx)
2974 						s->multi = -1;
2975 
2976 					/* special case for unison */
2977 					if (s->ts_prev
2978 					 && s->ts_prev->time == s->time
2979 					 && s->ts_prev->staff == s->staff
2980 					 && s->pits[s->nhd] == s->ts_prev->pits[0]
2981 					 && (s->sflags & (S_BEAM_ST | S_BEAM_END))
2982 							== (S_BEAM_ST | S_BEAM_END)
2983 					 && ((t = s->ts_next) == NULL
2984 					  || t->staff != s->staff
2985 					  || t->time != s->time))
2986 						s->multi = -1;
2987 				}
2988 			}
2989 		}
2990 
2991 		while (s && s->type == BAR) {
2992 			if (s->sflags & S_NEW_SY) {
2993 				sy = sy->next;
2994 				nst = sy->nstaff;
2995 			}
2996 			s = s->ts_next;
2997 		}
2998 	}
2999 }
3000 
3001 /* -- adjust the offset of the rests when many voices -- */
3002 /* this function is called only once per tune */
set_rest_offset(void)3003 static void set_rest_offset(void)
3004 {
3005 	struct SYSTEM *sy;
3006 	struct SYMBOL *s, *s2;
3007 	int nvoice, voice, end_time, not_alone, ymax, ymin,
3008 		shift, dots;
3009 	float dx;
3010 	struct {
3011 		struct SYMBOL *s;
3012 		int staff;
3013 		int end_time;
3014 	} vtb[MAXVOICE], *v;
3015 
3016 	memset(vtb, 0, sizeof vtb);
3017 
3018 	sy = cursys;
3019 	nvoice = 0;
3020 	for (s = tsfirst; s; s = s->ts_next) {
3021 		if (s->flags & ABC_F_INVIS)
3022 			continue;
3023 		if (s->sflags & S_NEW_SY)
3024 			sy = sy->next;
3025 		if (s->type != NOTEREST)
3026 			continue;
3027 		if (s->voice > nvoice)
3028 			nvoice = s->voice;
3029 		v = &vtb[s->voice];
3030 		v->s = s;
3031 		v->staff = s->staff;
3032 		v->end_time = s->time + s->dur;
3033 		if (s->abc_type != ABC_T_REST)
3034 			continue;
3035 
3036 		/* check if clash with previous symbols */
3037 		ymin = -127;
3038 		ymax = 127;
3039 		not_alone = dots = 0;
3040 		for (voice = 0, v = vtb; voice <= nvoice; voice++, v++) {
3041 			s2 = v->s;
3042 			if (!s2
3043 			 || v->staff != s->staff
3044 			 || voice == s->voice)
3045 				continue;
3046 			if (v->end_time <= s->time)
3047 				continue;
3048 			not_alone++;
3049 			if (sy->voice[voice].range < sy->voice[s->voice].range) {
3050 				if (s2->time == s->time) {
3051 					if (s2->ymn < ymax) {
3052 						ymax = s2->ymn;
3053 						if (s2->dots)
3054 							dots = 1;
3055 					}
3056 				} else {
3057 					if (s2->y < ymax)
3058 						ymax = s2->y;
3059 				}
3060 			} else {
3061 				if (s2->time == s->time) {
3062 					if (s2->ymx > ymin) {
3063 						ymin = s2->ymx;
3064 						if (s2->dots)
3065 							dots = 1;
3066 					}
3067 				} else {
3068 					if (s2->y > ymin)
3069 						ymin = s2->y;
3070 				}
3071 			}
3072 		}
3073 
3074 		/* check if clash with next symbols */
3075 		end_time = s->time + s->dur;
3076 		for (s2 = s->ts_next; s2; s2 = s2->ts_next) {
3077 			if (s2->time >= end_time)
3078 				break;
3079 			if (s2->staff != s->staff
3080 			 || s2->type != NOTEREST
3081 			 || (s2->flags & ABC_F_INVIS))
3082 				continue;
3083 			not_alone++;
3084 			if (sy->voice[s2->voice].range < sy->voice[s->voice].range) {
3085 				if (s2->time == s->time) {
3086 					if (s2->ymn < ymax) {
3087 						ymax = s2->ymn;
3088 						if (s2->dots)
3089 							dots = 1;
3090 					}
3091 				} else {
3092 					if (s2->y < ymax)
3093 						ymax = s2->y;
3094 				}
3095 			} else {
3096 				if (s2->time == s->time) {
3097 					if (s2->ymx > ymin) {
3098 						ymin = s2->ymx;
3099 						if (s2->dots)
3100 							dots = 1;
3101 					}
3102 				} else {
3103 					if (s2->y > ymin)
3104 						ymin = s2->y;
3105 				}
3106 			}
3107 		}
3108 		shift = ymax - s->ymx;
3109 		if (shift < 0) {
3110 			shift = (-shift + 5) / 6 * 6;
3111 			if (s->ymn - shift >= ymin) {
3112 				s->y -= shift;
3113 				s->ymx -= shift;
3114 				s->ymn -= shift;
3115 				continue;
3116 			}
3117 			dx = dots ? 15 : 10;
3118 			s->u.note.notes[0].shhd = dx;
3119 			s->xmx = dx;
3120 			continue;
3121 		}
3122 		shift = ymin - s->ymn;
3123 		if (shift > 0) {
3124 			shift = (shift + 5) / 6 * 6;
3125 			if (s->ymx + shift <= ymax) {
3126 				s->y += shift;
3127 				s->ymx += shift;
3128 				s->ymn += shift;
3129 				continue;
3130 			}
3131 			dx = dots ? 15 : 10;
3132 			s->u.note.notes[0].shhd = dx;
3133 			s->xmx = dx;
3134 			continue;
3135 		}
3136 		if (!not_alone) {
3137 			s->y = 12;
3138 			s->ymx = 24;
3139 			s->ymn = 0;
3140 		}
3141 	}
3142 }
3143 
3144 /* -- create a starting symbol -- */
sym_new(int type,struct VOICE_S * p_voice,struct SYMBOL * last_s)3145 static struct SYMBOL *sym_new(int type,
3146 				struct VOICE_S *p_voice,
3147 				struct SYMBOL *last_s)	/* same time */
3148 {
3149 	struct SYMBOL *s;
3150 
3151 	s = (struct SYMBOL *) getarena(sizeof *s);
3152 	memset(s, 0, sizeof *s);
3153 	s->type = type;
3154 	s->voice = p_voice - voice_tb;
3155 	s->staff = p_voice->staff;
3156 	s->time = last_s->time;
3157 
3158 	s->next = p_voice->last_sym->next;
3159 	if (s->next)
3160 		s->next->prev = s;
3161 	p_voice->last_sym->next = s;
3162 	s->prev = p_voice->last_sym;
3163 	p_voice->last_sym = s;
3164 
3165 	s->ts_next = last_s;
3166 	s->ts_prev = last_s->ts_prev;
3167 	s->ts_prev->ts_next = s;
3168 	if (!s->ts_prev || s->ts_prev->type != type)
3169 		s->sflags |= S_SEQST;
3170 	last_s->ts_prev = s;
3171 	if (last_s->type == type && s->voice != last_s->voice) {
3172 		last_s->sflags &= ~S_SEQST;
3173 		last_s->shrink = 0;
3174 	}
3175 	s->fn = last_s->fn;
3176 	s->linenum = last_s->linenum;
3177 	s->colnum = last_s->colnum;
3178 	return s;
3179 }
3180 
3181 /* -- init the symbols at start of a music line -- */
init_music_line(void)3182 static void init_music_line(void)
3183 {
3184 	struct VOICE_S *p_voice;
3185 	struct SYMBOL *s, *last_s;
3186 	int voice, staff;
3187 
3188 	/* initialize the voices */
3189 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
3190 		voice = p_voice - voice_tb;
3191 		p_voice->last_sym = p_voice->sym;
3192 		if (cursys->voice[voice].range < 0)
3193 			continue;
3194 		p_voice->second = cursys->voice[voice].second;
3195 
3196 		/* move the voice to a non empty staff */
3197 		staff = cursys->voice[voice].staff;
3198 		while (staff < nstaff && cursys->staff[staff].empty)
3199 			staff++;
3200 		p_voice->staff = staff;
3201 	}
3202 
3203 	/* add a clef at start of the main voices */
3204 	last_s = tsfirst;
3205 	while (last_s->type == CLEF) {		/* move the starting clefs */
3206 		voice = last_s->voice;
3207 		p_voice = &voice_tb[voice];
3208 		if (cursys->voice[voice].range >= 0
3209 		 && !cursys->voice[voice].second) {
3210 			last_s->aux = 0;			/* normal clef */
3211 			p_voice->last_sym = p_voice->sym = last_s;
3212 		}
3213 		last_s = last_s->ts_next;
3214 	}
3215 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
3216 		if (p_voice->sym && p_voice->sym->type == CLEF)
3217 			continue;
3218 		voice = p_voice - voice_tb;
3219 		if (cursys->voice[voice].range < 0
3220 		 || cursys->voice[voice].second)
3221 			continue;
3222 		staff = cursys->voice[voice].staff;
3223 #if 0
3224 		if (last_s->voice == voice && last_s->type == CLEF) {
3225 			last_s->aux = 0;			/* normal clef */
3226 #if 0
3227 			if (cursys->staff[staff].clef.invis)
3228 				s->flags |= ABC_F_INVIS;
3229 #endif
3230 			p_voice->last_sym = p_voice->sym = last_s;
3231 			last_s = last_s->ts_next;
3232 			continue;
3233 		}
3234 #endif
3235 		if (!staff_tb[staff].s_clef)
3236 			continue;			// no clef
3237 
3238 		s = (struct SYMBOL *) getarena(sizeof *s);
3239 		memset(s, 0, sizeof *s);
3240 		memcpy(&s->u.clef, &staff_tb[staff].s_clef->u.clef,
3241 				sizeof s->u.clef);
3242 		s->type = CLEF;
3243 		s->voice = voice;
3244 		s->staff = staff;
3245 		s->time = last_s->time;
3246 		s->next = p_voice->sym;
3247 		if (s->next) {
3248 			s->next->prev = s;
3249 			s->fn = s->next->fn;
3250 			s->linenum = s->next->linenum;
3251 			s->colnum = s->next->colnum;
3252 		}
3253 		p_voice->last_sym = p_voice->sym = s;
3254 		s->ts_next = last_s;
3255 		s->ts_prev = last_s->ts_prev;
3256 		if (!s->ts_prev) {
3257 			tsfirst = s;
3258 			s->sflags |= S_SEQST;
3259 		} else {
3260 			s->ts_prev->ts_next = s;
3261 		}
3262 		last_s->ts_prev = s;
3263 		if (last_s->type == CLEF)
3264 			last_s->sflags &= ~S_SEQST;
3265 //		if (cursys->voice[voice].second)
3266 //			s->sflags |= S_SECOND;
3267 		if (staff_tb[staff].s_clef->u.clef.invis
3268 		 || cursys->staff[staff].empty)
3269 			s->flags |= ABC_F_INVIS;
3270 //		set_yval(s);
3271 	}
3272 
3273 	/* add keysig */
3274 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
3275 		voice = p_voice - voice_tb;
3276 		if (cursys->voice[voice].range < 0
3277 		 || cursys->voice[voice].second
3278 		 || cursys->staff[cursys->voice[voice].staff].empty)
3279 			continue;
3280 		if (last_s->voice == voice && last_s->type == KEYSIG) {
3281 			p_voice->last_sym = last_s;
3282 			last_s->aux = last_s->u.key.sf;	// no key cancel
3283 			last_s = last_s->ts_next;
3284 			continue;
3285 		}
3286 		if (p_voice->key.sf != 0 || p_voice->key.nacc != 0) {
3287 			s = sym_new(KEYSIG, p_voice, last_s);
3288 			memcpy(&s->u.key, &p_voice->key, sizeof s->u.key);
3289 			if (s->u.key.instr == K_Hp)
3290 				s->aux = 3;	/* "A" -> "D" => G natural */
3291 //			set_yval(s);
3292 		}
3293 	}
3294 
3295 	/* add time signature if needed */
3296 	if (insert_meter & 1) {
3297 		for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
3298 			voice = p_voice - voice_tb;
3299 			if (cursys->voice[voice].range < 0
3300 			 || cursys->voice[voice].second
3301 			 || cursys->staff[cursys->voice[voice].staff].empty
3302 			 || p_voice->meter.nmeter == 0)		/* M:none */
3303 				continue;
3304 			if (last_s->voice == voice && last_s->type == TIMESIG) {
3305 				p_voice->last_sym = last_s;
3306 				last_s = last_s->ts_next;
3307 				continue;
3308 			}
3309 			s = sym_new(TIMESIG, p_voice, last_s);
3310 			memcpy(&s->u.meter, &p_voice->meter,
3311 			       sizeof s->u.meter);
3312 //			set_yval(s);
3313 		}
3314 		insert_meter &= ~1;		// no meter any more
3315 	}
3316 
3317 	/* add bar if needed (for repeat bracket) */
3318 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
3319 		int bar_start;
3320 
3321 		// if bar already, keep it in sequence
3322 		voice = p_voice - voice_tb;
3323 		if (last_s->voice == voice && last_s->type == BAR) {
3324 			p_voice->last_sym = last_s;
3325 			last_s = last_s->ts_next;
3326 			continue;
3327 		}
3328 
3329 		bar_start = p_voice->bar_start;
3330 		if (!bar_start)
3331 			continue;
3332 		p_voice->bar_start = 0;
3333 
3334 		if (cursys->voice[voice].range < 0
3335 		 || cursys->voice[voice].second
3336 		 || cursys->staff[cursys->voice[voice].staff].empty)
3337 			continue;
3338 
3339 		s = sym_new(BAR, p_voice, last_s);
3340 		s->u.bar.type = bar_start & 0x0fff;
3341 		if (bar_start & 0x8000)
3342 			s->flags |= ABC_F_INVIS;
3343 		if (bar_start & 0x4000)
3344 			s->sflags |= S_NOREPBRA;
3345 		if (bar_start & 0x2000)
3346 			s->flags |= ABC_F_RBSTART;
3347 		if (bar_start & 0x1000)
3348 			s->sflags |= S_RBSTART;
3349 		s->text = p_voice->bar_text;
3350 		s->gch = p_voice->bar_gch;
3351 		if (p_voice->bar_repeat)
3352 			s->u.bar.repeat_bar = p_voice->bar_repeat;
3353 
3354 		p_voice->bar_repeat = 0;
3355 		p_voice->bar_text = NULL;
3356 		p_voice->bar_gch = NULL;
3357 	}
3358 
3359 	/* if initialization of a new music line, compute the spacing,
3360 	 * including the first (old) sequence */
3361 	set_pitch(last_s);
3362 	s = last_s;
3363 	if (s) {
3364 		for ( ; s; s = s->ts_next)
3365 			if (s->sflags & S_SEQST)
3366 				break;
3367 		if (s)
3368 		    for (s = s->ts_next; s; s = s->ts_next)
3369 			if (s->sflags & S_SEQST)
3370 				break;
3371 	}
3372 	set_allsymwidth(s);	/* set the width of the added symbols */
3373 }
3374 
3375 /* -- set a pitch in all symbols and the start/stop of the beams -- */
set_words(struct VOICE_S * p_voice)3376 static void set_words(struct VOICE_S *p_voice)
3377 {
3378 	int pitch, beam_start;
3379 	struct SYMBOL *s, *s2, *lastnote;
3380 
3381 	for (s = p_voice->sym; s; s = s->next) {
3382 		if (s->abc_type == ABC_T_NOTE) {
3383 			pitch = s->pits[0];
3384 			break;
3385 		}
3386 	}
3387 	if (!s)
3388 		pitch = 127;			/* no note */
3389 	beam_start = 1;
3390 	lastnote = NULL;
3391 	for (s = p_voice->sym; s; s = s->next) {
3392 		switch (s->type) {
3393 		default:
3394 			if (s->flags & ABC_F_SPACE)
3395 				beam_start = 1;
3396 			break;
3397 		case MREST:
3398 			beam_start = 1;
3399 			break;
3400 		case BAR:
3401 			if (!(s->sflags & S_BEAM_ON))
3402 				beam_start = 1;
3403 
3404 			/* change the last long note to the square note */
3405 			if (!s->next && s->prev
3406 			 && s->prev->abc_type == ABC_T_NOTE
3407 			 && s->prev->dur >= BREVE)
3408 				s->prev->head = H_SQUARE;
3409 			break;
3410 		case NOTEREST:
3411 			if (s->sflags & S_TREM2)
3412 				break;
3413 			if (s->flags & ABC_F_SPACE)
3414 				beam_start = 1;
3415 			if (beam_start
3416 			 || s->nflags - s->aux <= 0) {
3417 				if (lastnote) {
3418 					lastnote->sflags |= S_BEAM_END;
3419 					lastnote = NULL;
3420 				}
3421 				if (s->nflags - s->aux <= 0) {
3422 					s->sflags |= (S_BEAM_ST | S_BEAM_END);
3423 				} else if (s->abc_type == ABC_T_NOTE) {
3424 					s->sflags |= S_BEAM_ST;
3425 					beam_start = 0;
3426 				}
3427 			}
3428 			if (s->sflags & S_BEAM_END)
3429 				beam_start = 1;
3430 			if (s->abc_type == ABC_T_NOTE)
3431 				lastnote = s;
3432 			break;
3433 		}
3434 		if (s->abc_type == ABC_T_NOTE) {
3435 			pitch = s->pits[0];
3436 //			if (s->prev
3437 //			 && s->prev->abc_type != ABC_T_NOTE) {
3438 //				s->prev->pits[0] =
3439 //					(s->prev->pits[0] + pitch)
3440 //						/ 2;
3441 			for (s2 = s->prev; s2; s2 = s2->prev) {
3442 				if (s2->abc_type != ABC_T_REST)
3443 					break;
3444 				s2->pits[0] = pitch;
3445 			}
3446 		} else {
3447 			s->pits[0] = pitch;
3448 		}
3449 	}
3450 	if (lastnote)
3451 		lastnote->sflags |= S_BEAM_END;
3452 }
3453 
3454 /* -- set the end of the repeat sequences -- */
set_rb(struct VOICE_S * p_voice)3455 static void set_rb(struct VOICE_S *p_voice)
3456 {
3457 	struct SYMBOL *s, *s2;
3458 	int mx, n;
3459 
3460 	s = p_voice->sym;
3461 	while (s) {
3462 		if (s->type != BAR || !(s->sflags & S_RBSTART)
3463 		 || (s->sflags & S_NOREPBRA)) {
3464 			s = s->next;
3465 			continue;
3466 		}
3467 
3468 		mx = cfmt.rbmax;
3469 
3470 		/* if 1st repeat sequence, compute the bracket length */
3471 		if (s->text && s->text[0] == '1') {
3472 			n = 0;
3473 			s2 = NULL;
3474 			for (s = s->next; s; s = s->next) {
3475 				if (s->type != BAR)
3476 					continue;
3477 				n++;
3478 				if (s->sflags & S_RBSTOP) {
3479 					if (n <= cfmt.rbmax) {
3480 						mx = n;
3481 						s2 = NULL;
3482 					}
3483 					break;
3484 				}
3485 				if (n == cfmt.rbmin)
3486 					s2 = s;
3487 			}
3488 			if (s2) {
3489 				s2->sflags |= S_RBSTOP;
3490 				mx = cfmt.rbmin;
3491 			}
3492 		}
3493 		while (s) {
3494 
3495 			/* check repbra shifts (:| | |2 in 2nd staves) */
3496 			if (!(s->flags & ABC_F_RBSTART)) {
3497 				s = s->next;
3498 				if (!s)
3499 					break;
3500 				if (!(s->flags & ABC_F_RBSTART)) {
3501 					s = s->next;
3502 					if (!s)
3503 						break;
3504 					if (!(s->flags & ABC_F_RBSTART))
3505 						break;
3506 				}
3507 			}
3508 			n = 0;
3509 			s2 = NULL;
3510 			for (s = s->next; s; s = s->next) {
3511 				if (s->type != BAR)
3512 					continue;
3513 				n++;
3514 				if (s->sflags & S_RBSTOP)
3515 					break;
3516 				if (!s->next) {
3517 					s->flags |= ABC_F_RBSTOP;
3518 					s->sflags |= S_RBSTOP;
3519 				} else if (n == mx) {
3520 					s->sflags |= S_RBSTOP;
3521 				}
3522 			}
3523 		}
3524 	}
3525 }
3526 
3527 /* -- initialize the generator -- */
3528 /* this function is called only once per tune  */
set_global(void)3529 static void set_global(void)
3530 {
3531 	struct SYSTEM *sy;
3532 	struct SYMBOL *s;
3533 	struct VOICE_S *p_voice;
3534 	int staff;
3535 	static const signed char delpit[4] = {0, -7, -14, 0};
3536 
3537 	/* get the max number of staves */
3538 	sy = cursys;
3539 	staff = cursys->nstaff;
3540 	while ((sy = sy->next) != NULL) {
3541 		if (sy->nstaff > staff)
3542 			staff = sy->nstaff;
3543 	}
3544 	nstaff = staff;
3545 
3546 	/* adjust the pitches if old abc2ps behaviour of clef definition */
3547 	if (cfmt.abc2pscompat) {
3548 		int i;
3549 
3550 		for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
3551 			int delta;
3552 			struct SYMBOL *g;
3553 
3554 			if (p_voice->octave != 0)
3555 				continue;
3556 #if 0
3557 			/* (the clefs in the voice table are not yet initialized) */
3558 //			i = p_voice->staff;
3559 //			i = cursys->staff[i].clef.type;
3560 			i = cursys->voice[p_voice - voice_tb].clef.type;
3561 #else
3562 			i = p_voice->s_clef->u.clef.type;
3563 #endif
3564 			if (i == PERC)
3565 				continue;
3566 			delta = delpit[i];
3567 			for (s = p_voice->sym; s; s = s->next) {
3568 				switch (s->type) {
3569 				case CLEF:
3570 					i = s->u.clef.type;
3571 					if (!s->u.clef.check_pitch)
3572 						i = 0;
3573 					delta = delpit[i];
3574 					break;
3575 				case NOTEREST:
3576 					if (delta == 0)
3577 						break;
3578 					if (s->abc_type == ABC_T_REST)
3579 						break;
3580 					for (i = s->nhd; i >= 0; i--)
3581 						s->pits[i] += delta;
3582 					break;
3583 				case GRACE:
3584 					if (delta == 0)
3585 						break;
3586 					for (g = s->extra; g; g = g->next) {
3587 						if (g->type != NOTEREST)
3588 							continue;
3589 						for (i = g->nhd; i >= 0; i--)
3590 							g->pits[i] += delta;
3591 					}
3592 					break;
3593 				}
3594 			}
3595 		}
3596 	}
3597 
3598 	/* set the pitches, the words (beams) and the repeat brackets */
3599 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
3600 		set_words(p_voice);
3601 //		if (!p_voice->second && !p_voice->norepbra)
3602 			set_rb(p_voice);
3603 	}
3604 
3605 	/* set the staff of the floating voices */
3606 	set_float();
3607 
3608 	// set the clefs and adjust the pitches of all symbols
3609 	set_clefs();
3610 	set_pitch(NULL);
3611 }
3612 
3613 /* -- return the left indentation of the staves -- */
set_indent(void)3614 static float set_indent(void)
3615 {
3616 	int staff, voice;
3617 	float w, maxw;
3618 	struct VOICE_S *p_voice;
3619 	char *p, *q;
3620 
3621 	maxw = 0;
3622 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
3623 		voice = p_voice - voice_tb;
3624 		if (cursys->voice[voice].range < 0)
3625 			continue;
3626 		staff = cursys->voice[voice].staff;
3627 		if (cursys->staff[staff].empty)
3628 			continue;
3629 		if ((p = p_voice->new_name ? p_voice->nm : p_voice->snm) == NULL)
3630 			continue;
3631 		str_font(VOICEFONT);
3632 		for (;;) {
3633 			if ((q = strstr(p, "\\n")) != NULL)
3634 				*q = '\0';
3635 			w = tex_str(p);
3636 			if (w > maxw)
3637 				maxw = w;
3638 			if (!q)
3639 				break;
3640 			*q = '\\';
3641 			p = q + 2;
3642 		}
3643 	}
3644 
3645 	if (maxw != 0) {
3646 		w = 0;
3647 //		for (staff = 0; staff <= nstaff; staff++) {
3648 		for (staff = 0; staff <= cursys->nstaff; staff++) {
3649 			if (cursys->staff[staff].flags
3650 					& (OPEN_BRACE2 | OPEN_BRACKET2)) {
3651 				w = 20;
3652 				break;
3653 			}
3654 			if ((cursys->staff[staff].flags
3655 					& (OPEN_BRACE | OPEN_BRACKET))
3656 			 && w == 0)
3657 				w = 10;
3658 		}
3659 		maxw += 4 * cwid(' ') * cfmt.font_tb[VOICEFONT].swfac + w;
3660 	}
3661 	if (insert_meter & 2)			/* if indent */
3662 		maxw += cfmt.indent;
3663 	return maxw;
3664 }
3665 
3666 /* -- decide on beams and on stem directions -- */
3667 /* this routine is called only once per tune */
set_beams(struct SYMBOL * sym)3668 static void set_beams(struct SYMBOL *sym)
3669 {
3670 	struct SYSTEM *sy;
3671 	struct SYMBOL *s, *t, *g, *s_opp;
3672 	int n, m, beam, laststem, mid_p;
3673 
3674 	beam = 0;
3675 	laststem = -1;
3676 	s_opp = NULL;
3677 	sy = cursys;
3678 	for (s = sym; s; s = s->next) {
3679 		if (s->abc_type != ABC_T_NOTE) {
3680 			switch (s->type) {
3681 			default:
3682 				continue;
3683 			case STAVES:
3684 				sy = sy->next;
3685 				continue;
3686 			case GRACE:
3687 				break;
3688 			}
3689 			g = s->extra;
3690 			while (g->abc_type != ABC_T_NOTE)
3691 				g = g->next;
3692 			if (g->stem == 2) {	/* opposite gstem direction */
3693 				s_opp = s;
3694 				continue;
3695 			}
3696 			if (s->stem == 0
3697 			 && (s->stem = s->multi) == 0)
3698 				s->stem = 1;
3699 			for (; g; g = g->next) {
3700 				g->stem = s->stem;
3701 				g->multi = s->multi;
3702 			}
3703 			continue;
3704 		}
3705 
3706 		mid_p = s->mid / 3 + 18;
3707 
3708 		if (s->stem == 0		/* if not explicitly set */
3709 		 && (s->stem = s->multi) == 0) { /* and alone on the staff */
3710 
3711 			/* notes in a beam have the same stem direction */
3712 			if (beam) {
3713 				s->stem = laststem;
3714 			} else if ((s->sflags & (S_BEAM_ST | S_BEAM_END))
3715 					== S_BEAM_ST) { /* start of beam */
3716 			    int pu = s->pits[s->nhd],
3717 				pd = s->pits[0];
3718 
3719 				beam = 1;
3720 				for (t = s->next; t; t = t->next) {
3721 					if (t->abc_type != ABC_T_NOTE)
3722 						continue;
3723 					if (t->stem || t->multi) {
3724 						s->stem = t->multi ? t->multi : t->stem;
3725 						break;
3726 					}
3727 					if (t->pits[t->nhd] > pu)
3728 						pu = t->pits[t->nhd];
3729 					if (t->pits[0] < pd)
3730 						pd = t->pits[0];
3731 					if (t->sflags & S_BEAM_END)
3732 						break;
3733 				}
3734 				if (t->sflags & S_BEAM_END) {
3735 					mid_p *= 2;
3736 					if (pu + pd < mid_p) {
3737 						s->stem = 1;
3738 					} else if (pu + pd > mid_p) {
3739 						s->stem = -1;
3740 					} else {
3741 						if (cfmt.bstemdown)
3742 							s->stem = -1;
3743 					}
3744 				}
3745 				if (!s->stem)
3746 					s->stem = laststem;
3747 			} else {				// no beam
3748 				n = s->pits[s->nhd] + s->pits[0];
3749 				if (n == mid_p * 2) {
3750 					n = 0;
3751 					for (m = 0; m <= s->nhd; m++)
3752 						n += s->pits[m];
3753 					mid_p *= s->nhd + 1;
3754 				} else {
3755 					mid_p *= 2;
3756 				}
3757 				if (n < mid_p)
3758 					s->stem = 1;
3759 				else if (n > mid_p)
3760 					s->stem = -1;
3761 				else if (cfmt.bstemdown)
3762 					s->stem = -1;
3763 				else
3764 					s->stem = laststem;
3765 			}
3766 		} else {			/* stem set by set_stem_dir */
3767 			if ((s->sflags & (S_BEAM_ST | S_BEAM_END))
3768 					== S_BEAM_ST) /* start of beam */
3769 				beam = 1;
3770 		}
3771 		if (s->sflags & S_BEAM_END)
3772 			beam = 0;
3773 		laststem = s->stem;
3774 
3775 		if (s_opp) {			/* opposite gstem direction */
3776 			for (g = s_opp->extra; g; g = g->next)
3777 				g->stem = -laststem;
3778 			s_opp->stem = -laststem;
3779 			s_opp = NULL;
3780 		}
3781 	}
3782 }
3783 
3784 /* handle unison in voice overlap */
same_head(struct SYMBOL * s1,struct SYMBOL * s2)3785 static int same_head(struct SYMBOL *s1, struct SYMBOL *s2)
3786 {
3787 	int i1, i2, l1, l2, i11, i12, i21, i22;
3788 	float sh1, sh2;
3789 
3790 	if ((s1->sflags & (S_SHIFTUNISON_1 | S_SHIFTUNISON_2))
3791 			== (S_SHIFTUNISON_1 | S_SHIFTUNISON_2))
3792 		return 0;
3793 	if ((l1 = s1->dur) >= SEMIBREVE)
3794 		return 0;
3795 	if ((l2 = s2->dur) >= SEMIBREVE)
3796 		return 0;
3797 	if (s1->flags & s2->flags & ABC_F_STEMLESS)
3798 		return 0;
3799 	if (s1->dots != s2->dots) {
3800 //		if ((s1->sflags & (S_SHIFTUNISON_1 | S_SHIFTUNISON_2))
3801 		if ((s1->sflags & S_SHIFTUNISON_1)
3802 		 || s1->dots * s2->dots != 0)
3803 			return 0;
3804 	}
3805 	if (s1->stem * s2->stem > 0)
3806 		return 0;
3807 
3808 	/* check if a common unison */
3809 	i1 = i2 = 0;
3810 	if (s1->pits[0] > s2->pits[0]) {
3811 		if (s1->stem < 0)
3812 			return 0;
3813 		while (s2->pits[i2] != s1->pits[0]) {
3814 			if (++i2 > s2->nhd)
3815 				return 0;
3816 		}
3817 	} else if (s1->pits[0] < s2->pits[0]) {
3818 		if (s2->stem < 0)
3819 			return 0;
3820 		while (s2->pits[0] != s1->pits[i1]) {
3821 			if (++i1 > s1->nhd)
3822 				return 0;
3823 		}
3824 	}
3825 	if (s2->u.note.notes[i2].acc != s1->u.note.notes[i1].acc)
3826 		return 0;
3827 	i11 = i1;
3828 	i21 = i2;
3829 	sh1 = s1->u.note.notes[i1].shhd;
3830 	sh2 = s2->u.note.notes[i2].shhd;
3831 	do {
3832 		i1++;
3833 		i2++;
3834 		if (i1 > s1->nhd) {
3835 //			if (s1->pits[0] < s2->pits[0])
3836 //				return 0;
3837 			break;
3838 		}
3839 		if (i2 > s2->nhd) {
3840 //			if (s1->pits[0] > s2->pits[0])
3841 //				return 0;
3842 			break;
3843 		}
3844 		if (s2->u.note.notes[i2].acc != s1->u.note.notes[i1].acc)
3845 			return 0;
3846 		if (sh1 < s1->u.note.notes[i1].shhd)
3847 			sh1 = s1->u.note.notes[i1].shhd;
3848 		if (sh2 < s2->u.note.notes[i2].shhd)
3849 			sh2 = s2->u.note.notes[i2].shhd;
3850 	} while (s2->pits[i2] == s1->pits[i1]);
3851 	if (i1 <= s1->nhd) {
3852 		if (i2 <= s2->nhd)
3853 			return 0;
3854 		if (s2->stem > 0)
3855 			return 0;
3856 	} else if (i2 <= s2->nhd) {
3857 		if (s1->stem > 0)
3858 			return 0;
3859 	}
3860 	i12 = i1;
3861 	i22 = i2;
3862 
3863 	if (l1 == l2)
3864 		goto same_head;
3865 	if (l1 < l2) {
3866 		l1 = l2;
3867 		l2 = s1->dur;
3868 	}
3869 	if (l1 < MINIM) {
3870 		if (s2->dots > 0)
3871 			goto head_2;
3872 		if (s1->dots > 0)
3873 			goto head_1;
3874 		goto same_head;
3875 	}
3876 	if (l2 < CROTCHET) {	/* (l1 >= MINIM) */
3877 //		if ((s1->sflags & S_SHIFTUNISON_2)
3878 //		 || s1->dots != s2->dots)
3879 		if (s1->sflags & S_SHIFTUNISON_2)
3880 			return 0;
3881 		if (s2->dur >= MINIM)
3882 			goto head_2;
3883 		goto head_1;
3884 	}
3885 	return 0;
3886 
3887 same_head:
3888 	if (voice_tb[s1->voice].scale < voice_tb[s2->voice].scale)
3889 		goto head_2;
3890 head_1:
3891 //	s2->nohdi1 = i21;	/* keep heads of 1st voice */
3892 //	s2->nohdi2 = i22;
3893 	for (i2 = i21; i2 < i22; i2++) {
3894 		s2->u.note.notes[i2].invisible = 1;
3895 		s2->u.note.notes[i2].acc = 0;
3896 	}
3897 	for (i2 = 0; i2 <= s2->nhd; i2++)
3898 		s2->u.note.notes[i2].shhd += sh1;
3899 	return 1;
3900 head_2:
3901 //	s1->nohdi1 = i11;	/* keep heads of 2nd voice */
3902 //	s1->nohdi2 = i12;
3903 	for (i1 = i11; i1 < i12; i1++) {
3904 		s1->u.note.notes[i1].invisible = 1;
3905 		s1->u.note.notes[i1].acc = 0;
3906 	}
3907 	for (i1 = 0; i1 <= s1->nhd; i1++)
3908 		s1->u.note.notes[i1].shhd += sh2;
3909 	return 1;
3910 }
3911 
3912 /* width of notes for voice overlap - index = head */
3913 static float w_note[] = {
3914 	3.5, 3.7, 5, 7
3915 };
3916 
3917 /* handle unison with different accidentals */
unison_acc(struct SYMBOL * s1,struct SYMBOL * s2,int i1,int i2)3918 static void unison_acc(struct SYMBOL *s1,
3919 			struct SYMBOL *s2,
3920 			int i1, int i2)
3921 {
3922 	int m;
3923 	float d;
3924 
3925 	if (s2->u.note.notes[i2].acc == 0) {
3926 		d = w_note[s2->head] * 2 + s2->xmx + s1->u.note.notes[i1].shac + 2;
3927 		if (s1->u.note.notes[i1].acc & 0xf8)
3928 			d += 2;
3929 		if (s2->dots)
3930 			d += 6;
3931 		for (m = 0; m <= s1->nhd; m++) {
3932 			s1->u.note.notes[m].shhd += d;
3933 			s1->u.note.notes[m].shac -= d;
3934 		}
3935 		s1->xmx += d;
3936 	} else {
3937 		d = w_note[s1->head] * 2 + s1->xmx + s2->u.note.notes[i2].shac + 2;
3938 		if (s2->u.note.notes[i2].acc & 0xf8)
3939 			d += 2;
3940 		if (s1->dots)
3941 			d += 6;
3942 		for (m = 0; m <= s2->nhd; m++) {
3943 			s2->u.note.notes[m].shhd += d;
3944 			s2->u.note.notes[m].shac -= d;
3945 		}
3946 		s2->xmx += d;
3947 	}
3948 }
3949 
3950 #define MAXPIT (48 * 2)
3951 
3952 /* set the left space of a note/chord */
set_left(struct SYMBOL * s,float * left)3953 static void set_left(struct SYMBOL *s, float *left)
3954 {
3955 	int m, i, j;
3956 	float w_base, w, shift;
3957 
3958 	for (i = 0; i < MAXPIT; i++)
3959 		left[i] = -100;
3960 
3961 	/* stem */
3962 	w = w_base = w_note[s->head];
3963 	if (s->nflags > -2) {
3964 		if (s->stem > 0) {
3965 			w = -w;
3966 			i = s->pits[0] * 2;
3967 			j = ((int) ((s->ymx - 2) / 3) + 18) * 2;
3968 		} else {
3969 			i = ((int) ((s->ymn + 2) / 3) + 18) * 2;
3970 			j = s->pits[s->nhd] * 2;
3971 		}
3972 		if (i < 0)
3973 			i = 0;
3974 		for (; i < MAXPIT && i <= j; i++)
3975 			left[i] = w;
3976 	}
3977 
3978 	/* notes */
3979 	if (s->stem > 0)
3980 		shift = s->u.note.notes[0].shhd;	/* previous shift */
3981 	else
3982 		shift = s->u.note.notes[s->nhd].shhd;
3983 	for (m = 0; m <= s->nhd; m++) {
3984 		w = -s->u.note.notes[m].shhd + w_base + shift;
3985 		i = s->pits[m] * 2;
3986 		if (i < 0)
3987 			i = 0;
3988 		else if (i >= MAXPIT - 1)
3989 			i = MAXPIT - 2;
3990 		if (w > left[i])
3991 			left[i] = w;
3992 		if (s->head != H_SQUARE)
3993 			w -= 1;
3994 		if (w > left[i - 1])
3995 			left[i - 1] = w;
3996 		if (w > left[i + 1])
3997 			left[i + 1] = w;
3998 	}
3999 }
4000 
4001 /* set the right space of a note/chord */
set_right(struct SYMBOL * s,float * right)4002 static void set_right(struct SYMBOL *s, float *right)
4003 {
4004 	int m, i, j, k, flags;
4005 	float w_base, w, shift;
4006 
4007 	for (i = 0; i < MAXPIT; i++)
4008 		right[i] = -100;
4009 
4010 	/* stem and flags */
4011 	flags = s->nflags > 0
4012 	     && (s->sflags & (S_BEAM_ST | S_BEAM_END))
4013 			== (S_BEAM_ST | S_BEAM_END);
4014 	w = w_base = w_note[s->head];
4015 	if (s->nflags > -2) {
4016 		if (s->stem < 0) {
4017 			w = -w;
4018 			i = ((int) ((s->ymn + 2) / 3) + 18) * 2;
4019 			j = s->pits[s->nhd] * 2;
4020 			k = i + 4;
4021 		} else {
4022 			i = s->pits[0] * 2;
4023 			j = ((int) ((s->ymx - 2) / 3) + 18) * 2;
4024 			k = i;				// (have gcc happy)
4025 		}
4026 		if (i < 0)
4027 			i = 0;
4028 		for ( ; i < MAXPIT && i < j; i++)
4029 			right[i] = w;
4030 	}
4031 
4032 	if (flags) {
4033 		if (s->stem > 0) {
4034 			if (s->xmx == 0)
4035 				i = s->pits[s->nhd] * 2;
4036 			else
4037 				i = s->pits[0] * 2;
4038 			i += 4;
4039 			if (i < 0)
4040 				i = 0;
4041 			for (; i < MAXPIT && i <= j - 4; i++)
4042 				right[i] = 11;
4043 		} else {
4044 			i = k;
4045 			if (i < 0)
4046 				i = 0;
4047 			for (; i < MAXPIT && i <= s->pits[0] * 2 - 4; i++)
4048 				right[i] = 3.5;
4049 		}
4050 	}
4051 
4052 	/* notes */
4053 	if (s->stem > 0)
4054 		shift = s->u.note.notes[0].shhd;	/* previous shift */
4055 	else
4056 		shift = s->u.note.notes[s->nhd].shhd;
4057 	for (m = 0; m <= s->nhd; m++) {
4058 		w = s->u.note.notes[m].shhd + w_base - shift;
4059 		i = s->pits[m] * 2;
4060 		if (i < 0)
4061 			i = 0;
4062 		else if (i >= MAXPIT - 1)
4063 			i = MAXPIT - 2;
4064 		if (w > right[i])
4065 			right[i] = w;
4066 		if (s->head != H_SQUARE)
4067 			w -= 1;
4068 		if (w > right[i - 1])
4069 			right[i - 1] = w;
4070 		if (w > right[i + 1])
4071 			right[i + 1] = w;
4072 	}
4073 }
4074 
4075 /* -- shift the notes horizontally when voices overlap -- */
4076 /* this routine is called only once per tune */
set_overlap(void)4077 static void set_overlap(void)
4078 {
4079 	struct SYMBOL *s, *s1, *s2, *s3;
4080 	int i, i1, i2, m, sd, t, dp;
4081 	float d, d2, dr, dr2, dx;
4082 	float left1[MAXPIT], right1[MAXPIT], left2[MAXPIT], right2[MAXPIT];
4083 	float right3[MAXPIT], *pl, *pr;
4084 
4085 	for (s = tsfirst; s; s = s->ts_next) {
4086 		if (s->abc_type != ABC_T_NOTE
4087 		 || (s->flags & ABC_F_INVIS))
4088 			continue;
4089 
4090 		/* treat the stem on two staves with different directions */
4091 		if ((s->sflags & S_XSTEM)
4092 		 && s->ts_prev->stem < 0) {
4093 			s2 = s->ts_prev;
4094 			for (m = 0; m <= s2->nhd; m++) {
4095 				s2->u.note.notes[m].shhd += STEM_XOFF * 2;
4096 				s2->u.note.notes[m].shac -= STEM_XOFF * 2;
4097 			}
4098 			s2->xmx += STEM_XOFF * 2;
4099 		}
4100 
4101 		/* search the next note at the same time on the same staff */
4102 		s2 = s;
4103 		for (;;) {
4104 			s2 = s2->ts_next;
4105 			if (!s2)
4106 				break;
4107 			if (s2->time != s->time) {
4108 				s2 = NULL;
4109 				break;
4110 			}
4111 			if (s2->abc_type == ABC_T_NOTE
4112 			 && !(s2->flags & ABC_F_INVIS)
4113 			 && s2->staff == s->staff)
4114 				break;
4115 		}
4116 		if (!s2)
4117 			continue;
4118 		s1 = s;
4119 
4120 		/* set the dot vertical offset */
4121 		if (cursys->voice[s1->voice].range < cursys->voice[s2->voice].range)
4122 			s2->doty = -3;
4123 		else
4124 			s1->doty = -3;
4125 
4126 		/* no shift if no overlap */
4127 		if (s1->ymn > s2->ymx
4128 		 || s1->ymx < s2->ymn)
4129 			continue;
4130 
4131 		if (same_head(s1, s2))
4132 			continue;
4133 
4134 		/* compute the minimum space for 's1 s2' and 's2 s1' */
4135 		set_right(s1, right1);
4136 		set_left(s2, left2);
4137 
4138 		s3 = s1->ts_prev;
4139 		if (s3 && s3->time == s1->time
4140 		 && s3->staff == s1->staff
4141 		 && s3->abc_type == ABC_T_NOTE
4142 		 && !(s3->flags & ABC_F_INVIS)) {
4143 			set_right(s3, right3);
4144 			for (i = 0; i < MAXPIT; i++) {
4145 				if (right3[i] > right1[i])
4146 					right1[i] = right3[i];
4147 			}
4148 		} else {
4149 			s3 = NULL;
4150 		}
4151 		d = -100;
4152 		for (i = 0; i < MAXPIT; i++) {
4153 			if (left2[i] + right1[i] > d)
4154 				d = left2[i] + right1[i];
4155 		}
4156 		if (d < -3) {			// no clash if no dots clash
4157 			if (!s1->dots || !s2->dots
4158 			 || s2->doty >= 0
4159 			 || s1->stem > 0 || s2->stem < 0
4160 			 || s1->pits[s1->nhd] + 2 != s2->pits[0]
4161 			 || (s2->pits[0] & 1))
4162 				continue;
4163 		}
4164 
4165 		set_right(s2, right2);
4166 		set_left(s1, left1);
4167 		if (s3) {
4168 			set_left(s3, right3);
4169 			for (i = 0; i < MAXPIT; i++) {
4170 				if (right3[i] > left1[i])
4171 					left1[i] = right3[i];
4172 			}
4173 		}
4174 		d2 = dr = dr2 = -100;
4175 		for (i = 0; i < MAXPIT; i++) {
4176 			if (left1[i] + right2[i] > d2)
4177 				d2 = left1[i] + right2[i];
4178 			if (right1[i] > dr)
4179 				dr = right1[i];
4180 			if (right2[i] > dr2)
4181 				dr2 = right2[i];
4182 		}
4183 
4184 		/* check for unison with different accidentals
4185 		 * and clash of dots */
4186 		t = 0;
4187 		i1 = s1->nhd;
4188 		i2 = s2->nhd;
4189 		for (;;) {
4190 			dp = s1->pits[i1] - s2->pits[i2];
4191 			switch (dp) {
4192 			case 0:
4193 				if (s1->u.note.notes[i1].acc
4194 						!= s2->u.note.notes[i2].acc) {
4195 					t = -1;
4196 					break;
4197 				}
4198 				if (s2->u.note.notes[i2].acc)
4199 					s2->u.note.notes[i2].acc = 0;
4200 				if (s1->dots && s2->dots
4201 				 && (s1->pits[i1] & 1))
4202 					t = 1;
4203 				break;
4204 			case -1:
4205 //				if (s1->dots && s2->dots)
4206 //					t = 1;
4207 				if (s1->dots && s2->dots) {
4208 					if (s1->pits[i1] & 1) {
4209 						s1->doty = 0;
4210 						s2->doty = 0;
4211 					} else {
4212 						s1->doty = -3;
4213 						s2->doty = -3;
4214 					}
4215 				}
4216 				break;
4217 			case -2:
4218 				if (s1->dots && s2->dots
4219 				 && !(s1->pits[i1] & 1)) {
4220 //					t = 1;
4221 					s1->doty = 0;
4222 					s2->doty = 0;
4223 					break;
4224 				}
4225 				break;
4226 			}
4227 			if (t < 0)
4228 				break;
4229 			if (dp >= 0) {
4230 				if (--i1 < 0)
4231 					break;
4232 			}
4233 			if (dp <= 0) {
4234 				if (--i2 < 0)
4235 					break;
4236 			}
4237 		}
4238 
4239 		if (t < 0) {		/* unison and different accidentals */
4240 			unison_acc(s1, s2, i1, i2);
4241 			continue;
4242 		}
4243 
4244 		sd = 0;
4245 		pl = left2;
4246 		pr = right2;
4247 		if (s1->dots) {
4248 			if (s2->dots) {
4249 				if (!t)			/* if no dot clash */
4250 					sd = 1;		/* align the dots */
4251 			}
4252 		} else if (s2->dots) {
4253 			if (d2 + dr < d + dr2)
4254 				sd = 1;			/* align the dots */
4255 		}
4256 		if (!s3 && d2 + dr < d + dr2) {
4257 			s1 = s2;			/* invert the voices */
4258 			s2 = s;
4259 			d = d2;
4260 			pl = left1;
4261 			pr = right1;
4262 			dr2 = dr;
4263 		}
4264 		d += 3;
4265 		if (d < 0)
4266 			d = 0;				// (not return!)
4267 
4268 		/* handle the previous shift */
4269 		m = s1->stem >= 0 ? 0 : s1->nhd;
4270 		d += s1->u.note.notes[m].shhd;
4271 		m = s2->stem >= 0 ? 0 : s2->nhd;
4272 		d -= s2->u.note.notes[m].shhd;
4273 
4274 		/*
4275 		 * room for the dots
4276 		 * - if the dots of v1 don't shift, adjust the shift of v2
4277 		 * - otherwise, align the dots and shift them if clash
4278 		 */
4279 		if (s1->dots) {
4280 			dx = 7.7 + s1->xmx +		// x 1st dot
4281 				3.5 * s1->dots - 3.5 +	// x last dot
4282 				3;			// some space
4283 			if (!sd) {
4284 				d2 = -100;
4285 				for (i1 = 0; i1 <= s1->nhd; i1++) {
4286 					i = s1->pits[i1];
4287 					if (!(i & 1)) {
4288 						if (s1->doty >= 0)
4289 							i++;
4290 						else
4291 							i--;
4292 					}
4293 					i *= 2;
4294 					if (i < 1)
4295 						i = 1;
4296 					else if (i >= MAXPIT - 1)
4297 						i = MAXPIT - 2;
4298 					if (pl[i] > d2)
4299 						d2 = pl[i];
4300 					if (pl[i - 1] + 1 > d2)
4301 						d2 = pl[i - 1] + 1;
4302 					if (pl[i + 1] + 1 > d2)
4303 						d2 = pl[i + 1] + 1;
4304 				}
4305 				if (dx + d2 + 2 > d)
4306 					d = dx + d2 + 2;
4307 			} else {
4308 				if (dx < d + dr2 + s2->xmx) {
4309 					d2 = 0;
4310 					for (i1 = 0; i1 <= s1->nhd; i1++) {
4311 						i = s1->pits[i1];
4312 						if (!(i & 1)) {
4313 							if (s1->doty >= 0)
4314 								i++;
4315 							else
4316 								i--;
4317 						}
4318 						i *= 2;
4319 						if (i < 1)
4320 							i = 1;
4321 						else if (i >= MAXPIT - 1)
4322 							i = MAXPIT - 2;
4323 						if (pr[i] > d2)
4324 							d2 = pr[i];
4325 						if (pr[i - 1] + 1> d2)
4326 							d2 = pr[i - 1] = 1;
4327 						if (pr[i + 1] + 1 > d2)
4328 							d2 = pr[i + 1] + 1;
4329 					}
4330 					if (d2 > 4.5
4331 					 && 7.7 + s1->xmx + 2 < d + d2 + s2->xmx)
4332 						s2->xmx = d2 + 3 - 7.7;
4333 				}
4334 			}
4335 		}
4336 
4337 		for (m = s2->nhd; m >= 0; m--) {
4338 			s2->u.note.notes[m].shhd += d;
4339 //			if (s2->u.note.accs[m] != 0
4340 //			 && s2->pits[m] < s1->pits[0] - 4)
4341 //				s2->shac[m] -= d;
4342 		}
4343 		s2->xmx += d;
4344 		if (sd)
4345 			s1->xmx = s2->xmx;	// align the dots
4346 	}
4347 }
4348 
4349 /* -- set the stem lengths -- */
4350 /* this routine is called only once per tune */
set_stems(void)4351 static void set_stems(void)
4352 {
4353 	struct SYSTEM *sy;
4354 	struct SYMBOL *s, *s2, *g;
4355 	float slen, scale;
4356 	int ymn, ymx, nflags, mid;
4357 
4358 	sy = cursys;
4359 	for (s = tsfirst; s; s = s->ts_next) {
4360 		if (s->abc_type != ABC_T_NOTE) {
4361 			int ymin, ymax;
4362 
4363 			switch (s->type) {
4364 			default:
4365 				continue;
4366 			case STAVES:
4367 				sy = sy->next;
4368 				continue;
4369 			case GRACE:
4370 				break;
4371 			}
4372 			ymin = ymax = s->mid;
4373 			for (g = s->extra; g; g = g->next) {
4374 				if (g->type != NOTEREST)
4375 					continue;
4376 				slen = GSTEM;
4377 				if (g->nflags > 1)
4378 					slen += 1.2 * (g->nflags - 1);
4379 				ymn = 3 * (g->pits[0] - 18);
4380 				ymx = 3 * (g->pits[g->nhd] - 18);
4381 				if (s->stem >= 0) {
4382 					g->y = ymn;
4383 					g->ys = ymx + slen;
4384 					ymx = (int) (g->ys + 0.5);
4385 				} else {
4386 					g->y = ymx;
4387 					g->ys = ymn - slen;
4388 					ymn = (int) (g->ys - 0.5);
4389 				}
4390 				ymx += 2;
4391 				ymn -= 2;
4392 				if (ymn < ymin)
4393 					ymin = ymn;
4394 				else if (ymx > ymax)
4395 					ymax = ymx;
4396 				g->ymx = ymx;
4397 				g->ymn = ymn;
4398 			}
4399 			s->ymx = ymax;
4400 			s->ymn = ymin;
4401 			continue;
4402 		}
4403 
4404 		/* shift notes in chords (need stem direction to do this) */
4405 		set_head_shift(s);
4406 
4407 		/* if start or end of beam, adjust the number of flags
4408 		 * with the other end */
4409 		nflags = s->nflags;
4410 		if ((s->sflags & (S_BEAM_ST | S_BEAM_END)) == S_BEAM_ST) {
4411 			if (s->sflags & S_FEATHERED_BEAM)
4412 				nflags = ++s->nflags;
4413 			for (s2 = s->next; /*s2*/; s2 = s2->next) {
4414 				if (s2->abc_type == ABC_T_NOTE) {
4415 					if (s->sflags & S_FEATHERED_BEAM)
4416 						s2->nflags++;
4417 					if (s2->sflags & S_BEAM_END)
4418 						break;
4419 				}
4420 			}
4421 /*			if (s2) */
4422 			    if (s2->nflags > nflags)
4423 				nflags = s2->nflags;
4424 		} else if ((s->sflags & (S_BEAM_ST | S_BEAM_END)) == S_BEAM_END) {
4425 			for (s2 = s->prev; /*s2*/; s2 = s2->prev) {
4426 				if (s2->sflags & S_BEAM_ST)
4427 					break;
4428 			}
4429 /*			if (s2) */
4430 			    if (s2->nflags > nflags)
4431 				nflags = s2->nflags;
4432 		}
4433 
4434 		/* set height of stem end */
4435 		mid = s->mid;
4436 		slen = cfmt.stemheight;
4437 		switch (nflags) {
4438 		case 2: slen += 2; break;
4439 		case 3:	slen += 5; break;
4440 		case 4:	slen += 10; break;
4441 		case 5:	slen += 16; break;
4442 		}
4443 		if ((scale = voice_tb[s->voice].scale) != 1)
4444 			slen *= (scale + 1) * 0.5;
4445 		ymn = 3 * (s->pits[0] - 18);
4446 		if (s->nhd > 0) {
4447 			slen -= 2;
4448 			ymx = 3 * (s->pits[s->nhd] - 18);
4449 		} else {
4450 			ymx = ymn;
4451 		}
4452 		if (s->aux != 0)
4453 			slen += 2 * s->aux;		/* tremolo */
4454 		if (s->flags & ABC_F_STEMLESS) {
4455 			if (s->stem >= 0) {
4456 				s->y = ymn;
4457 				s->ys = ymx;
4458 			} else {
4459 				s->ys = ymn;
4460 				s->y = ymx;
4461 			}
4462 			if (nflags == -4)		/* if longa */
4463 				ymn -= 6;
4464 			s->ymx = ymx + 4;
4465 			s->ymn = ymn - 4;
4466 		} else if (s->stem >= 0) {
4467 			if (nflags >= 2)
4468 				slen -= 1;
4469 			if (s->pits[s->nhd] > 26
4470 			 && (nflags <= 0
4471 			  || (s->sflags & (S_BEAM_ST | S_BEAM_END))
4472 					!= (S_BEAM_ST | S_BEAM_END))) {
4473 				slen -= 2;
4474 				if (s->pits[s->nhd] > 28)
4475 					slen -= 2;
4476 			}
4477 			s->y = ymn;
4478 			if (s->u.note.notes[0].ti1 != 0)
4479 /*fixme
4480  *			 || s->u.note.ti2[0] != 0) */
4481 				ymn -= 3;
4482 			s->ymn = ymn - 4;
4483 			s->ys = ymx + slen;
4484 			if (s->ys < mid)
4485 				s->ys = mid;
4486 			s->ymx = (int) (s->ys + 2.5);
4487 		} else {			/* stem down */
4488 			if (s->pits[0] < 18
4489 			 && (nflags <= 0
4490 			  || (s->sflags & (S_BEAM_ST | S_BEAM_END))
4491 					!= (S_BEAM_ST | S_BEAM_END))) {
4492 				slen -= 2;
4493 				if (s->pits[0] < 16)
4494 					slen -= 2;
4495 			}
4496 			s->ys = ymn - slen;
4497 			if (s->ys > mid)
4498 				s->ys = mid;
4499 			s->ymn = (int) (s->ys - 2.5);
4500 			s->y = ymx;
4501 /*fixme:the tie may be lower*/
4502 			if (s->u.note.notes[s->nhd].ti1 != 0)
4503 				ymx += 3;
4504 			s->ymx = ymx + 4;
4505 		}
4506 	}
4507 }
4508 
4509 /* -- split up unsuitable bars at end of staff -- */
check_bar(struct SYMBOL * s)4510 static void check_bar(struct SYMBOL *s)
4511 {
4512 	struct VOICE_S *p_voice;
4513 	int bar_type, i;
4514 
4515 	p_voice = &voice_tb[s->voice];
4516 
4517 	/* search the last bar */
4518 	while (s->type == CLEF || s->type == KEYSIG || s->type == TIMESIG) {
4519 		if (s->type == TIMESIG
4520 		 && s->time > p_voice->sym->time) /* if not empty voice */
4521 			insert_meter |= 1;	  /* meter in the next line */
4522 		if ((s = s->prev) == NULL)
4523 			return;
4524 	}
4525 	if (s->type != BAR)
4526 		return;
4527 
4528 	if (s->u.bar.repeat_bar) {
4529 		p_voice->bar_start = B_OBRA;
4530 		p_voice->bar_text = s->text;
4531 		p_voice->bar_gch = s->gch;
4532 		p_voice->bar_repeat = 1;
4533 		s->text = NULL;
4534 		s->gch = NULL;
4535 		s->u.bar.repeat_bar = 0;
4536 		if (s->flags & ABC_F_INVIS)
4537 			p_voice->bar_start |= 0x8000;
4538 		if (s->sflags & S_NOREPBRA)
4539 			p_voice->bar_start |= 0x4000;
4540 		if (s->flags & ABC_F_RBSTART)
4541 			p_voice->bar_start |= 0x2000;
4542 		if (s->sflags & S_RBSTART)
4543 			p_voice->bar_start |= 0x1000;
4544 	}
4545 	bar_type = s->u.bar.type;
4546 	if (bar_type == B_COL)			/* ':' */
4547 		return;
4548 	if ((bar_type & 0x0f) != B_COL)		/* if not left repeat bar */
4549 		return;
4550 	if (!(s->sflags & S_RRBAR)) {		/* 'xx:' (not ':xx:') */
4551 		if (bar_type == ((B_SINGLE << 8) | B_LREP)) {
4552 			p_voice->bar_start = B_LREP;
4553 			s->u.bar.type = B_DOUBLE;
4554 			return;
4555 		}
4556 		p_voice->bar_start = bar_type & 0x0fff;
4557 		if (s->flags & ABC_F_INVIS)
4558 			p_voice->bar_start |= 0x8000;
4559 		if (s->sflags & S_NOREPBRA)
4560 			p_voice->bar_start |= 0x4000;
4561 		if (s->prev && s->prev->type == BAR)
4562 			unlksym(s);
4563 		else
4564 			s->u.bar.type = B_BAR;
4565 		return;
4566 	}
4567 	if (bar_type == B_DREP) {		/* '::' */
4568 		s->u.bar.type = B_RREP;
4569 		p_voice->bar_start = B_LREP;
4570 		if (s->flags & ABC_F_INVIS)
4571 			p_voice->bar_start |= 0x8000;
4572 		if (s->sflags & S_NOREPBRA)
4573 			p_voice->bar_start |= 0x4000;
4574 		if (s->flags & ABC_F_RBSTART)
4575 			p_voice->bar_start |= 0x2000;
4576 		if (s->sflags & S_RBSTART)
4577 			p_voice->bar_start |= 0x1000;
4578 		return;
4579 	}
4580 	for (i = 0; bar_type != 0; i++)
4581 		bar_type >>= 4;
4582 	bar_type = s->u.bar.type;
4583 	s->u.bar.type = bar_type >> ((i / 2) * 4);
4584 	i = ((i + 1) / 2 * 4);
4585 	bar_type &= 0x0fff;
4586 	p_voice->bar_start = bar_type & ((1 << i) - 1);
4587 	if (s->flags & ABC_F_INVIS)
4588 		p_voice->bar_start |= 0x8000;
4589 	if (s->sflags & S_NOREPBRA)
4590 		p_voice->bar_start |= 0x4000;
4591 	if (s->flags & ABC_F_RBSTART)
4592 		p_voice->bar_start |= 0x2000;
4593 	if (s->sflags & S_RBSTART)
4594 		p_voice->bar_start |= 0x1000;
4595 }
4596 
4597 /* -- move the symbols of an empty staff to the next one -- */
sym_staff_move(int staff)4598 static void sym_staff_move(int staff)
4599 //			struct SYMBOL *s,
4600 //			struct SYSTEM *sy)
4601 {
4602 	struct SYMBOL *s;
4603 
4604 //	for (;;) {
4605 	for (s = tsfirst; s; s = s->ts_next) {
4606 		if (s->sflags & S_NL)
4607 			break;
4608 		if (s->staff == staff
4609 		 && s->type != CLEF) {
4610 			s->staff++;
4611 			s->flags |= ABC_F_INVIS;
4612 		}
4613 //		s = s->ts_next;
4614 //		if (s == tsnext || s->sflags & S_NEW_SY)
4615 //			break;
4616 	}
4617 }
4618 
4619 /* -- adjust the empty flag of a brace system -- */
set_brace(struct SYSTEM * sy,char * empty,char * empty_gl)4620 static void set_brace(struct SYSTEM *sy, char *empty, char *empty_gl)
4621 {
4622 	int staff, i, empty_fl;
4623 
4624 	/* if a system brace has empty and non empty staves, keep all staves */
4625 	for (staff = 0; staff <= nstaff; staff++) {
4626 		if (!(sy->staff[staff].flags & (OPEN_BRACE | OPEN_BRACE2)))
4627 			continue;
4628 		empty_fl = 0;
4629 		i = staff;
4630 		while (staff <= nstaff) {
4631 			empty_fl |= empty[staff] ? 1 : 2;
4632 			if (cursys->staff[staff].flags & (CLOSE_BRACE | CLOSE_BRACE2))
4633 				break;
4634 			staff++;
4635 		}
4636 		if (empty_fl == 3) {	/* if empty and not empty staves */
4637 			while (i <= staff) {
4638 				empty[i] = 0;
4639 				empty_gl[i++] = 0;
4640 			}
4641 		}
4642 	}
4643 }
4644 
4645 /* -- define the start and end of a piece of tune -- */
4646 /* tsnext becomes the beginning of the next line */
set_piece(void)4647 static void set_piece(void)
4648 {
4649 	struct SYSTEM *sy;
4650 	struct SYMBOL *s;
4651 	struct VOICE_S *p_voice;
4652 	struct STAFF_S *p_staff;
4653 	int staff;
4654 	char empty[MAXSTAFF], empty_gl[MAXSTAFF];
4655 
4656 	/* reset the staves */
4657 	sy = cursys;
4658 	for (staff = 0; staff <= nstaff; staff++) {
4659 		p_staff = &staff_tb[staff];
4660 		p_staff->y = 0;		/* staff system not computed */
4661 		p_staff->stafflines = sy->staff[staff].stafflines;
4662 		p_staff->staffscale = sy->staff[staff].staffscale;
4663 	}
4664 
4665 	/* search the next end of line,
4666 	 * set the repeat measures, (remove some dble bars?)
4667 	 * and flag the empty staves
4668 	 */
4669 	memset(empty, 1, sizeof empty);
4670 	memset(empty_gl, 1, sizeof empty_gl);
4671 	for (s = tsfirst; s; s = s->ts_next) {
4672 		if (s->sflags & S_NL)
4673 			break;
4674 		if (s->sflags & S_NEW_SY) {
4675 			set_brace(sy, empty, empty_gl);
4676 			for (staff = 0; staff <= nstaff; staff++) {
4677 				sy->staff[staff].empty = empty[staff];
4678 				empty[staff] = 1;
4679 			}
4680 			sy = sy->next;
4681 			for (staff = 0; staff <= sy->nstaff; staff++) {
4682 				p_staff = &staff_tb[staff];
4683 				p_staff->stafflines = sy->staff[staff].stafflines;
4684 				if (!p_staff->stafflines)
4685 					p_staff->stafflines = "|||||";
4686 				p_staff->staffscale = sy->staff[staff].staffscale;
4687 				if (p_staff->staffscale == 0)
4688 					p_staff->staffscale = 1;
4689 			}
4690 		}
4691 		if (!empty[s->staff])
4692 			continue;
4693 		switch (s->type) {
4694 		case GRACE:
4695 			empty_gl[s->staff] = empty[s->staff] = 0;
4696 			break;
4697 		case NOTEREST:
4698 		case SPACE:
4699 		case MREST:
4700 			if (cfmt.staffnonote > 1) {
4701 				empty_gl[s->staff] = empty[s->staff] = 0;
4702 			} else if (!(s->flags & ABC_F_INVIS)) {
4703 				if (s->abc_type == ABC_T_NOTE
4704 				 || cfmt.staffnonote != 0)
4705 					empty_gl[s->staff] = empty[s->staff] = 0;
4706 			}
4707 			break;
4708 		}
4709 	}
4710 	tsnext = s;
4711 
4712 	/* set the last empty staves */
4713 	set_brace(sy, empty, empty_gl);
4714 	for (staff = 0; staff <= nstaff; staff++)
4715 		sy->staff[staff].empty = empty[staff];
4716 
4717 	/* define the offsets of the measure bars */
4718 	for (staff = 0; staff <= nstaff; staff++) {
4719 		int i, l;
4720 		char *stafflines;
4721 
4722 		if (empty_gl[staff])
4723 			continue;
4724 
4725 		p_staff = &staff_tb[staff];
4726 		stafflines = p_staff->stafflines;
4727 		l = strlen(stafflines);
4728 		p_staff->topbar = 6 * (l - 1);
4729 		for (i = 0; i < l - 1; i++)
4730 			if (stafflines[i] != '.')
4731 				break;
4732 		p_staff->botbar = i * 6;
4733 		if (i >= l - 2) {		// 0, 1 or 2 lines
4734 			p_staff->botbar -= 6;
4735 			p_staff->topbar += 6;
4736 		}
4737 	}
4738 
4739 	/* move the symbols of the empty staves to the next staff */
4740 //	sy = cursys;
4741 	for (staff = 0; staff < nstaff; staff++) {
4742 #if 1
4743 		if (empty_gl[staff])
4744 			sym_staff_move(staff);
4745 #else
4746 		if (sy->staff[staff].empty)
4747 			sym_staff_move(staff, tsfirst, sy);
4748 	}
4749 	if (sy->next) {
4750 		for (s = tsfirst; s; s = s->ts_next) {
4751 			if (s->sflags & S_NL)
4752 				break;
4753 			if (s->sflags & S_NEW_SY) {
4754 				sy = sy->next;
4755 				for (staff = 0; staff < nstaff; staff++) {
4756 					if (sy->staff[staff].empty)
4757 						sym_staff_move(staff, s, sy);
4758 				}
4759 				if (!sy->next)
4760 					break;
4761 			}
4762 		}
4763 #endif
4764 	}
4765 
4766 	/* initialize the music line */
4767 	init_music_line();
4768 
4769 	/* if last music line, nothing more to do */
4770 	if (!tsnext)
4771 		return;
4772 
4773 	s = tsnext;
4774 	s->sflags &= ~S_NL;
4775 	s = s->ts_prev;
4776 	s->ts_next = NULL;
4777 
4778 	/* set the end of the voices */
4779 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
4780 		int voice;
4781 
4782 		voice = p_voice - voice_tb;
4783 		for (s = tsnext->ts_prev; s; s = s->ts_prev) {
4784 			if (s->voice == voice) {
4785 				s->next = NULL;
4786 				check_bar(s);
4787 				break;
4788 			}
4789 		}
4790 		if (!s)
4791 			p_voice->sym = NULL;
4792 	}
4793 }
4794 
4795 /* -- position the symbols along the staff -- */
set_sym_glue(float width)4796 static void set_sym_glue(float width)
4797 {
4798 	struct SYMBOL *s;
4799 	float beta0, alfa, beta;
4800 	int some_grace;
4801 	float xmin, x, xmax, spafac;
4802 
4803 	/* calculate the whole space of the symbols */
4804 	some_grace = 0;
4805 	xmin = x = xmax = 0;
4806 	s = tsfirst;
4807 	for (;;) {
4808 		if (s->type == GRACE)
4809 			some_grace = 1;
4810 		if (s->sflags & S_SEQST) {
4811 			float space;
4812 
4813 			xmin += s->shrink;
4814 			if ((space = s->space) < s->shrink)
4815 				space = s->shrink;
4816 			x += space;
4817 			if (cfmt.stretchstaff)
4818 				space *= 1.8;
4819 			xmax += space;
4820 		}
4821 		if (!s->ts_next)
4822 			break;
4823 		s = s->ts_next;
4824 	}
4825 
4826 	/* set max shrink and stretch */
4827 	if (!cfmt.continueall)
4828 		beta0 = BETA_X;
4829 	else
4830 		beta0 = BETA_C;
4831 
4832 	/* memorize the glue for the last music line */
4833 	if (tsnext) {
4834 		if (x - width >= 0) {
4835 			beta_last = 0;
4836 		} else {
4837 			beta_last = (width - x) / (xmax - x);	/* stretch */
4838 			if (beta_last > beta0) {
4839 				if (cfmt.stretchstaff) {
4840 					if (!cfmt.continueall
4841 					 && cfmt.linewarn) {
4842 						error(0, s,
4843 						      "Line underfull (%.0fpt of %.0fpt)",
4844 							beta0 * xmax + (1 - beta0) * x,
4845 							width);
4846 					}
4847 				} else {
4848 					width = x;
4849 					beta_last = 0;
4850 				}
4851 			}
4852 		}
4853 	} else {			/* if last music line */
4854 		if (x < width) {
4855 			beta = (width - x) / (xmax - x);	/* stretch */
4856 			if (beta >= beta_last) {
4857 				beta = beta_last * xmax + (1 - beta_last) * x;
4858 
4859 				/* shrink underfull last line same as previous */
4860 				if (beta < width * (1. - cfmt.stretchlast))
4861 					width = beta;
4862 			}
4863 		}
4864 	}
4865 
4866 	spafac = width / x;			/* space expansion factor */
4867 
4868 	/* define the x offsets of all starting symbols */
4869 	x = xmax = 0;
4870 	s = tsfirst;
4871 	for (;;) {
4872 		if (s->sflags & S_SEQST) {
4873 			float new_space;
4874 
4875 			new_space = s->shrink;
4876 			if (s->space != 0) {
4877 				if (new_space < s->space * spafac)
4878 					new_space = s->space * spafac;
4879 				xmax += s->space * spafac * 1.8;
4880 			}
4881 			x += new_space;
4882 			xmax += new_space;
4883 			s->x = x;
4884 			s->xmax = xmax;
4885 		}
4886 		if (!s->ts_next)
4887 			break;
4888 		s = s->ts_next;
4889 	}
4890 
4891 	/* if the last symbol is not a bar, add some extra space */
4892 	switch (s->type) {
4893 	case BAR:
4894 	case FMTCHG:
4895 		break;
4896 	case CUSTOS:
4897 		x += s->wr;
4898 		xmin += s->wr;
4899 		xmax += s->wr;
4900 		break;
4901 	default: {
4902 		float min;
4903 
4904 		min = s->wr;
4905 		while (!(s->sflags & S_SEQST)) {
4906 			s = s->ts_prev;
4907 			if (s->wr > min)
4908 				min = s->wr;
4909 		}
4910 		xmin += min + 3;
4911 		if (tsnext && tsnext->space * 0.8 > s->wr + 4) {
4912 			x += tsnext->space * 0.8 * spafac;
4913 			xmax += tsnext->space * 0.8 * spafac * 1.8;
4914 		} else {
4915 #if 1
4916 			x += min + 4;
4917 			xmax += min + 4;
4918 #else
4919 /*fixme:should calculate the space according to the last symbol duration */
4920 			x += (min + 4) * spafac;
4921 			xmax += (min + 4) * spafac * 1.8;
4922 #endif
4923 		}
4924 		break;
4925 	    }
4926 	}
4927 
4928 	/* calculate the exact glue */
4929 	if (x >= width) {
4930 		beta = 0;
4931 		if (x == xmin) {
4932 			alfa = 1;			// no extra space
4933 		} else {
4934 			alfa = (x - width) / (x - xmin);	/* shrink */
4935 			if (alfa > 1) {
4936 				error(1, s,
4937 				      "Line too much shrunk (%.0f/%0.fpt of %.0fpt)",
4938 					xmin, x, width);
4939 // uncomment for staff greater than music line
4940 //				alfa = 1;
4941 			}
4942 		}
4943 		realwidth = xmin * alfa + x * (1 - alfa);
4944 	} else {
4945 		alfa = 0;
4946 		if (xmax > x)
4947 			beta = (width - x) / (xmax - x);	/* stretch */
4948 		else
4949 			beta = 1;				/* (no note) */
4950 		if (beta > beta0) {
4951 			if (!cfmt.stretchstaff)
4952 				beta = 0;
4953 		}
4954 		realwidth = xmax * beta + x * (1 - beta);
4955 	}
4956 
4957 	/* set the final x offsets */
4958 	s = tsfirst;
4959 	if (alfa != 0) {
4960 		if (alfa < 1) {
4961 			x = xmin = 0;
4962 			for (; s; s = s->ts_next) {
4963 				if (s->sflags & S_SEQST) {
4964 					xmin += s->shrink * alfa;
4965 					x = xmin + s->x * (1 - alfa);
4966 				}
4967 				s->x = x;
4968 			}
4969 		} else {
4970 			alfa = realwidth / x;
4971 			x = 0;
4972 			for (; s; s = s->ts_next) {
4973 				if (s->sflags & S_SEQST)
4974 					x = s->x * alfa;
4975 				s->x = x;
4976 			}
4977 		}
4978 	} else {
4979 		x = 0;
4980 		for (; s; s = s->ts_next) {
4981 			if (s->sflags & S_SEQST)
4982 				x = s->xmax * beta + s->x * (1 - beta);
4983 			s->x = x;
4984 		}
4985 	}
4986 
4987 	/* set the x offsets of the grace notes */
4988 	if (some_grace) {
4989 		for (s = tsfirst; s; s = s->ts_next) {
4990 			struct SYMBOL *g;
4991 
4992 			if (s->type != GRACE)
4993 				continue;
4994 			x = s->x - s->wl + (cfmt.gracespace >> 16) * 0.1;
4995 			for (g = s->extra; g; g = g->next)
4996 				if (g->type == NOTEREST)
4997 					g->x += x;
4998 		}
4999 	}
5000 }
5001 
5002 /* -- initialize a new music line -- */
new_music_line(void)5003 static void new_music_line(void)
5004 {
5005 	struct VOICE_S *p_voice;
5006 	struct SYMBOL *s;
5007 	int voice;
5008 
5009 	/* set the first symbol of each voice */
5010 	tsfirst->ts_prev = NULL;
5011 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
5012 		p_voice->sym = NULL;		/* may have no symbol */
5013 		voice = p_voice - voice_tb;
5014 		for (s = tsfirst; s; s = s->ts_next) {
5015 			if (s->voice == voice) {
5016 				p_voice->sym = s;
5017 				s->prev = NULL;
5018 				break;
5019 			}
5020 		}
5021 	}
5022 
5023 }
5024 
5025 /* -- initialize the start of generation / new music line -- */
gen_init(void)5026 static void gen_init(void)
5027 {
5028 	struct SYMBOL *s;
5029 
5030 	for (s = tsfirst ; s; s = s->ts_next) {
5031 		if (s->extra) {
5032 			output_ps(s, 0);
5033 			if (!s->extra && s->type == FMTCHG) {
5034 				unlksym(s);
5035 				if (!tsfirst)
5036 					return;
5037 			}
5038 		}
5039 		if (s->sflags & S_NEW_SY) {
5040 			s->sflags &= ~S_NEW_SY;
5041 			cursys = cursys->next;
5042 		}
5043 		switch (s->type) {
5044 		case CLEF:
5045 		case KEYSIG:
5046 		case TIMESIG:
5047 			continue;
5048 //		default:
5049 //			break;		/* may be Q: */
5050 		}
5051 		return;
5052 	}
5053 	tsfirst = NULL;			/* no more notes */
5054 }
5055 
5056 /* -- show the errors -- */
error_show(void)5057 static void error_show(void)
5058 {
5059 	struct SYMBOL *s;
5060 
5061 	for (s = tsfirst; s; s = s->ts_next) {
5062 		if (s->flags & ABC_F_ERROR) {
5063 			putxy(s->x, staff_tb[s->staff].y + s->y);
5064 			a2b("showerror\n");
5065 		}
5066 	}
5067 }
5068 
5069 /* -- delay output until the staves are defined (by draw_systems) -- */
delayed_output(float indent)5070 static float delayed_output(float indent)
5071 {
5072 	float line_height;
5073 	char *outbuf_sav, *mbf_sav, *tmpbuf;
5074 
5075 	outbuf_sav = outbuf;
5076 	mbf_sav = mbf;
5077 	tmpbuf = malloc(outbufsz);
5078 	if (!tmpbuf) {
5079 		error(1, NULL, "Out of memory for delayed outbuf - abort");
5080 		exit(EXIT_FAILURE);
5081 	}
5082 	mbf = outbuf = tmpbuf;
5083 	*outbuf = '\0';
5084 	outft = -1;
5085 	draw_sym_near();
5086 	outbuf = outbuf_sav;
5087 	mbf = mbf_sav;
5088 	outft = -1;
5089 	line_height = draw_systems(indent);
5090 	a2b("%s", tmpbuf);
5091 	free(tmpbuf);
5092 	return line_height;
5093 }
5094 
5095 /* -- generate the music -- */
output_music(void)5096 void output_music(void)
5097 {
5098 	struct VOICE_S *p_voice;
5099 	float lwidth, indent;
5100 
5101 	/* set the staff system if any STAVES at start of the next line */
5102 	gen_init();
5103 	if (!tsfirst)
5104 		return;
5105 	check_buffer();
5106 	set_global();			/* initialize the generator */
5107 	if (first_voice->next) {	/* if many voices */
5108 //		if (cfmt.combinevoices >= 0)
5109 			combine_voices();
5110 		set_stem_dir();		/* set the stems direction in 'multi' */
5111 	}
5112 	for (p_voice = first_voice; p_voice; p_voice = p_voice->next)
5113 		set_beams(p_voice->sym);	/* decide on beams */
5114 	set_stems();			/* set the stem lengths */
5115 	if (first_voice->next) {	/* when multi-voices */
5116 		set_rest_offset();	/* set the vertical offset of rests */
5117 		set_overlap();		/* shift the notes on voice overlap */
5118 	}
5119 	set_acc_shft();			// set the horizontal offset of accidentals
5120 	set_allsymwidth(NULL);		/* set the width of all symbols */
5121 
5122 	lwidth = ((cfmt.landscape ? cfmt.pageheight : cfmt.pagewidth)
5123 		- cfmt.leftmargin - cfmt.rightmargin)
5124 			/ cfmt.scale;
5125 	if (lwidth < 50) {
5126 		error(1, 0, "Bad page width %.1f", lwidth);
5127 		lwidth = 10 CM;
5128 	}
5129 	indent = set_indent();
5130 	cut_tune(lwidth, indent);
5131 	beta_last = 0;
5132 	for (;;) {			/* loop per music line */
5133 		float line_height;
5134 
5135 		set_piece();
5136 		indent = set_indent();
5137 		set_sym_glue(lwidth - indent);
5138 		if (indent != 0)
5139 			a2b("%.2f 0 T\n", indent); /* do indentation */
5140 		line_height = delayed_output(indent);
5141 		draw_all_symb();
5142 		draw_all_deco();
5143 		if (showerror)
5144 			error_show();
5145 		bskip(line_height);
5146 		if (indent != 0) {
5147 			a2b("%.2f 0 T\n", -indent);
5148 			insert_meter &= ~2;	// no more indentation
5149 		}
5150 		tsfirst = tsnext;
5151 		gen_init();
5152 		if (!tsfirst)
5153 			break;
5154 		buffer_eob(0);
5155 		new_music_line();
5156 	}
5157 	outft = -1;
5158 }
5159 
5160 /* -- reset the generator -- */
reset_gen(void)5161 void reset_gen(void)
5162 {
5163 	if (cfmt.fields[0] & (1 << ('M' - 'A')))
5164 		insert_meter = 3;	/* insert meter and indent */
5165 	else
5166 		insert_meter = 2;	/* indent only */
5167 }
5168