Debian Codegraph @

texk/gregorio/gregorio-src/src/struct.c

raw
1 /*
2 * Gregorio is a program that translates gabc files to GregorioTeX
3 * This file implements the Gregorio data structures.
4 *
5 * Copyright (C) 2006-2025 The Gregorio Project (see CONTRIBUTORS.md)
6 *
7 * This file is part of Gregorio.
8 *
9 * Gregorio 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 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Gregorio is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Gregorio. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /**
24 * @file
25 * @brief This file contains a set of function to manipulate the gregorio
26 * structure.
27 *
28 * It starts by simple add/delete functions for almost all
29 * structs, and ends with more complex functions for manipulating
30 * horizontal episema, keys, etc.
31 *
32 * The first functions are not commented, but they always act like
33 * this : we give them a pointer to the pointer to the current element
34 * (by element I mean a struct which can be gregorio_note,
35 * gregorio_element, etc.), they add an element and the update the
36 * pointer to the element so that it points to the new element (may
37 * seem a bit strange).
38 *
39 * All the delete functions are recursive and free all memory.
40 *
41 */
42
43 #include "config.h"
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <assert.h>
49 #include "struct.h"
50 #include "unicode.h"
51 #include "messages.h"
52 #include "support.h"
53 #include "characters.h"
54 #include "support.h"
55
56 unsigned short tex_position_id = 0;
57
58 gregorio_clef_info gregorio_default_clef = {
59 /*.line =*/ 3,
60 /*.secondary_line =*/ 0,
61 /*.pitch_difference =*/ 0,
62 /*.clef =*/ CLEF_C,
63 /*.flatted =*/ false,
64 /*.secondary_clef =*/ CLEF_C, /* not used since secondary_line is 0 */
65 /*.secondary_flatted =*/ false,
66 };
67
68 static size_t hepisema_adjustments_capacity = 0;
69 static unsigned short hepisema_adjustments_last = 0;
70 static gregorio_hepisema_adjustment *hepisema_adjustments = NULL;
71
72 static size_t texverbs_capacity = 0;
73 static unsigned short texverbs_last = 0;
74 static char **texverbs = NULL;
75
76 void gregorio_struct_init(void)
77 {
78 hepisema_adjustments_capacity = 8;
79 hepisema_adjustments = gregorio_grow_buffer(NULL,
80 &hepisema_adjustments_capacity, gregorio_hepisema_adjustment);
81 hepisema_adjustments[0].vbasepos = HVB_AUTO;
82 hepisema_adjustments[0].nudge = NULL;
83
84 texverbs_capacity = 32;
85 texverbs = gregorio_grow_buffer(NULL, &texverbs_capacity, char *);
86 texverbs[0] = NULL;
87 }
88
89 void gregorio_struct_destroy(void)
90 {
91 size_t i;
92 gregorio_hepisema_adjustment *adj;
93 char **texverb;
94
95 for (i = 0, adj = hepisema_adjustments; i <= hepisema_adjustments_last;
96 ++i, ++adj) {
97 if (adj->nudge) {
98 free(adj->nudge);
99 }
100 }
101 free(hepisema_adjustments);
102
103 for (i = 0, texverb = texverbs; i <= texverbs_last; ++i, ++texverb) {
104 if (*texverb) {
105 /* If the code falls here, this is a programming error. */
106 /* LCOV_EXCL_START */
107 gregorio_fail(gregorio_struct_destroy, "unfreed texverb");
108 free(*texverb);
109 }
110 /* LCOV_EXCL_STOP */
111 }
112 free(texverbs);
113 }
114
115 static unsigned short register_texverb(char *const texverb)
116 {
117 if (texverbs_last == USHRT_MAX) {
118 /* It's not reasonable to trigger this condition while testing */
119 /* LCOV_EXCL_START */
120 gregorio_message(_("too many texverbs"), "register_texverb",
121 VERBOSITY_ERROR, 0);
122 return 0;
123 /* LCOV_EXCL_STOP */
124 }
125 ++texverbs_last;
126 if (texverbs_last >= texverbs_capacity) {
127 texverbs = gregorio_grow_buffer(texverbs, &texverbs_capacity, char *);
128 }
129 texverbs[texverbs_last] = texverb;
130 return texverbs_last;
131 }
132
133 static void free_one_texverb(unsigned short index)
134 {
135 if (index) {
136 gregorio_assert(index <= texverbs_last, gregorio_texverb,
137 "array index out of bounds", return);
138 free(texverbs[index]);
139 texverbs[index] = NULL;
140 }
141 }
142
143 static gregorio_note *create_and_link_note(gregorio_note **current_note,
144 const gregorio_scanner_location *const loc)
145 {
146 gregorio_note *note = gregorio_calloc(1, sizeof(gregorio_note));
147 note->previous = *current_note;
148 note->next = NULL;
149 if (*current_note) {
150 (*current_note)->next = note;
151 }
152 *current_note = note;
153 note->src_line = loc->first_line;
154 note->src_column = loc->first_column;
155 note->src_offset = loc->first_offset;
156
157 return note;
158 }
159
160 void gregorio_position_h_episema_above(gregorio_note *note, signed char height,
161 bool connect)
162 {
163 assert(note && (note->type == GRE_NOTE || note->type == GRE_BAR));
164 note->h_episema_above = height;
165 note->h_episema_above_connect = connect;
166 }
167
168 static void set_h_episema_above(gregorio_note *note, signed char height,
169 grehepisema_size size, bool connect)
170 {
171 gregorio_position_h_episema_above(note, height, connect);
172 note->h_episema_above_size = size;
173 }
174
175 void gregorio_position_h_episema_below(gregorio_note *note, signed char height,
176 bool connect)
177 {
178 assert(note && (note->type == GRE_NOTE || note->type == GRE_BAR));
179 note->h_episema_below = height;
180 note->h_episema_below_connect = connect;
181 }
182
183 static void set_h_episema_below(gregorio_note *note, signed char height,
184 grehepisema_size size, bool connect)
185 {
186 gregorio_position_h_episema_below(note, height, connect);
187 note->h_episema_below_size = size;
188 }
189
190 void gregorio_add_note(gregorio_note **current_note, signed char pitch,
191 gregorio_shape shape, gregorio_sign signs,
192 gregorio_liquescentia liquescentia, gregorio_note *prototype,
193 const gregorio_scanner_location *const loc)
194 {
195 gregorio_note *element = create_and_link_note(current_note, loc);
196 element->type = GRE_NOTE;
197 element->u.note.pitch = pitch;
198 element->u.note.shape = shape;
199 element->signs = signs;
200 element->special_sign = _NO_SIGN;
201 element->u.note.liquescentia = liquescentia;
202 if (prototype) {
203 set_h_episema_above(element, prototype->h_episema_above,
204 prototype->h_episema_above_size,
205 prototype->h_episema_above_connect);
206 set_h_episema_below(element, prototype->h_episema_below,
207 prototype->h_episema_below_size,
208 prototype->h_episema_below_connect);
209 element->he_adjustment_index[SO_OVER] =
210 prototype->he_adjustment_index[SO_OVER];
211 element->he_adjustment_index[SO_UNDER] =
212 prototype->he_adjustment_index[SO_UNDER];
213 element->high_ledger_line = prototype->high_ledger_line;
214 element->high_ledger_specificity = prototype->high_ledger_specificity;
215 element->low_ledger_line = prototype->low_ledger_line;
216 element->low_ledger_specificity = prototype->low_ledger_specificity;
217 }
218 element->texverb = 0;
219 element->choral_sign = NULL;
220 }
221
222 static void add_pitched_item_as_note(gregorio_note **current_note,
223 gregorio_type type, signed char pitch,
224 const gregorio_scanner_location *const loc)
225 {
226 gregorio_note *element = create_and_link_note(current_note, loc);
227 element->type = type;
228 element->u.note.pitch = pitch;
229 }
230
231 void gregorio_add_end_of_line_as_note(gregorio_note **current_note,
232 bool eol_ragged, bool eol_forces_custos, bool eol_forces_custos_on,
233 const gregorio_scanner_location *const loc)
234 {
235 gregorio_note *element = create_and_link_note(current_note, loc);
236 element->type = GRE_END_OF_LINE;
237 element->u.other.eol_ragged = eol_ragged;
238 element->u.other.eol_forces_custos = eol_forces_custos;
239 element->u.other.eol_forces_custos_on = eol_forces_custos_on;
240 }
241
242 void gregorio_add_custos_as_note(gregorio_note **current_note,
243 const gregorio_scanner_location *const loc)
244 {
245 gregorio_note *element = create_and_link_note(current_note, loc);
246 element->type = GRE_CUSTOS;
247 }
248
249 void gregorio_add_manual_custos_as_note(gregorio_note **current_note,
250 signed char pitch, const gregorio_scanner_location *const loc)
251 {
252 add_pitched_item_as_note(current_note, GRE_MANUAL_CUSTOS, pitch, loc);
253 }
254
255 void gregorio_add_suppress_custos_as_note(gregorio_note **current_note,
256 const gregorio_scanner_location *const loc)
257 {
258 gregorio_note *element = create_and_link_note(current_note, loc);
259 element->type = GRE_SUPPRESS_CUSTOS;
260 }
261
262 void gregorio_add_clef_as_note(gregorio_note **current_note,
263 gregorio_clef clef, signed char clef_line, bool flatted,
264 const gregorio_scanner_location *const loc)
265 {
266 gregorio_note *element = create_and_link_note(current_note, loc);
267 element->type = GRE_CLEF;
268 element->u.clef.clef = clef;
269 element->u.clef.line = clef_line;
270 element->u.clef.flatted = flatted;
271 }
272
273 void gregorio_add_secondary_clef_to_note(gregorio_note *current_note,
274 gregorio_clef clef, signed char clef_line, bool flatted)
275 {
276 gregorio_not_null(current_note, gregorio_add_secondary_clef_to_note,
277 return);
278 if (current_note->type != GRE_CLEF) {
279 gregorio_message(_("trying to add a secondary clef to something that "
280 "is not a clef"), "gregorio_add_secondary_clef_to_note",
281 VERBOSITY_ERROR, 0);
282 return;
283 }
284
285 if (current_note->u.clef.secondary_line) {
286 gregorio_message(_("secondary clef already exists"),
287 "gregorio_add_secondary_clef_to_note", VERBOSITY_ERROR, 0);
288 return;
289 }
290
291 current_note->u.clef.secondary_clef = clef;
292 current_note->u.clef.secondary_line = clef_line;
293 current_note->u.clef.secondary_flatted = flatted;
294 }
295
296 void gregorio_add_bar_as_note(gregorio_note **current_note, gregorio_bar bar,
297 const gregorio_scanner_location *const loc)
298 {
299 gregorio_note *element = create_and_link_note(current_note, loc);
300 element->type = GRE_BAR;
301 element->u.other.bar = bar;
302 }
303
304 void gregorio_add_space_as_note(gregorio_note **current_note,
305 const gregorio_space space, char *factor,
306 const gregorio_scanner_location *const loc)
307 {
308 gregorio_note *element = create_and_link_note(current_note, loc);
309 element->type = GRE_SPACE;
310 element->u.other.space = space;
311 element->u.other.ad_hoc_space_factor = factor;
312 }
313
314 unsigned short gregorio_add_texverb_as_note(gregorio_note **current_note,
315 char *str, gregorio_type type,
316 const gregorio_scanner_location *const loc)
317 {
318 gregorio_note *element;
319 /* gregorio_not_null(str, gregorio_add_texverb_as_note, return NULL); */
320 element = create_and_link_note(current_note, loc);
321 assert(type == GRE_TEXVERB_GLYPH || type == GRE_TEXVERB_ELEMENT
322 || type == GRE_ALT);
323 element->type = type;
324 element->texverb = register_texverb(str);
325 return element->texverb;
326 }
327
328 void gregorio_add_nlba_as_note(gregorio_note **current_note, gregorio_nlba type,
329 const gregorio_scanner_location *const loc)
330 {
331 gregorio_note *element = create_and_link_note(current_note, loc);
332 element->type = GRE_NLBA;
333 element->u.other.nlba = type;
334 }
335
336 void gregorio_start_autofuse(gregorio_note **current_note,
337 const gregorio_scanner_location *const loc)
338 {
339 gregorio_note *element = create_and_link_note(current_note, loc);
340 element->type = GRE_AUTOFUSE_START;
341 }
342
343 void gregorio_end_autofuse(gregorio_note **current_note,
344 const gregorio_scanner_location *const loc)
345 {
346 gregorio_note *element = create_and_link_note(current_note, loc);
347 element->type = GRE_AUTOFUSE_END;
348 }
349
350 static __inline void change_note_texverb(gregorio_note *note, char *str)
351 {
352 if (note->texverb) {
353 gregorio_change_texverb(note->texverb, str);
354 } else {
355 note->texverb = register_texverb(str);
356 }
357 }
358
359 void gregorio_add_texverb_to_note(gregorio_note *const current_note, char *str)
360 {
361 size_t len;
362 char *res;
363 gregorio_not_null(str, gregorio_add_texverb_as_note, return);
364 if (current_note) {
365 const char *texverb = gregorio_texverb(current_note->texverb);
366 if (texverb) {
367 len = strlen(texverb) + strlen(str) + 1;
368 res = gregorio_malloc(len);
369 strcpy(res, texverb);
370 strcat(res, str);
371 free(str);
372 change_note_texverb(current_note, res);
373 } else {
374 change_note_texverb(current_note, str);
375 }
376 }
377 }
378
379 void gregorio_add_cs_to_note(gregorio_note *const current_note,
380 char *const str, const bool nabc)
381 {
382 if (current_note) {
383 if (current_note->choral_sign) {
384 free(current_note->choral_sign);
385 }
386 current_note->choral_sign = str;
387 current_note->choral_sign_is_nabc = nabc;
388 }
389 }
390
391 void gregorio_add_shape_hint_to_note(gregorio_note *const current_note,
392 char *const str)
393 {
394 if (current_note) {
395 if (current_note->shape_hint) {
396 free(current_note->shape_hint);
397 }
398 current_note->shape_hint = str;
399 }
400 }
401
402 void gregorio_add_special_sign(gregorio_note *note, gregorio_sign sign)
403 {
404 if (!note || note->type != GRE_NOTE) {
405 gregorio_message(_("trying to add a special sign to something that is "
406 "not a note"), "gregorio_add_special_sign", VERBOSITY_ERROR,
407 0);
408 return;
409 }
410 note->special_sign = sign;
411 }
412
413 static void fix_oriscus_liquescentia(gregorio_note *const note,
414 const bool legacy_oriscus_orientation)
415 {
416 if (legacy_oriscus_orientation) {
417 switch (note->u.note.liquescentia) {
418 case L_AUCTUS_ASCENDENS:
419 note->u.note.liquescentia =
420 (note->u.note.liquescentia & ~TAIL_LIQUESCENTIA_MASK)
421 | L_AUCTUS_DESCENDENS;
422 /* fall through */
423 case L_AUCTUS_DESCENDENS:
424 note->u.note.shape = S_ORISCUS_DESCENDENS;
425 break;
426 case L_DEMINUTUS:
427 note->u.note.shape = S_ORISCUS_DEMINUTUS;
428 break;
429 default:
430 note->u.note.shape = S_ORISCUS_ASCENDENS;
431 break;
432 }
433 } else {
434 note->u.note.liquescentia &= ((~TAIL_LIQUESCENTIA_MASK) | L_DEMINUTUS);
435 if (note->u.note.liquescentia & L_DEMINUTUS) {
436 note->u.note.shape = S_ORISCUS_DEMINUTUS;
437 }
438 }
439 }
440
441 static void fix_oriscus_scapus_liquescentia(gregorio_note *const note,
442 const bool legacy_oriscus_orientation)
443 {
444 if (legacy_oriscus_orientation) {
445 switch (note->u.note.liquescentia) {
446 case L_AUCTUS_ASCENDENS:
447 note->u.note.liquescentia =
448 (note->u.note.liquescentia & ~TAIL_LIQUESCENTIA_MASK)
449 | L_AUCTUS_DESCENDENS;
450 /* fall through */
451 case L_AUCTUS_DESCENDENS:
452 note->u.note.shape = S_ORISCUS_SCAPUS_DESCENDENS;
453 break;
454 default:
455 note->u.note.shape = S_ORISCUS_SCAPUS_ASCENDENS;
456 break;
457 }
458 } else {
459 note->u.note.liquescentia &= ~TAIL_LIQUESCENTIA_MASK;
460 }
461 }
462
463 void gregorio_change_shape(gregorio_note *const note,
464 const gregorio_shape shape, const bool legacy_oriscus_orientation)
465 {
466 if (!note || note->type != GRE_NOTE) {
467 gregorio_message(_("trying to change the shape of something that is "
468 "not a note"), "change_shape", VERBOSITY_ERROR, 0);
469 return;
470 }
471
472 note->u.note.shape = shape;
473 switch (shape) {
474 case S_STROPHA:
475 case S_DISTROPHA:
476 case S_TRISTROPHA:
477 note->u.note.liquescentia &= L_AUCTUS_ASCENDENS | L_INITIO_DEBILIS;
478 break;
479
480 case S_ORISCUS_UNDETERMINED:
481 case S_ORISCUS_ASCENDENS:
482 case S_ORISCUS_DESCENDENS:
483 fix_oriscus_liquescentia(note, legacy_oriscus_orientation);
484 break;
485
486 case S_ORISCUS_SCAPUS_UNDETERMINED:
487 case S_ORISCUS_SCAPUS_ASCENDENS:
488 case S_ORISCUS_SCAPUS_DESCENDENS:
489 fix_oriscus_scapus_liquescentia(note, legacy_oriscus_orientation);
490 break;
491
492 default:
493 break;
494 }
495 }
496
497 void gregorio_add_cavum(gregorio_note *const note)
498 {
499 if (!note || note->type != GRE_NOTE) {
500 gregorio_message(_("trying to add cavum to something that is "
501 "not a note"), "change_shape", VERBOSITY_ERROR, 0);
502 return;
503 }
504 note->u.note.is_cavum = true;
505 }
506
507 void gregorio_add_tail_liquescentia(gregorio_note *note,
508 gregorio_liquescentia liq, const bool legacy_oriscus_orientation)
509 {
510 if (!note || note->type != GRE_NOTE) {
511 gregorio_message(_("trying to make a liquescence on something that "
512 "is not a note"), "add_liquescentia", VERBOSITY_ERROR, 0);
513 return;
514 }
515
516 note->u.note.liquescentia =
517 (note->u.note.liquescentia & ~TAIL_LIQUESCENTIA_MASK)
518 | (liq & TAIL_LIQUESCENTIA_MASK);
519
520 switch (note->u.note.shape) {
521 case S_STROPHA:
522 case S_DISTROPHA:
523 case S_TRISTROPHA:
524 if (note->u.note.liquescentia & L_AUCTUS_DESCENDENS) {
525 note->u.note.liquescentia =
526 (note->u.note.liquescentia & ~TAIL_LIQUESCENTIA_MASK)
527 | L_AUCTUS_ASCENDENS;
528 }
529 break;
530
531 case S_ORISCUS_UNDETERMINED:
532 case S_ORISCUS_ASCENDENS:
533 case S_ORISCUS_DESCENDENS:
534 case S_ORISCUS_DEMINUTUS:
535 fix_oriscus_liquescentia(note, legacy_oriscus_orientation);
536 break;
537
538 case S_ORISCUS_SCAPUS_UNDETERMINED:
539 case S_ORISCUS_SCAPUS_ASCENDENS:
540 case S_ORISCUS_SCAPUS_DESCENDENS:
541 fix_oriscus_scapus_liquescentia(note, legacy_oriscus_orientation);
542 break;
543
544 default:
545 break;
546 }
547 }
548
549 static void apply_auto_h_episema(gregorio_note *const note,
550 const grehepisema_size size, const bool disable_bridge)
551 {
552 if (note->h_episema_above == HEPISEMA_NONE
553 && note->h_episema_below == HEPISEMA_NONE) {
554 /* if both are unset, set both to auto */
555 set_h_episema_above(note, HEPISEMA_AUTO, size, !disable_bridge);
556 set_h_episema_below(note, HEPISEMA_AUTO, size, !disable_bridge);
557 } else if (note->h_episema_above == HEPISEMA_AUTO
558 && note->h_episema_below == HEPISEMA_AUTO) {
559 /* if both are auto, then force both */
560 /* the upper episema keeps its settings */
561 note->h_episema_above = HEPISEMA_FORCED;
562
563 set_h_episema_below(note, HEPISEMA_FORCED, size, !disable_bridge);
564 } else {
565 /* force whichever is not already forced */
566 if (note->h_episema_above != HEPISEMA_FORCED) {
567 set_h_episema_above(note, HEPISEMA_FORCED, size, !disable_bridge);
568 }
569 if (note->h_episema_below != HEPISEMA_FORCED) {
570 set_h_episema_below(note, HEPISEMA_FORCED, size, !disable_bridge);
571 }
572 }
573 }
574
575 /**********************************
576 *
577 * Activate_isolated_h_episema is used when we see an "isolated"
578 * horizontal episema: when we type ab__ lex see a then b then _ then _, so
579 * we must put the _ on the a (kind of backward process), and say the
580 * the episema on the b is a multi episema. Here n is the length of
581 * the isolated episema we found (can be up to 4).
582 *
583 *********************************/
584 static void gregorio_activate_isolated_h_episema(gregorio_note *note,
585 const grehepisema_size size, const bool disable_bridge, int n)
586 {
587 gregorio_not_null(note, gregorio_activate_isolated_h_episema, return);
588 if (note->type != GRE_NOTE) {
589 gregorio_message(ngt_("isolated horizontal episema after something "
590 "that is not a note, ignored",
591 "isolated horizontal episema after something that is not "
592 "a note, ignored", n), "activate_h_isolated_episema",
593 VERBOSITY_ERROR, 0);
594 return;
595 }
596 for (; n > 0; --n) {
597 note = note->previous;
598 if (!note || note->type != GRE_NOTE) {
599 gregorio_message(_("found more horizontal episema than notes "
600 "able to be under"), "activate_h_isolated_episema",
601 VERBOSITY_ERROR, 0);
602 return;
603 }
604 }
605 apply_auto_h_episema(note, size, disable_bridge);
606 }
607
608 void gregorio_add_h_episema(gregorio_note *note,
609 grehepisema_size size, gregorio_vposition vposition,
610 bool disable_bridge, unsigned int *nbof_isolated_episema)
611 {
612 if (!note || (note->type != GRE_NOTE && note->type != GRE_BAR)) {
613 gregorio_message(_("trying to add a horizontal episema on something "
614 "that is not a note"), "add_h_episema",
615 VERBOSITY_ERROR, 0);
616 return;
617 }
618 gregorio_not_null(nbof_isolated_episema, gregorio_add_h_episema, return);
619 if (vposition && *nbof_isolated_episema) {
620 gregorio_message(_("trying to add a forced horizontal episema on a "
621 "note which already has an automatic horizontal "
622 "episema"), "add_h_episema", VERBOSITY_ERROR, 0);
623 return;
624 }
625
626 if (vposition || !*nbof_isolated_episema) {
627 switch (vposition) {
628 case VPOS_ABOVE:
629 set_h_episema_above(note, HEPISEMA_FORCED, size, !disable_bridge);
630 break;
631
632 case VPOS_BELOW:
633 set_h_episema_below(note, HEPISEMA_FORCED, size, !disable_bridge);
634 break;
635
636 default: /* VPOS_AUTO */
637 apply_auto_h_episema(note, size, disable_bridge);
638 *nbof_isolated_episema = 1;
639 break;
640 }
641 } else {
642 gregorio_activate_isolated_h_episema(note, size, disable_bridge,
643 (*nbof_isolated_episema)++);
644 }
645 }
646
647 void gregorio_add_sign(gregorio_note *note, gregorio_sign sign,
648 gregorio_vposition vposition)
649 {
650 if (!note) {
651 gregorio_message(_("trying to add a sign to something that is "
652 "not a note"), "gregorio_add_sign", VERBOSITY_ERROR, 0);
653 return;
654 }
655 switch (sign) {
656 case _PUNCTUM_MORA:
657 switch (note->signs) {
658 case _NO_SIGN:
659 note->signs = _PUNCTUM_MORA;
660 break;
661 case _V_EPISEMA:
662 note->signs = _V_EPISEMA_PUNCTUM_MORA;
663 break;
664 case _PUNCTUM_MORA:
665 note->signs = _AUCTUM_DUPLEX;
666 break;
667 case _V_EPISEMA_PUNCTUM_MORA:
668 note->signs = _V_EPISEMA_AUCTUM_DUPLEX;
669 break;
670 default:
671 break;
672 }
673
674 note->mora_vposition = vposition;
675 break;
676
677 case _V_EPISEMA:
678 switch (note->signs) {
679 case _NO_SIGN:
680 note->signs = _V_EPISEMA;
681 break;
682 case _PUNCTUM_MORA:
683 note->signs = _V_EPISEMA_PUNCTUM_MORA;
684 break;
685 case _AUCTUM_DUPLEX:
686 note->signs = _V_EPISEMA_AUCTUM_DUPLEX;
687 break;
688 default:
689 break;
690 }
691
692 if (note->type == GRE_NOTE && vposition) {
693 note->v_episema_height = note->u.note.pitch + vposition;
694 }
695 break;
696
697 default:
698 /* not reachable unless there's a programming error */
699 /* LCOV_EXCL_START */
700 gregorio_fail2(gregorio_add_sign, "unexpected sign to add: %s",
701 gregorio_sign_to_string(sign));
702 break;
703 /* LCOV_EXCL_STOP */
704 }
705 }
706
707 void gregorio_go_to_first_note(gregorio_note **note)
708 {
709 gregorio_note *tmp;
710 gregorio_not_null_ptr(note, gregorio_go_to_first_note, return);
711 tmp = *note;
712 while (tmp->previous) {
713 tmp = tmp->previous;
714 }
715 *note = tmp;
716 }
717
718 static __inline void free_one_note(gregorio_note *note)
719 {
720 if (note->type == GRE_SPACE) {
721 free(note->u.other.ad_hoc_space_factor);
722 }
723 free_one_texverb(note->texverb);
724 free(note->choral_sign);
725 free(note->shape_hint);
726 free(note);
727 }
728
729 void gregorio_free_one_note(gregorio_note **note)
730 {
731 gregorio_note *next = NULL;
732 gregorio_not_null_ptr(note, gregorio_free_one_note, return);
733 if ((*note)->next) {
734 (*note)->next->previous = (*note)->previous;
735 next = (*note)->next;
736 }
737 if ((*note)->previous) {
738 (*note)->previous->next = (*note)->next;
739 }
740 free_one_note(*note);
741 *note = next;
742 }
743
744 static void gregorio_free_notes(gregorio_note **note)
745 {
746 gregorio_note *tmp;
747 while (*note) {
748 tmp = (*note)->next;
749 free_one_note(*note);
750 *note = tmp;
751 }
752 }
753
754 static gregorio_glyph *create_and_link_glyph(gregorio_glyph **current_glyph)
755 {
756 gregorio_glyph *glyph = gregorio_calloc(1, sizeof(gregorio_glyph));
757 glyph->previous = *current_glyph;
758 glyph->next = NULL;
759 if (*current_glyph) {
760 (*current_glyph)->next = glyph;
761 }
762 *current_glyph = glyph;
763
764 return glyph;
765 }
766
767 void gregorio_add_glyph(gregorio_glyph **current_glyph,
768 gregorio_glyph_type type, gregorio_note *first_note,
769 gregorio_liquescentia liquescentia, bool is_cavum)
770 {
771 gregorio_glyph *next_glyph = create_and_link_glyph(current_glyph);
772 next_glyph->type = GRE_GLYPH;
773 next_glyph->u.notes.glyph_type = type;
774 next_glyph->u.notes.liquescentia = liquescentia;
775 next_glyph->u.notes.is_cavum = is_cavum;
776 next_glyph->u.notes.first_note = first_note;
777 }
778
779 void gregorio_add_clef_as_glyph(gregorio_glyph **current_glyph,
780 gregorio_clef_info clef, unsigned short texverb)
781 {
782 gregorio_glyph *next_glyph = create_and_link_glyph(current_glyph);
783 next_glyph->type = GRE_CLEF;
784 next_glyph->u.misc.clef = clef;
785 next_glyph->texverb = texverb;
786 }
787
788 void gregorio_add_pitched_element_as_glyph(gregorio_glyph **current_glyph,
789 gregorio_type type, signed char pitch, bool force_pitch,
790 unsigned short texverb)
791 {
792 gregorio_glyph *next_glyph = create_and_link_glyph(current_glyph);
793 assert(type == GRE_CUSTOS);
794 next_glyph->type = type;
795 next_glyph->u.misc.pitched.pitch = pitch;
796 next_glyph->u.misc.pitched.force_pitch = force_pitch;
797 next_glyph->texverb = texverb;
798 }
799
800 void gregorio_add_unpitched_element_as_glyph(gregorio_glyph **current_glyph,
801 gregorio_type type, gregorio_extra_info *info, gregorio_sign sign,
802 unsigned short texverb)
803 {
804 gregorio_glyph *next_glyph = create_and_link_glyph(current_glyph);
805 assert(type != GRE_NOTE && type != GRE_GLYPH && type != GRE_ELEMENT
806 && type != GRE_CLEF && type != GRE_CUSTOS);
807 next_glyph->type = type;
808 next_glyph->u.misc.unpitched.info = *info;
809 next_glyph->u.misc.unpitched.special_sign = sign;
810 next_glyph->texverb = texverb;
811
812 /* this was copied into the glyph, so we need to clear it to avoid a
813 * double-free */
814 info->ad_hoc_space_factor = NULL;
815 }
816
817 void gregorio_go_to_first_glyph(gregorio_glyph **glyph)
818 {
819 gregorio_glyph *tmp;
820 gregorio_not_null_ptr(glyph, gregorio_go_to_first_glyph, return);
821 tmp = *glyph;
822 while (tmp->previous) {
823 tmp = tmp->previous;
824 }
825 *glyph = tmp;
826 }
827
828 static __inline void free_one_glyph(gregorio_glyph *glyph)
829 {
830 free_one_texverb(glyph->texverb);
831 switch (glyph->type) {
832 case GRE_GLYPH:
833 gregorio_free_notes(&glyph->u.notes.first_note);
834 break;
835 case GRE_SPACE:
836 free(glyph->u.misc.unpitched.info.ad_hoc_space_factor);
837 break;
838 default:
839 /* nothing to do */
840 break;
841 }
842 free(glyph);
843 }
844
845 void gregorio_free_one_glyph(gregorio_glyph **glyph)
846 {
847 gregorio_glyph *next = NULL;
848 gregorio_not_null_ptr(glyph, gregorio_free_one_glyph, return);
849 if ((*glyph)->next) {
850 (*glyph)->next->previous = NULL;
851 next = (*glyph)->next;
852 }
853 if ((*glyph)->previous) {
854 (*glyph)->previous->next = NULL;
855 }
856 free_one_glyph(*glyph);
857 *glyph = next;
858 }
859
860 static void gregorio_free_glyphs(gregorio_glyph **glyph)
861 {
862 gregorio_glyph *next_glyph;
863 gregorio_not_null(glyph, gregorio_free_glyphs, return);
864 while (*glyph) {
865 next_glyph = (*glyph)->next;
866 free_one_glyph(*glyph);
867 *glyph = next_glyph;
868 }
869 }
870
871 static gregorio_element *create_and_link_element(gregorio_element
872 **current_element)
873 {
874 gregorio_element *element = gregorio_calloc(1, sizeof(gregorio_element));
875 element->previous = *current_element;
876 element->next = NULL;
877 if (*current_element) {
878 (*current_element)->next = element;
879 }
880 *current_element = element;
881
882 return element;
883 }
884
885 void gregorio_add_element(gregorio_element **current_element,
886 gregorio_glyph *first_glyph)
887 {
888 gregorio_element *next = create_and_link_element(current_element);
889 next->type = GRE_ELEMENT;
890 next->u.first_glyph = first_glyph;
891 }
892
893 void gregorio_add_misc_element(gregorio_element **current_element,
894 gregorio_type type, gregorio_misc_element_info *info,
895 unsigned short texverb)
896 {
897 gregorio_element *special = create_and_link_element(current_element);
898 special->type = type;
899 special->u.misc = *info;
900 special->texverb = texverb;
901
902 if (type == GRE_SPACE) {
903 /* this was copied into the glyph, so we need to clear it to avoid a
904 * double-free */
905 info->unpitched.info.ad_hoc_space_factor = NULL;
906 }
907 }
908
909 static __inline void free_one_element(gregorio_element *element)
910 {
911 size_t i;
912 free_one_texverb(element->texverb);
913 for (i = 0; i < element->nabc_lines; i++) {
914 free(element->nabc[i]);
915 }
916 free(element->nabc);
917 switch (element->type) {
918 case GRE_ELEMENT:
919 gregorio_free_glyphs(&element->u.first_glyph);
920 break;
921 case GRE_SPACE:
922 free(element->u.misc.unpitched.info.ad_hoc_space_factor);
923 break;
924 default:
925 /* nothing to do */
926 break;
927 }
928 free(element);
929 }
930
931 void gregorio_free_one_element(gregorio_element **element)
932 {
933 gregorio_element *next = NULL;
934 gregorio_element *to_free;
935 gregorio_not_null_ptr(element, gregorio_free_one_element, return);
936 to_free = *element;
937 if (to_free->next) {
938 to_free->next->previous = NULL;
939 next = to_free->next;
940 }
941 if (to_free->previous) {
942 to_free->previous->next = NULL;
943 }
944 /* The previous line is probably optimized out */
945 /* LCOV_EXCL_STOP */
946 free_one_element(to_free);
947 *element = next;
948 }
949
950 static void gregorio_free_elements(gregorio_element **element)
951 {
952 gregorio_element *next;
953 if (!element || !*element) {
954 return;
955 }
956 while (*element) {
957 next = (*element)->next;
958 free_one_element(*element);
959 *element = next;
960 }
961 }
962
963 void gregorio_add_character(gregorio_character **current_character,
964 grewchar wcharacter)
965 {
966 gregorio_character *element =
967 (gregorio_character *) gregorio_calloc(1, sizeof(gregorio_character));
968 element->is_character = 1;
969 element->cos.character = wcharacter;
970 element->next_character = NULL;
971 element->previous_character = *current_character;
972 if (*current_character) {
973 (*current_character)->next_character = element;
974 }
975 *current_character = element;
976 }
977
978 static void gregorio_free_one_character(gregorio_character *current_character)
979 {
980 free(current_character);
981 }
982
983 void gregorio_free_characters(gregorio_character *current_character)
984 {
985 gregorio_character *next_character;
986 gregorio_not_null(current_character, gregorio_free_characters, return);
987 while (current_character) {
988 next_character = current_character->next_character;
989 gregorio_free_one_character(current_character);
990 current_character = next_character;
991 }
992 }
993
994 void gregorio_go_to_first_character(const gregorio_character **character)
995 {
996 const gregorio_character *tmp;
997 gregorio_not_null_ptr(character, gregorio_free_characters, return);
998 tmp = *character;
999 while (tmp->previous_character) {
1000 tmp = tmp->previous_character;
1001 }
1002 *character = tmp;
1003 }
1004
1005 void gregorio_begin_style(gregorio_character **current_character,
1006 grestyle_style style)
1007 {
1008 gregorio_character *element =
1009 (gregorio_character *) gregorio_calloc(1, sizeof(gregorio_character));
1010 element->is_character = 0;
1011 element->cos.s.type = ST_T_BEGIN;
1012 element->cos.s.style = style;
1013 element->previous_character = *current_character;
1014 element->next_character = NULL;
1015 if (*current_character) {
1016 (*current_character)->next_character = element;
1017 }
1018 *current_character = element;
1019 }
1020
1021 void gregorio_end_style(gregorio_character **current_character,
1022 grestyle_style style)
1023 {
1024 gregorio_character *element =
1025 (gregorio_character *) gregorio_calloc(1, sizeof(gregorio_character));
1026 element->is_character = 0;
1027 element->cos.s.type = ST_T_END;
1028 element->cos.s.style = style;
1029 element->next_character = NULL;
1030 element->previous_character = *current_character;
1031 if (*current_character) {
1032 (*current_character)->next_character = element;
1033 }
1034 *current_character = element;
1035 }
1036
1037 gregorio_character *gregorio_clone_characters(
1038 const gregorio_character *source)
1039 {
1040 gregorio_character *target = NULL, *current = NULL;
1041
1042 for (; source; source = source->next_character) {
1043 gregorio_character *character = (gregorio_character *)
1044 gregorio_calloc(1, sizeof(gregorio_character));
1045 *character = *source;
1046 character->next_character = NULL;
1047
1048 if (current) {
1049 character->previous_character = current;
1050 current = current->next_character = character;
1051 } else {
1052 character->previous_character = NULL;
1053 target = current = character;
1054 }
1055 }
1056
1057 return target;
1058 }
1059
1060 void gregorio_add_syllable(gregorio_syllable **current_syllable,
1061 int number_of_voices, gregorio_element *elements[],
1062 gregorio_character *first_character,
1063 gregorio_character *first_translation_character,
1064 gregorio_word_position position, char *abovelinestext,
1065 gregorio_tr_centering translation_type, gregorio_nlba no_linebreak_area,
1066 gregorio_euouae euouae, const gregorio_scanner_location *const loc,
1067 const bool first_word, const bool clear)
1068 {
1069 gregorio_syllable *next;
1070 gregorio_element **tab;
1071 int i;
1072 gregorio_not_null(elements, gregorio_add_syllable, return);
1073 gregorio_assert(number_of_voices == 1, gregorio_add_syllable,
1074 "gregorio only supports one voice", return);
1075 next = gregorio_calloc(1, sizeof(gregorio_syllable));
1076 next->position = position;
1077 next->no_linebreak_area = no_linebreak_area;
1078 next->euouae = euouae;
1079 next->text = first_character;
1080 next->translation = first_translation_character;
1081 next->translation_type = translation_type;
1082 next->abovelinestext = abovelinestext;
1083 next->first_word = first_word;
1084 next->clear = clear;
1085 if (loc) {
1086 next->src_line = loc->first_line;
1087 next->src_column = loc->first_column;
1088 next->src_offset = loc->first_offset;
1089 }
1090 next->next_syllable = NULL;
1091 next->previous_syllable = *current_syllable;
1092 tab = (gregorio_element **) gregorio_malloc(number_of_voices *
1093 sizeof(gregorio_element *));
1094 for (i = 0; i < number_of_voices; i++) {
1095 tab[i] = elements[i];
1096 }
1097 next->elements = tab;
1098 if (*current_syllable) {
1099 (*current_syllable)->next_syllable = next;
1100 }
1101 *current_syllable = next;
1102 }
1103
1104 static void gregorio_free_one_syllable(gregorio_syllable **syllable,
1105 int number_of_voices)
1106 {
1107 int i;
1108 gregorio_syllable *next;
1109 gregorio_not_null_ptr(syllable, gregorio_free_one_syllable, return);
1110 for (i = 0; i < number_of_voices; i++) {
1111 gregorio_free_elements((struct gregorio_element **)
1112 &((*syllable)->elements[i]));
1113 }
1114 if ((*syllable)->text) {
1115 gregorio_free_characters((*syllable)->text);
1116 }
1117 if ((*syllable)->translation) {
1118 gregorio_free_characters((*syllable)->translation);
1119 }
1120 free((*syllable)->abovelinestext);
1121 next = (*syllable)->next_syllable;
1122 if (next) {
1123 next->previous_syllable = NULL;
1124 }
1125 free((*syllable)->elements);
1126 free(*syllable);
1127 *syllable = next;
1128 }
1129
1130 static void gregorio_free_syllables(gregorio_syllable **syllable,
1131 int number_of_voices)
1132 {
1133 gregorio_not_null_ptr(syllable, gregorio_free_one_syllable, return);
1134 while (*syllable) {
1135 gregorio_free_one_syllable(syllable, number_of_voices);
1136 }
1137 }
1138
1139 gregorio_score *gregorio_new_score(void)
1140 {
1141 gregorio_score *new_score = gregorio_calloc(1, sizeof(gregorio_score));
1142 new_score->number_of_voices = 1;
1143 gregorio_set_score_staff_lines(new_score, 4);
1144 return new_score;
1145 }
1146
1147 static void gregorio_free_score_infos(gregorio_score *score)
1148 {
1149 gregorio_not_null(score, gregorio_free_score_infos, return);
1150 /* don't free the strings coming from headers; they will be freed when the
1151 * headers themselves are freed */
1152 if (score->first_voice_info) {
1153 gregorio_free_voice_infos(score->first_voice_info);
1154 }
1155 }
1156
1157 static void free_headers(gregorio_score *score) {
1158 gregorio_header *header = score->headers;
1159 while (header) {
1160 gregorio_header *next = header->next;
1161 free(header->name);
1162 free(header->value);
1163 free(header);
1164 header = next;
1165 }
1166 }
1167
1168 void gregorio_free_score(gregorio_score *score)
1169 {
1170 gregorio_not_null(score, gregorio_free_score, return);
1171 if (score->first_syllable) {
1172 gregorio_free_syllables(&(score->first_syllable),
1173 score->number_of_voices);
1174 }
1175 gregorio_free_score_infos(score);
1176 free_headers(score);
1177 free(score);
1178 }
1179
1180 void gregorio_add_voice_info(gregorio_voice_info **current_voice_info)
1181 {
1182 gregorio_voice_info *next = gregorio_calloc(1, sizeof(gregorio_voice_info));
1183 gregorio_assert(!*current_voice_info, gregorio_add_voice_info,
1184 "since gregorio only supports once voice, this should only be "
1185 "called once", (*current_voice_info)->next_voice_info = next);
1186 *current_voice_info = next;
1187 }
1188
1189 void gregorio_free_voice_infos(gregorio_voice_info *voice_info)
1190 {
1191 gregorio_voice_info *next;
1192 gregorio_not_null(voice_info, gregorio_free_voice_infos, return);
1193 while (voice_info) {
1194 next = voice_info->next_voice_info;
1195 free(voice_info);
1196 voice_info = next;
1197 }
1198 }
1199
1200 void gregorio_set_score_annotation(gregorio_score *score, char *annotation)
1201 {
1202 int annotation_num;
1203 gregorio_not_null(score, gregorio_set_score_annotation, return);
1204 /* save the annotation in the first spare place. */
1205 for (annotation_num = 0; annotation_num < MAX_ANNOTATIONS; ++annotation_num) {
1206 if (score->annotation[annotation_num] == NULL) {
1207 score->annotation[annotation_num] = annotation;
1208 break;
1209 }
1210 }
1211 if (annotation_num >= MAX_ANNOTATIONS) {
1212 gregorio_message(_("too many annotations"),
1213 "gregorio_set_annotation", VERBOSITY_WARNING, 0);
1214 }
1215 }
1216
1217 void gregorio_set_score_staff_lines(gregorio_score *const score,
1218 const char staff_lines)
1219 {
1220 gregorio_not_null(score, gregorio_set_score_staff_lines, return);
1221 if (staff_lines < 2 || staff_lines > 5) {
1222 gregorio_message(_("invalid number of staff lines"),
1223 "gregorio_set_score_staff_lines", VERBOSITY_ERROR, 0);
1224 return;
1225 }
1226 score->staff_lines = staff_lines;
1227 score->highest_pitch = LOWEST_PITCH + 4 + (2 * staff_lines);
1228 score->high_ledger_line_pitch = score->highest_pitch - 1;
1229 score->virgula_far_pitch = score->highest_pitch - 6;
1230
1231 gregorio_assert(score->highest_pitch <= MAX_PITCH,
1232 gregorio_set_score_staff_lines, "highest pitch exceeds MAX_PITCH",
1233 return);
1234 }
1235
1236 void gregorio_add_score_header(gregorio_score *score, char *name, char *value)
1237 {
1238 gregorio_header *header;
1239 gregorio_not_null(score, gregorio_add_score_header, return);
1240 header = (gregorio_header *)gregorio_malloc(sizeof(gregorio_header));
1241 header->name = name;
1242 header->value = value;
1243 header->next = NULL;
1244 if (score->last_header) {
1245 score->last_header->next = header;
1246 } else {
1247 score->headers = header;
1248 }
1249 score->last_header = header;
1250 }
1251
1252 /**********************************
1253 *
1254 * A function to build an integer from a key, very useful to represent
1255 * it in the structure.
1256 *
1257 *The representation is :
1258 *
1259 * * 1 for a C key on the first (bottom) line
1260 * * 3 for a C key on the second line
1261 * * 5 for a C key on the third line (default key)
1262 * * 7 for a C key on the fourth line
1263 * * 9 for a C key on the fifth line
1264 *
1265 * * -2 for a F key on the first line
1266 * * 0 for a F key on the second line
1267 * * 2 for a F key on the third line
1268 * * 4 for a F key on the fourth line
1269 * * 6 for a F key on the fifth line
1270 *
1271 *********************************/
1272
1273 int gregorio_calculate_new_key(gregorio_clef_info clef)
1274 {
1275 switch (clef.clef) {
1276 case CLEF_C:
1277 return (2 * clef.line) - 1;
1278 break;
1279 case CLEF_F:
1280 return (2 * clef.line) - 4;
1281 break;
1282 default:
1283 /* not reachable unless there's a programming error */
1284 /* LCOV_EXCL_START */
1285 gregorio_fail(gregorio_calculate_new_key, "can't calculate key");
1286 return NO_KEY;
1287 /* LCOV_EXCL_STOP */
1288 }
1289 }
1290
1291 /* shape is an output parameter */
1292 static __inline signed char next_pitch_from_glyph(const gregorio_glyph *glyph,
1293 gregorio_shape *const alterations, gregorio_shape *const shape)
1294 {
1295 while (glyph) {
1296 if (glyph->type == GRE_GLYPH) {
1297 if (glyph->u.notes.glyph_type == G_ALTERATION) {
1298 const gregorio_note *note;
1299 for (note = glyph->u.notes.first_note; note;
1300 note = note->next) {
1301 switch (note->u.note.shape) {
1302 case S_FLAT:
1303 case S_FLAT_PAREN:
1304 case S_FLAT_SOFT:
1305 case S_SHARP:
1306 case S_SHARP_PAREN:
1307 case S_SHARP_SOFT:
1308 case S_NATURAL:
1309 case S_NATURAL_PAREN:
1310 case S_NATURAL_SOFT:
1311 if (note->u.note.pitch >= LOWEST_PITCH &&
1312 note->u.note.pitch <= MAX_PITCH) {
1313 alterations[note->u.note.pitch] =
1314 note->u.note.shape;
1315 }
1316 break;
1317 default:
1318 /* not reachable unless there's a programming error */
1319 /* LCOV_EXCL_START */
1320 gregorio_fail2(next_pitch_from_glyph,
1321 "unrecognized alteration shape: %s",
1322 gregorio_shape_to_string(note->u.note.shape));
1323 break;
1324 /* LCOV_EXCL_STOP */
1325 }
1326 }
1327 } else if (glyph->u.notes.first_note) {
1328 assert(glyph->u.notes.first_note->type == GRE_NOTE);
1329 if (shape) {
1330 *shape = alterations[
1331 glyph->u.notes.first_note->u.note.pitch];
1332 }
1333 return glyph->u.notes.first_note->u.note.pitch;
1334 }
1335 }
1336 glyph = glyph->next;
1337 }
1338 return 0;
1339 }
1340
1341 /* shape is an output parameter */
1342 static signed char syllable_first_note(const gregorio_syllable *syllable,
1343 gregorio_shape *const alterations, gregorio_shape *const shape)
1344 {
1345 gregorio_element *element;
1346 gregorio_glyph *glyph;
1347 signed char pitch;
1348 gregorio_not_null(syllable, syllable_first_note, return 0);
1349 element = syllable->elements[0];
1350 while (element) {
1351 if (element->type == GRE_CUSTOS) {
1352 return element->u.misc.pitched.pitch;
1353 }
1354 if (element->type == GRE_ELEMENT && element->u.first_glyph) {
1355 glyph = element->u.first_glyph;
1356 pitch = next_pitch_from_glyph(glyph, alterations, shape);
1357 if (pitch) {
1358 return pitch;
1359 }
1360 }
1361 element = element->next;
1362 }
1363 return 0;
1364 }
1365
1366 /* shape is an output parameter */
1367 signed char gregorio_determine_next_pitch(const gregorio_syllable *syllable,
1368 const gregorio_element *element, const gregorio_glyph *glyph,
1369 gregorio_shape *const shape)
1370 {
1371 signed char pitch;
1372 gregorio_shape alterations[MAX_PITCH + 1];
1373
1374 memset(alterations, 0, sizeof alterations);
1375
1376 if (shape) {
1377 *shape = S_UNDETERMINED;
1378 }
1379 gregorio_not_null(element, gregorio_determine_next_pitch,
1380 return DUMMY_PITCH);
1381 gregorio_not_null(syllable, gregorio_determine_next_pitch,
1382 return DUMMY_PITCH);
1383 /* we first explore the next glyphs to find a note, if there is one */
1384 if (glyph) {
1385 glyph = glyph->next;
1386 pitch = next_pitch_from_glyph(glyph, alterations, shape);
1387 if (pitch) {
1388 return pitch;
1389 }
1390 }
1391 /* then we do the same with the elements */
1392 element = element->next;
1393 while (element) {
1394 if (element->type == GRE_CUSTOS) {
1395 return element->u.misc.pitched.pitch;
1396 }
1397 if (element->type == GRE_ELEMENT && element->u.first_glyph) {
1398 glyph = element->u.first_glyph;
1399 pitch = next_pitch_from_glyph(glyph, alterations, shape);
1400 if (pitch) {
1401 return pitch;
1402 }
1403 } /* I think this is optimized out; LCOV_EXCL_LINE */
1404 element = element->next;
1405 }
1406
1407 /* then we do the same with the syllables */
1408 syllable = syllable->next_syllable;
1409 while (syllable) {
1410 /* we call another function that will return the pitch of the first
1411 * note if syllable has a note, and 0 else */
1412 pitch = syllable_first_note(syllable, alterations, shape);
1413 if (pitch) {
1414 return pitch;
1415 }
1416 syllable = syllable->next_syllable;
1417 }
1418 /* here it means that there is no next note, so we return a stupid value,
1419 * but it won' t be used */
1420 if (shape) {
1421 *shape = S_UNDETERMINED;
1422 }
1423 return DUMMY_PITCH;
1424 }
1425
1426 /**********************************
1427 *
1428 * A function called after the entire score is determined : we check
1429 * if the first element is a key change, if it is the case we delete
1430 * it and we update the score->voice-info->initial_key. Works in
1431 * polyphony.
1432 *
1433 ******************************/
1434
1435 void gregorio_fix_initial_keys(gregorio_score *score,
1436 gregorio_clef_info default_clef)
1437 {
1438 gregorio_element *element;
1439 gregorio_voice_info *voice_info;
1440 int i;
1441 char to_delete = 1;
1442
1443 gregorio_not_null(score, gregorio_fix_initial_keys, return);
1444 gregorio_not_null(score->first_voice_info, gregorio_fix_initial_keys,
1445 return);
1446 if (!score->first_syllable) {
1447 /* valid but almost nonsense: a score with no syllables */
1448 return;
1449 }
1450 voice_info = score->first_voice_info;
1451 for (i = 0; i < score->number_of_voices; i++) {
1452 element = score->first_syllable->elements[i];
1453 if (element && element->type == GRE_CLEF) {
1454 voice_info->initial_clef = element->u.misc.clef;
1455 gregorio_free_one_element(&(score->first_syllable->elements[i]));
1456 gregorio_messagef("gregorio_fix_initial_keys", VERBOSITY_INFO, 0,
1457 _("in voice %d the first element is a key definition, "
1458 "considered as initial key"), i + 1);
1459 }
1460 voice_info = voice_info->next_voice_info;
1461 }
1462
1463 /* then we suppress syllables that contain nothing anymore : case of (c2)
1464 * at beginning of files */
1465
1466 for (i = 0; i < score->number_of_voices; i++) {
1467 if (score->first_syllable->elements[i]) {
1468 to_delete = 0;
1469 break;
1470 }
1471 }
1472
1473 if (to_delete) {
1474 gregorio_free_one_syllable(&(score->first_syllable),
1475 score->number_of_voices);
1476 }
1477 /* finally we initialize voice infos that have no initial key to default
1478 * key */
1479
1480 voice_info = score->first_voice_info;
1481
1482 for (i = 0; i < score->number_of_voices; i++) {
1483 if (!voice_info->initial_clef.line) {
1484 voice_info->initial_clef = default_clef;
1485 gregorio_messagef("gregorio_fix_initial_keys", VERBOSITY_INFO, 0,
1486 _("no initial key definition in voice %d, default key "
1487 "definition applied"), i + 1);
1488 }
1489 voice_info = voice_info->next_voice_info;
1490 }
1491 }
1492
1493 /**********************************
1494 *
1495 * A small function to determine if an element list contains only
1496 * special elements (bar, key-change, etc.), useful because the
1497 * representation (in xml for example) may vary according to it.
1498 *
1499 *********************************/
1500
1501 bool gregorio_is_only_special(gregorio_element *element)
1502 {
1503 gregorio_not_null(element, gregorio_is_only_special, return 0);
1504 while (element) {
1505 if (element->type == GRE_ELEMENT) {
1506 return 0;
1507 }
1508 element = element->next;
1509 }
1510 return 1;
1511 }
1512
1513 /* This should only be called when an unknown enum value is encountered; as
1514 * such, this is a programming error */
1515 /* LCOV_EXCL_START */
1516 const char *gregorio_unknown(int value) {
1517 static char buf[20];
1518 gregorio_snprintf(buf, sizeof buf, "?%d", value);
1519 return buf;
1520 }
1521 /* LCOV_EXCL_STOP */
1522
1523 /*
1524 * A small helper for the following function
1525 */
1526
1527 static __inline bool is_clef(gregorio_type x)
1528 {
1529 return x == GRE_CLEF;
1530 }
1531
1532 /*
1533 * This function is used in write_syllable, it detects if the syllable is like
1534 * (c4), (::c4), (z0c4) or (z0::c4). It returns the gregorio_element of the
1535 * clef change.
1536 */
1537 gregorio_element *gregorio_get_clef_change(gregorio_syllable *syllable)
1538 {
1539 gregorio_element *element;
1540 gregorio_assert(syllable && syllable->elements && syllable->elements[0],
1541 gregoriotex_syllable_is_clef_change, "invalid syllable",
1542 return NULL);
1543 element = syllable->elements[0];
1544 /* we just detect the foud cases */
1545 if (element->type == GRE_CUSTOS && element->next
1546 && (is_clef(element->next->type)) && !element->next->next) {
1547 return element->next;
1548 }
1549 if (element->type == GRE_BAR && element->next
1550 && (is_clef(element->next->type)) && !element->next->next) {
1551 return element->next;
1552 }
1553 if ((is_clef(element->type)) && !element->next) {
1554 return element;
1555 }
1556 if (element->type == GRE_CUSTOS && element->next
1557 && element->next->type == GRE_BAR && element->next->next
1558 && (is_clef(element->next->next->type))
1559 && !element->next->next->next) {
1560 return element->next->next;
1561 }
1562 return NULL;
1563 }
1564
1565 unsigned short gregorio_add_hepisema_adjustment(
1566 const gregorio_hepisema_vbasepos vbasepos, char *const nudge)
1567 {
1568 if (hepisema_adjustments_last == USHRT_MAX) {
1569 /* It's not reasonable to trigger this condition while testing */
1570 /* LCOV_EXCL_START */
1571 gregorio_message(_("too many horizontal episema adjustments"),
1572 "gregorio_add_hepisema_adjustment", VERBOSITY_ERROR, 0);
1573 return 0;
1574 /* LCOV_EXCL_STOP */
1575 }
1576 ++hepisema_adjustments_last;
1577 if (hepisema_adjustments_last >= hepisema_adjustments_capacity) {
1578 hepisema_adjustments = gregorio_grow_buffer(hepisema_adjustments,
1579 &hepisema_adjustments_capacity, gregorio_hepisema_adjustment);
1580 }
1581 hepisema_adjustments[hepisema_adjustments_last].vbasepos = vbasepos;
1582 hepisema_adjustments[hepisema_adjustments_last].nudge = nudge;
1583 hepisema_adjustments[hepisema_adjustments_last].pitch_extremum = NO_PITCH;
1584 return hepisema_adjustments_last;
1585 }
1586
1587 gregorio_hepisema_adjustment *gregorio_get_hepisema_adjustment(
1588 const unsigned short index)
1589 {
1590 gregorio_assert(index <= hepisema_adjustments_last,
1591 gregorio_get_hepisema_adjustment, "array index out of bounds",
1592 return &hepisema_adjustments[0]);
1593 return &hepisema_adjustments[index];
1594 }
1595
1596 const char *gregorio_texverb(unsigned short index)
1597 {
1598 gregorio_assert(index <= texverbs_last, gregorio_texverb,
1599 "array index out of bounds", return NULL);
1600 return texverbs[index];
1601 }
1602
1603 void gregorio_change_texverb(unsigned short index, char *texverb)
1604 {
1605 /* not allowed to change index 0 (== NULL) */
1606 gregorio_assert(index != 0, gregorio_texverb,
1607 "array index 0 may not be changed", return);
1608 gregorio_assert(index <= texverbs_last, gregorio_texverb,
1609 "array index out of bounds", return);
1610 if (texverbs[index]) {
1611 free(texverbs[index]);
1612 }
1613 texverbs[index] = texverb;
1614 }
1615
1616 ENUM_TO_STRING(gregorio_type, GREGORIO_TYPE)
1617 ENUM_TO_STRING(gregorio_shape, GREGORIO_SHAPE)
1618 ENUM_TO_STRING(gregorio_bar, GREGORIO_BAR)
1619 ENUM_TO_STRING(gregorio_sign, GREGORIO_SIGN)
1620 ENUM_TO_STRING(gregorio_space, GREGORIO_SPACE)
1621 ENUM_TO_STRING(gregorio_liquescentia, GREGORIO_LIQUESCENTIA)
1622 ENUM_TO_STRING(grehepisema_size, GREHEPISEMA_SIZE)
1623 ENUM_TO_STRING(gregorio_vposition, GREGORIO_VPOSITION)
1624 ENUM_TO_STRING(gregorio_glyph_type, GREGORIO_GLYPH_TYPE)
1625 ENUM_TO_STRING(grestyle_style, GRESTYLE_STYLE)
1626 ENUM_TO_STRING(gregorio_tr_centering, GREGORIO_TR_CENTERING)
1627 ENUM_TO_STRING(gregorio_nlba, GREGORIO_NLBA)
1628 ENUM_TO_STRING(gregorio_euouae, GREGORIO_EUOUAE)
1629 ENUM_TO_STRING(gregorio_word_position, GREGORIO_WORD_POSITION)
1630 ENUM_TO_STRING(gregorio_hepisema_vbasepos, GREGORIO_HEPISEMA_VBASEPOS)