Debian Codegraph @

texk/gregorio/gregorio-src/src/gabc/gabc-write.c

raw
1 /*
2 * Gregorio is a program that translates gabc files to GregorioTeX
3 * This file provides functions for writing gabc from Gregorio structures.
4 *
5 * Copyright (C) 2006-2025 The Gregorio Project (see CONTRIBUTORS.md)
6 *
7 * This file is part of Gregorio.
8 *
9 * This program is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * This is a simple and easyly understandable output module. If you want to
23 * write a module, you can consider it as a model.
24 */
25
26 #include "config.h"
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <string.h> /* for strchr */
30 #include "bool.h"
31 #include "characters.h"
32 #include "struct.h"
33 #include "unicode.h"
34 #include "messages.h"
35 #include "plugins.h"
36
37 #include "gabc.h"
38
39 typedef enum {
40 GABC_NORMAL,
41 GABC_AT_PROTRUSION_FACTOR,
42 GABC_IN_PROTRUSION_FACTOR,
43 GABC_IN_AUTO_PROTRUSION
44 } gabc_write_state;
45
46 static gabc_write_state write_state;
47
48 static __inline char pitch_letter(const char height) {
49 char result = height + 'a' - LOWEST_PITCH;
50 if (result == 'o') {
51 return 'p';
52 }
53 return result;
54 }
55
56 /* not reachable unless there's a programming error */
57 /* LCOV_EXCL_START */
58 static __inline void unsupported(const char *fn, const int line,
59 const char *type, const char *value)
60 {
61 gregorio_messagef(fn, VERBOSITY_ASSERTION, line, _("unsupported %s %s"),
62 type, value);
63 }
64 /* LCOV_EXCL_STOP */
65
66 /*
67 * Output one attribute, allowing for multi-line values
68 */
69 static void gabc_write_str_attribute(FILE *f, const char *name,
70 const char *attr)
71 {
72 if (attr) {
73 fprintf(f, "%s: %s%s;\n", name, attr, strchr(attr, '\n') ? ";" : "");
74 }
75 }
76
77 /*
78 *
79 * Then we start the functions made to write the text of the syllable. See
80 * comments on struct.h and struct-utils.c to understand more deeply.
81 *
82 * This first function will be called each time we will encounter a
83 * gregorio_character which is the beginning of a style.
84 *
85 */
86
87 static void gabc_write_begin(FILE *f, grestyle_style style)
88 {
89 switch (style) {
90 case ST_ITALIC:
91 fprintf(f, "<i>");
92 break;
93 case ST_COLORED:
94 fprintf(f, "<c>");
95 break;
96 case ST_SMALL_CAPS:
97 fprintf(f, "<sc>");
98 break;
99 case ST_BOLD:
100 fprintf(f, "<b>");
101 break;
102 case ST_FORCED_CENTER:
103 fprintf(f, "{");
104 break;
105 case ST_TT:
106 fprintf(f, "<tt>");
107 break;
108 case ST_UNDERLINED:
109 fprintf(f, "<ul>");
110 break;
111 case ST_ELISION:
112 fprintf(f, "<e>");
113 break;
114 case ST_PROTRUSION_FACTOR:
115 write_state = GABC_AT_PROTRUSION_FACTOR;
116 break;
117 case ST_INITIAL:
118 case ST_CENTER:
119 case ST_FIRST_WORD:
120 case ST_FIRST_SYLLABLE:
121 case ST_FIRST_SYLLABLE_INITIAL:
122 case ST_PROTRUSION:
123 /* nothing should be emitted for these */
124 break;
125 default:
126 /* not reachable unless there's a programming error */
127 /* LCOV_EXCL_START */
128 unsupported("gabc_write_begin", __LINE__, "style",
129 grestyle_style_to_string(style));
130 break;
131 /* LCOV_EXCL_STOP */
132 }
133 }
134
135 /*
136 *
137 * This function is about the same but for ends of styles.
138 *
139 */
140
141 static void gabc_write_end(FILE *f, grestyle_style style)
142 {
143 switch (style) {
144 case ST_ITALIC:
145 fprintf(f, "</i>");
146 break;
147 case ST_COLORED:
148 fprintf(f, "</c>");
149 break;
150 case ST_SMALL_CAPS:
151 fprintf(f, "</sc>");
152 break;
153 case ST_BOLD:
154 fprintf(f, "</b>");
155 break;
156 case ST_FORCED_CENTER:
157 fprintf(f, "}");
158 break;
159 case ST_TT:
160 fprintf(f, "</tt>");
161 break;
162 case ST_UNDERLINED:
163 fprintf(f, "</ul>");
164 break;
165 case ST_ELISION:
166 fprintf(f, "</e>");
167 break;
168 case ST_PROTRUSION_FACTOR:
169 if (write_state == GABC_IN_PROTRUSION_FACTOR) {
170 fprintf(f, ">");
171 }
172 break;
173 case ST_INITIAL:
174 case ST_CENTER:
175 case ST_FIRST_WORD:
176 case ST_FIRST_SYLLABLE:
177 case ST_FIRST_SYLLABLE_INITIAL:
178 case ST_PROTRUSION:
179 /* nothing should be emitted for these */
180 break;
181 default:
182 /* not reachable unless there's a programming error */
183 /* LCOV_EXCL_START */
184 unsupported("gabc_write_end", __LINE__, "style",
185 grestyle_style_to_string(style));
186 break;
187 /* LCOV_EXCL_STOP */
188 }
189 }
190
191 /*
192 *
193 * The function called when we will encounter a character. There may be other
194 * representations of the character (for example for Omega), so it is necessary
195 * to have such a function defined in each module.
196 *
197 */
198
199 static void gabc_print_char(FILE *f, const grewchar to_print)
200 {
201 if (write_state == GABC_AT_PROTRUSION_FACTOR) {
202 write_state = GABC_IN_PROTRUSION_FACTOR;
203 if (to_print == 'd') {
204 fprintf(f, "<pr");
205 } else {
206 fprintf(f, "<pr:");
207 gregorio_print_unichar(f, to_print);
208 }
209 } else {
210 switch(to_print) {
211 case ',':
212 case ';':
213 case ':':
214 case '.':
215 if (write_state == GABC_IN_AUTO_PROTRUSION) {
216 break;
217 }
218 /* fall through */
219 case '-':
220 case '{':
221 case '}':
222 case '(':
223 case ')':
224 case '[':
225 case ']':
226 case '|':
227 case '<':
228 case '%':
229 case '$':
230 gregorio_print_unichar(f, '$');
231 break;
232 }
233 gregorio_print_unichar(f, to_print);
234 }
235 }
236
237 /*
238 *
239 * This function writes a unicode string as gregorio_print_unistring, but
240 * handles escapes using gabc_print_char.
241 *
242 */
243 static void gabc_print_unistring(FILE *f, const grewchar *first_char)
244 {
245 while (*first_char != 0) {
246 gabc_print_char(f, *first_char);
247 first_char++;
248 }
249 }
250
251 /*
252 *
253 * This function writes a null-terminated string as gregorio_print_unistring,
254 * but handles escapes using gabc_print_char.
255 *
256 */
257 static void gabc_print_string(FILE *f, const char *first_char)
258 {
259 while (*first_char != '\0') {
260 gabc_print_char(f, (grewchar)*first_char);
261 first_char++;
262 }
263 }
264
265 /*
266 *
267 * This function writes the special chars. As the specials chars are
268 * represented simply in gabc, this function is very simple, but for TeX output
269 * modules, this may be.. a little more difficult.
270 *
271 */
272 static void gabc_write_special_char(FILE *f, const grewchar *first_char)
273 {
274 fprintf(f, "<sp>");
275 gabc_print_unistring(f, first_char);
276 fprintf(f, "</sp>");
277 }
278
279 /*
280 *
281 * This functions writes verbatim output... but as the previous one it is very
282 * simple.
283 *
284 */
285 static void gabc_write_verb(FILE *f, const grewchar *first_char)
286 {
287 if (write_state == GABC_AT_PROTRUSION_FACTOR) {
288 /* this is an auto protrusion, so ignore it */
289 write_state = GABC_IN_AUTO_PROTRUSION;
290 } else {
291 fprintf(f, "<v>");
292 gabc_print_unistring(f, first_char);
293 fprintf(f, "</v>");
294 }
295 }
296
297 /*
298 *
299 * Quite important: the function that writes the liquescentia. It is called at
300 * the end of the function that writes one glyph.
301 *
302 */
303
304 static void gabc_write_end_liquescentia(FILE *f, char liquescentia)
305 {
306 switch (liquescentia & TAIL_LIQUESCENTIA_MASK) {
307 case L_DEMINUTUS:
308 fprintf(f, "~");
309 break;
310 case L_AUCTUS_ASCENDENS:
311 fprintf(f, "<");
312 break;
313 case L_AUCTUS_DESCENDENS:
314 fprintf(f, ">");
315 break;
316 }
317 }
318
319 /*
320 *
321 * The function that writes a key change... quite simple.
322 *
323 */
324
325 static void gabc_write_clef(FILE *f, gregorio_clef_info clef)
326 {
327 fprintf(f, "%c%s%d", clef.clef == CLEF_C? 'c' : 'f', clef.flatted? "b" : "",
328 clef.line);
329 if (clef.secondary_line) {
330 fprintf(f, "@%c%s%d", clef.secondary_clef == CLEF_C? 'c' : 'f',
331 clef.secondary_flatted? "b" : "", clef.secondary_line);
332 }
333 }
334
335 /*
336 *
337 * The function that writes spaces, called when we encounter one.
338 *
339 */
340
341 static void gabc_write_space(FILE *f, gregorio_space type, char *factor,
342 bool next_is_space)
343 {
344 switch (type) {
345 case SP_NEUMATIC_CUT:
346 if (next_is_space) {
347 /* if the following is not a space, we omit this because the
348 * code always puts a "/" between elements unless there is some
349 * other space there */
350 fprintf (f, "/");
351 }
352 break;
353 case SP_LARGER_SPACE:
354 fprintf(f, "//");
355 break;
356 case SP_GLYPH_SPACE:
357 fprintf(f, " ");
358 break;
359 case SP_AD_HOC_SPACE:
360 fprintf(f, "/[%s]", factor);
361 break;
362 case SP_NEUMATIC_CUT_NB:
363 fprintf(f, "!/");
364 break;
365 case SP_LARGER_SPACE_NB:
366 fprintf(f, "!//");
367 break;
368 case SP_GLYPH_SPACE_NB:
369 fprintf(f, "! ");
370 break;
371 case SP_AD_HOC_SPACE_NB:
372 fprintf(f, "!/[%s]", factor);
373 break;
374 default:
375 /* not reachable unless there's a programming error */
376 /* LCOV_EXCL_START */
377 unsupported("gabc_write_space", __LINE__, "space type",
378 gregorio_space_to_string(type));
379 break;
380 /* LCOV_EXCL_STOP */
381 }
382 }
383
384 /*
385 *
386 * A function to write a bar.
387 *
388 */
389
390 static void gabc_write_bar(FILE *f, gregorio_bar type)
391 {
392 switch (type) {
393 case B_VIRGULA:
394 fprintf(f, "`");
395 break;
396 case B_DIVISIO_MINIMA:
397 fprintf(f, ",");
398 break;
399 case B_DIVISIO_MINOR:
400 fprintf(f, ";");
401 break;
402 case B_DIVISIO_MAIOR:
403 fprintf(f, ":");
404 break;
405 case B_DIVISIO_FINALIS:
406 fprintf(f, "::");
407 break;
408 case B_DIVISIO_MINOR_D1:
409 fprintf(f, ";1");
410 break;
411 case B_DIVISIO_MINOR_D2:
412 fprintf(f, ";2");
413 break;
414 case B_DIVISIO_MINOR_D3:
415 fprintf(f, ";3");
416 break;
417 case B_DIVISIO_MINOR_D4:
418 fprintf(f, ";4");
419 break;
420 case B_DIVISIO_MINOR_D5:
421 fprintf(f, ";5");
422 break;
423 case B_DIVISIO_MINOR_D6:
424 fprintf(f, ";6");
425 break;
426 case B_DIVISIO_MINOR_D7:
427 fprintf(f, ";7");
428 break;
429 case B_DIVISIO_MINOR_D8:
430 fprintf(f, ";8");
431 break;
432 case B_VIRGULA_HIGH:
433 fprintf(f, "`0");
434 break;
435 case B_DIVISIO_MINIMA_HIGH:
436 fprintf(f, ",0");
437 break;
438 case B_DIVISIO_MAIOR_DOTTED:
439 fprintf(f, ":?");
440 break;
441 case B_DIVISIO_MINIMIS:
442 fprintf(f, "^");
443 break;
444 case B_DIVISIO_MINIMIS_HIGH:
445 fprintf(f, "^0");
446 break;
447 case B_VIRGULA_PAREN:
448 fprintf(f, "`?");
449 break;
450 case B_VIRGULA_PAREN_HIGH:
451 fprintf(f, "`0?");
452 break;
453 case B_DIVISIO_MINIMA_PAREN:
454 fprintf(f, ",?");
455 break;
456 case B_DIVISIO_MINIMA_PAREN_HIGH:
457 fprintf(f, ",0?");
458 break;
459 default:
460 /* not reachable unless there's a programming error */
461 /* LCOV_EXCL_START */
462 unsupported("gabc_write_bar", __LINE__, "bar type",
463 gregorio_bar_to_string(type));
464 break;
465 /* LCOV_EXCL_STOP */
466 }
467 }
468
469 /* writing the signs of a bar */
470
471 static void gabc_write_bar_signs(FILE *f, gregorio_sign type)
472 {
473 switch (type) {
474 case _V_EPISEMA:
475 fprintf(f, "'");
476 break;
477 case _V_EPISEMA_BAR_H_EPISEMA:
478 fprintf(f, "'_");
479 break;
480 case _BAR_H_EPISEMA:
481 fprintf(f, "_");
482 break;
483 case _NO_SIGN:
484 /* if there's no sign, don't emit anything */
485 break;
486 default:
487 /* not reachable unless there's a programming error */
488 /* LCOV_EXCL_START */
489 unsupported("gabc_write_bar_signs", __LINE__, "bar signs",
490 gregorio_sign_to_string(type));
491 break;
492 /* LCOV_EXCL_STOP */
493 }
494 }
495
496 static void gabc_hepisema(FILE *f, const char *prefix, bool connect,
497 grehepisema_size size)
498 {
499 fprintf(f, "_%s", prefix);
500 if (!connect) {
501 fprintf(f, "2");
502 }
503 switch (size) {
504 case H_SMALL_LEFT:
505 fprintf(f, "3");
506 break;
507 case H_SMALL_CENTRE:
508 fprintf(f, "4");
509 break;
510 case H_SMALL_RIGHT:
511 fprintf(f, "5");
512 break;
513 case H_NORMAL:
514 /* nothing to print */
515 break;
516 default:
517 /* not reachable unless there's a programming error */
518 /* LCOV_EXCL_START */
519 unsupported("gabc_hepisema", __LINE__, "hepisema size",
520 grehepisema_size_to_string(size));
521 break;
522 /* LCOV_EXCL_STOP */
523 }
524 }
525
526 static const char *vepisema_position(gregorio_note *note)
527 {
528 if (!note->v_episema_height) {
529 return "";
530 }
531 if (note->v_episema_height < note->u.note.pitch) {
532 return "0";
533 }
534 return "1";
535 }
536
537 static const char *mora_vposition(gregorio_note *note)
538 {
539 switch (note->mora_vposition) {
540 case VPOS_AUTO:
541 return "";
542 case VPOS_ABOVE:
543 return "1";
544 case VPOS_BELOW:
545 return "0";
546 default:
547 /* not reachable unless there's a programming error */
548 /* LCOV_EXCL_START */
549 unsupported("mora_vposition", __LINE__, "vposition",
550 gregorio_vposition_to_string(note->mora_vposition));
551 return "";
552 /* LCOV_EXCL_STOP */
553 }
554 }
555
556 static void write_note_heuristics(FILE *f, gregorio_note *note) {
557 switch (note->high_ledger_specificity) {
558 case LEDGER_EXPLICIT:
559 fprintf(f, "[hl:%c]", note->high_ledger_line? '1' : '0');
560 break;
561 case LEDGER_EXPLICITLY_DRAWN:
562 fprintf(f, "[oll:%c]", note->high_ledger_line? '1' : '0');
563 break;
564 default:
565 break;
566 }
567 switch (note->low_ledger_specificity) {
568 case LEDGER_EXPLICIT:
569 fprintf(f, "[ll:%c]", note->low_ledger_line? '1' : '0');
570 break;
571 case LEDGER_EXPLICITLY_DRAWN:
572 fprintf(f, "[ull:%c]", note->low_ledger_line? '1' : '0');
573 break;
574 default:
575 break;
576 }
577 }
578
579 typedef struct glyph_context {
580 gregorio_syllable *syllable;
581 gregorio_element *element;
582 unsigned short he_adjustment_index[2];
583 } glyph_context;
584
585 /*
586 *
587 * The function that writes one gregorio_note.
588 *
589 */
590
591 static void gabc_write_gregorio_note(FILE *f, gregorio_note *note,
592 const bool is_quadratum)
593 {
594 char shape;
595 gregorio_assert(note, gabc_write_gregorio_note, "call with NULL argument",
596 return);
597 gregorio_assert(note->type == GRE_NOTE, gabc_write_gregorio_note,
598 "call with argument which type is not GRE_NOTE", return);
599 shape = note->u.note.shape;
600 switch (shape) {
601 /* first we write the letters that determine the shapes */
602 case S_PUNCTUM:
603 if (is_quadratum) {
604 fprintf(f, "%cq", pitch_letter(note->u.note.pitch));
605 } else {
606 fprintf(f, "%c", pitch_letter(note->u.note.pitch));
607 }
608 break;
609 case S_PUNCTUM_INCLINATUM_ASCENDENS:
610 fprintf(f, "%c1", toupper((unsigned char)pitch_letter(note->u.note.pitch)));
611 break;
612 case S_PUNCTUM_INCLINATUM_DESCENDENS:
613 fprintf(f, "%c0", toupper((unsigned char)pitch_letter(note->u.note.pitch)));
614 break;
615 case S_PUNCTUM_INCLINATUM_STANS:
616 fprintf(f, "%c2", toupper((unsigned char)pitch_letter(note->u.note.pitch)));
617 break;
618 case S_PUNCTUM_INCLINATUM_DEMINUTUS:
619 if (note->next) {
620 fprintf(f, "%c~", toupper((unsigned char)pitch_letter(note->u.note.pitch)));
621 } else {
622 fprintf(f, "%c", toupper((unsigned char)pitch_letter(note->u.note.pitch)));
623 }
624 break;
625 case S_PUNCTUM_INCLINATUM_AUCTUS:
626 fprintf(f, "%c", toupper((unsigned char)pitch_letter(note->u.note.pitch)));
627 break;
628 case S_FLAT:
629 fprintf(f, "%cx", pitch_letter(note->u.note.pitch));
630 break;
631 case S_FLAT_PAREN:
632 fprintf(f, "%cx?", pitch_letter(note->u.note.pitch));
633 break;
634 case S_FLAT_SOFT:
635 fprintf(f, "%cX", pitch_letter(note->u.note.pitch));
636 break;
637 case S_NATURAL:
638 fprintf(f, "%cy", pitch_letter(note->u.note.pitch));
639 break;
640 case S_NATURAL_PAREN:
641 fprintf(f, "%cy?", pitch_letter(note->u.note.pitch));
642 break;
643 case S_NATURAL_SOFT:
644 fprintf(f, "%cY", pitch_letter(note->u.note.pitch));
645 break;
646 case S_SHARP:
647 fprintf(f, "%c#", pitch_letter(note->u.note.pitch));
648 break;
649 case S_SHARP_PAREN:
650 fprintf(f, "%c#?", pitch_letter(note->u.note.pitch));
651 break;
652 case S_SHARP_SOFT:
653 fprintf(f, "%c#*", pitch_letter(note->u.note.pitch));
654 break;
655 case S_VIRGA:
656 fprintf(f, "%cv", pitch_letter(note->u.note.pitch));
657 break;
658 case S_VIRGA_REVERSA:
659 fprintf(f, "%cV", pitch_letter(note->u.note.pitch));
660 break;
661 case S_ORISCUS_ASCENDENS:
662 fprintf(f, "%co1", pitch_letter(note->u.note.pitch));
663 break;
664 case S_ORISCUS_DESCENDENS:
665 fprintf(f, "%co0", pitch_letter(note->u.note.pitch));
666 break;
667 case S_ORISCUS_DEMINUTUS:
668 fprintf(f, "%co", pitch_letter(note->u.note.pitch));
669 /* Note: the DEMINUTUS is also in the liquescentia */
670 break;
671 case S_QUILISMA:
672 if (is_quadratum) {
673 fprintf(f, "%cW", pitch_letter(note->u.note.pitch));
674 } else {
675 fprintf(f, "%cw", pitch_letter(note->u.note.pitch));
676 }
677 break;
678 case S_LINEA:
679 fprintf(f, "%c=", pitch_letter(note->u.note.pitch));
680 break;
681 case S_LINEA_PUNCTUM:
682 fprintf(f, "%cR", pitch_letter(note->u.note.pitch));
683 break;
684 case S_ORISCUS_SCAPUS_ASCENDENS:
685 fprintf(f, "%cO1", pitch_letter(note->u.note.pitch));
686 break;
687 case S_ORISCUS_SCAPUS_DESCENDENS:
688 fprintf(f, "%cO0", pitch_letter(note->u.note.pitch));
689 break;
690 case S_STROPHA:
691 case S_STROPHA_AUCTA:
692 fprintf(f, "%cs", pitch_letter(note->u.note.pitch));
693 break;
694 default:
695 /* includes S_BIVIRGA, S_TRIVIRGA, S_DISTROPHA, and S_TRISTROPHA */
696 /* not reachable unless there's a programming error */
697 /* LCOV_EXCL_START */
698 unsupported("gabc_write_gregorio_note", __LINE__, "shape",
699 gregorio_shape_to_string(shape));
700 fprintf(f, "%c", pitch_letter(note->u.note.pitch));
701 break;
702 /* LCOV_EXCL_STOP */
703 }
704 if (note->u.note.is_cavum) {
705 fprintf(f, "r");
706 }
707 switch (note->signs) {
708 case _PUNCTUM_MORA:
709 fprintf(f, ".%s", mora_vposition(note));
710 break;
711 case _AUCTUM_DUPLEX:
712 fprintf(f, "..");
713 break;
714 case _V_EPISEMA:
715 fprintf(f, "'%s", vepisema_position(note));
716 break;
717 case _V_EPISEMA_PUNCTUM_MORA:
718 fprintf(f, "'%s.%s", vepisema_position(note), mora_vposition(note));
719 break;
720 case _V_EPISEMA_AUCTUM_DUPLEX:
721 fprintf(f, "'%s..", vepisema_position(note));
722 break;
723 case _NO_SIGN:
724 /* if there's no sign, don't emit anything */
725 break;
726 default:
727 /* not reachable unless there's a programming error */
728 /* LCOV_EXCL_START */
729 unsupported("gabc_write_gregorio_note", __LINE__, "shape signs",
730 gregorio_sign_to_string(note->signs));
731 break;
732 /* LCOV_EXCL_STOP */
733 }
734 switch (note->special_sign) {
735 case _ACCENTUS:
736 fprintf(f, "r1");
737 break;
738 case _ACCENTUS_REVERSUS:
739 fprintf(f, "r2");
740 break;
741 case _CIRCULUS:
742 fprintf(f, "r3");
743 break;
744 case _SEMI_CIRCULUS:
745 fprintf(f, "r4");
746 break;
747 case _SEMI_CIRCULUS_REVERSUS:
748 fprintf(f, "r5");
749 break;
750 case _MUSICA_FICTA_FLAT:
751 fprintf(f, "r6");
752 break;
753 case _MUSICA_FICTA_NATURAL:
754 fprintf(f, "r7");
755 break;
756 case _MUSICA_FICTA_SHARP:
757 fprintf(f, "r8");
758 break;
759 case _NO_SIGN:
760 /* if there's no sign, don't emit anything */
761 break;
762 default:
763 /* not reachable unless there's a programming error */
764 /* LCOV_EXCL_START */
765 unsupported("gabc_write_gregorio_note", __LINE__, "special sign",
766 gregorio_sign_to_string(note->special_sign));
767 break;
768 /* LCOV_EXCL_STOP */
769 }
770 if (note->h_episema_above == HEPISEMA_AUTO
771 && note->h_episema_below == HEPISEMA_AUTO) {
772 gabc_hepisema(f, "", note->h_episema_above_connect,
773 note->h_episema_above_size);
774 } else {
775 if (note->h_episema_below == HEPISEMA_FORCED) {
776 gabc_hepisema(f, "0", note->h_episema_below_connect,
777 note->h_episema_below_size);
778 }
779 if (note->h_episema_above == HEPISEMA_FORCED) {
780 gabc_hepisema(f, "1", note->h_episema_above_connect,
781 note->h_episema_above_size);
782 }
783 }
784 write_note_heuristics(f, note);
785 if (note->texverb) {
786 fprintf(f, "[nv:");
787 gabc_print_string(f, gregorio_texverb(note->texverb));
788 fprintf(f, "]");
789 }
790 if (note->choral_sign) {
791 fprintf(f, "[cs:%s]", note->choral_sign);
792 }
793 if (note->shape_hint) {
794 fprintf(f, "[shape:%s]", note->shape_hint);
795 }
796 }
797
798 static void get_next_hepisema_adjustments(unsigned short *adjustment_index,
799 const gregorio_syllable *syllable, const gregorio_element *element,
800 const gregorio_glyph *glyph, const gregorio_note *note)
801 {
802 while (note) {
803 note = note->next;
804 if (!note) {
805 while (glyph) {
806 glyph = glyph->next;
807 if (!glyph) {
808 while (element) {
809 element = element->next;
810 if (!element) {
811 syllable = syllable->next_syllable;
812 if (syllable) {
813 element = syllable->elements[0];
814 }
815 }
816 if (element && element->type == GRE_ELEMENT) {
817 glyph = element->u.first_glyph;
818 break;
819 }
820 }
821 }
822 if (glyph && glyph->type == GRE_GLYPH) {
823 note = glyph->u.notes.first_note;
824 break;
825 }
826 }
827 }
828 if (note && note->type == GRE_NOTE) {
829 break;
830 }
831 }
832
833 if (note) {
834 adjustment_index[SO_OVER] = note->he_adjustment_index[SO_OVER];
835 adjustment_index[SO_UNDER] = note->he_adjustment_index[SO_UNDER];
836 } else {
837 adjustment_index[SO_OVER] = 0;
838 adjustment_index[SO_UNDER] = 0;
839 }
840 }
841
842 static __inline void emit_hepisema_adjustment(FILE *const f,
843 const gregorio_note *const note, const gregorio_sign_orientation index,
844 const char which, const bool open_brace)
845 {
846 gregorio_hepisema_adjustment *adj = gregorio_get_hepisema_adjustment(
847 note->he_adjustment_index[index]);
848
849 fprintf(f, "[%ch", which);
850 if (adj->vbasepos || adj->nudge) {
851 fputc(':', f);
852 switch (adj->vbasepos) {
853 case HVB_AUTO:
854 break;
855 case HVB_MIDDLE:
856 fputc('m', f);
857 break;
858 case HVB_O_LOW:
859 if (index == SO_OVER) {
860 fputc('l', f);
861 } else {
862 fprintf(f, "ol");
863 }
864 break;
865 case HVB_O_HIGH:
866 if (index == SO_OVER) {
867 fputc('h', f);
868 } else {
869 fprintf(f, "oh");
870 }
871 break;
872 case HVB_U_LOW:
873 if (index == SO_UNDER) {
874 fputc('l', f);
875 } else {
876 fprintf(f, "ul");
877 }
878 break;
879 case HVB_U_HIGH:
880 if (index == SO_UNDER) {
881 fputc('h', f);
882 } else {
883 fprintf(f, "uh");
884 }
885 break;
886 }
887 if (adj->nudge) {
888 fprintf(f, "%s", adj->nudge);
889 }
890 }
891 if (open_brace) {
892 fputc('{', f);
893 }
894 fputc(']', f);
895 }
896
897 static __inline void open_hepisema_adjustment(FILE *const f,
898 const gregorio_note *const note,
899 const unsigned short *const prev_adjustment_index,
900 const unsigned short *const next_adjustment_index,
901 const gregorio_sign_orientation index, const char which)
902 {
903 const unsigned short adjustment_index =
904 note->he_adjustment_index[index];
905
906 if (adjustment_index
907 && adjustment_index != prev_adjustment_index[index]
908 && adjustment_index == next_adjustment_index[index]) {
909 emit_hepisema_adjustment(f, note, index, which, true);
910 }
911 }
912
913 static __inline void close_hepisema_adjustment(FILE *const f,
914 const gregorio_note *const note,
915 const unsigned short *const prev_adjustment_index,
916 const unsigned short *const next_adjustment_index,
917 const gregorio_sign_orientation index, const char which)
918 {
919 const unsigned short adjustment_index =
920 note->he_adjustment_index[index];
921
922 if (adjustment_index) {
923 if (adjustment_index != next_adjustment_index[index]) {
924 if (adjustment_index == prev_adjustment_index[index]) {
925 fprintf(f, "[%ch}]", which);
926 } else {
927 emit_hepisema_adjustment(f, note, index, which, false);
928 }
929 }
930 }
931 }
932
933 /*
934 *
935 * The function that writes one glyph. If it is really a glyph (meaning not a
936 * space or an alteration), we just do like always, a loop on the notes and a
937 * call to the function that writes one note on each of them.
938 *
939 */
940
941 static void gabc_write_gregorio_glyph(FILE *f, gregorio_glyph *glyph,
942 glyph_context *context)
943 {
944 unsigned short next_adjustment_index[2] = { 0, 0 };
945 gregorio_note *current_note;
946
947 gregorio_assert(glyph, gabc_write_gregorio_glyph, "call with NULL argument",
948 return);
949 switch (glyph->type) {
950 case GRE_TEXVERB_GLYPH:
951 if (glyph->texverb) {
952 fprintf(f, "[gv:");
953 gabc_print_string(f, gregorio_texverb(glyph->texverb));
954 fprintf(f, "]");
955 }
956 break;
957 case GRE_SPACE:
958 if (glyph->next) {
959 switch (glyph->u.misc.unpitched.info.space) {
960 case SP_ZERO_WIDTH:
961 fprintf(f, "!");
962 break;
963 case SP_HALF_SPACE:
964 fprintf(f, "/0");
965 break;
966 case SP_INTERGLYPH_SPACE:
967 fprintf(f, "/!");
968 break;
969 default:
970 /* not reachable unless there's a programming error */
971 /* LCOV_EXCL_START */
972 unsupported("gabc_write_gregorio_glyph", __LINE__, "space type",
973 gregorio_space_to_string(
974 glyph->u.misc.unpitched.info.space));
975 break;
976 /* LCOV_EXCL_STOP */
977 }
978 } else {
979 /* not reachable unless there's a programming error */
980 /* LCOV_EXCL_START */
981 gregorio_fail(gabc_write_gregorio_glyph, "bad space");
982 /* LCOV_EXCL_STOP */
983 }
984 break;
985 case GRE_GLYPH:
986 if (is_initio_debilis(glyph->u.notes.liquescentia)) {
987 fprintf(f, "-");
988 } else if (is_fused(glyph->u.notes.liquescentia)) {
989 fprintf(f, "@");
990 }
991
992 current_note = glyph->u.notes.first_note;
993 while (current_note) {
994 get_next_hepisema_adjustments(next_adjustment_index,
995 context->syllable, context->element, glyph, current_note);
996
997 open_hepisema_adjustment(f, current_note,
998 context->he_adjustment_index, next_adjustment_index,
999 SO_OVER, 'o');
1000 open_hepisema_adjustment(f, current_note,
1001 context->he_adjustment_index, next_adjustment_index,
1002 SO_UNDER, 'u');
1003
1004 /* third argument necessary for the special shape pes quadratum */
1005 gabc_write_gregorio_note(f, current_note,
1006 glyph->u.notes.glyph_type == G_PES_QUADRATUM
1007 && current_note == glyph->u.notes.first_note);
1008
1009 close_hepisema_adjustment(f, current_note,
1010 context->he_adjustment_index, next_adjustment_index,
1011 SO_OVER, 'o');
1012 close_hepisema_adjustment(f, current_note,
1013 context->he_adjustment_index, next_adjustment_index,
1014 SO_UNDER, 'u');
1015
1016 context->he_adjustment_index[SO_OVER] =
1017 current_note->he_adjustment_index[SO_OVER];
1018 context->he_adjustment_index[SO_UNDER] =
1019 current_note->he_adjustment_index[SO_UNDER];
1020
1021 current_note = current_note->next;
1022 }
1023 gabc_write_end_liquescentia(f, glyph->u.notes.liquescentia);
1024 break;
1025 default:
1026 /* not reachable unless there's a programming error */
1027 /* LCOV_EXCL_START */
1028 unsupported("gabc_write_gregorio_glyph", __LINE__, "glyph type",
1029 gregorio_type_to_string(glyph->type));
1030 break;
1031 /* LCOV_EXCL_STOP */
1032 }
1033 }
1034
1035 /*
1036 *
1037 * To write an element, first we check the type of the element (if it is a bar,
1038 * etc.), and if it is really an element, we make a loop on the list of glyphs
1039 * inside the neume, and for each of them we call the function that will write
1040 * one glyph.
1041 *
1042 */
1043
1044 static void gabc_write_gregorio_element(FILE *f, gregorio_element *element,
1045 glyph_context *context)
1046 {
1047 gregorio_glyph *current_glyph;
1048 gregorio_assert(element, gabc_write_gregorio_element,
1049 "call with NULL argument", return);
1050 current_glyph = element->u.first_glyph;
1051 switch (element->type) {
1052 case GRE_ELEMENT:
1053 while (current_glyph) {
1054 gabc_write_gregorio_glyph(f, current_glyph, context);
1055 current_glyph = current_glyph->next;
1056 }
1057 break;
1058 case GRE_TEXVERB_ELEMENT:
1059 if (element->texverb) {
1060 fprintf(f, "[ev:");
1061 gabc_print_string(f, gregorio_texverb(element->texverb));
1062 fprintf(f, "]");
1063 }
1064 break;
1065 case GRE_ALT:
1066 if (element->texverb) {
1067 fprintf(f, "[alt:");
1068 gabc_print_string(f, gregorio_texverb(element->texverb));
1069 fprintf(f, "]");
1070 }
1071 break;
1072 case GRE_SPACE:
1073 gabc_write_space(f, element->u.misc.unpitched.info.space,
1074 element->u.misc.unpitched.info.ad_hoc_space_factor,
1075 element->next && element->next->type == GRE_SPACE);
1076 break;
1077 case GRE_BAR:
1078 gabc_write_bar(f, element->u.misc.unpitched.info.bar);
1079 gabc_write_bar_signs(f, element->u.misc.unpitched.special_sign);
1080 break;
1081 case GRE_CLEF:
1082 gabc_write_clef(f, element->u.misc.clef);
1083 break;
1084 case GRE_END_OF_LINE:
1085 if (element->u.misc.unpitched.info.eol_ragged) {
1086 fprintf(f, "Z");
1087 } else {
1088 fprintf(f, "z");
1089 }
1090 if (element->u.misc.unpitched.info.eol_forces_custos) {
1091 fprintf(f, element->u.misc.unpitched.info.eol_forces_custos_on? "+"
1092 : "-");
1093 }
1094 break;
1095 case GRE_CUSTOS:
1096 if (element->u.misc.pitched.force_pitch) {
1097 fprintf(f, "%c+", pitch_letter(element->u.misc.pitched.pitch));
1098 } else {
1099 fprintf(f, "z0");
1100 }
1101 break;
1102 case GRE_SUPPRESS_CUSTOS:
1103 fprintf(f, "[nocustos]");
1104 break;
1105 case GRE_NLBA:
1106 switch (element->u.misc.unpitched.info.nlba) {
1107 case NLBA_BEGINNING:
1108 fprintf(f, "<nlba>");
1109 break;
1110 case NLBA_END:
1111 fprintf(f, "</nlba>");
1112 break;
1113 default:
1114 /* not reachable unless there's a programming error */
1115 /* LCOV_EXCL_START */
1116 unsupported("gabc_write_gregorio_element", __LINE__, "nlba type",
1117 gregorio_nlba_to_string(element->u.misc.unpitched.info.nlba));
1118 break;
1119 /* LCOV_EXCL_STOP */
1120 }
1121 break;
1122 default:
1123 /* not reachable unless there's a programming error */
1124 /* LCOV_EXCL_START */
1125 unsupported("gabc_write_gregorio_element", __LINE__, "element type",
1126 gregorio_type_to_string(element->type));
1127 break;
1128 /* LCOV_EXCL_STOP */
1129 }
1130 }
1131
1132 /*
1133 *
1134 * Here is defined the function that will write the list of gregorio_elements.
1135 * It is very simple: it makes a loop in which it calls a function that writes
1136 * one element.
1137 *
1138 */
1139
1140 static bool gabc_write_gregorio_elements(FILE *f, gregorio_element *element,
1141 glyph_context *context)
1142 {
1143 bool linebreak_or_bar_in_element = false;
1144 while (element) {
1145 context->element = element;
1146 gabc_write_gregorio_element(f, element, context);
1147 /* we don't want a bar after an end of line */
1148 if (element->type != GRE_END_OF_LINE
1149 && (element->type != GRE_SPACE
1150 || element->u.misc.unpitched.info.space == SP_NEUMATIC_CUT)
1151 && element->next && element->next->type == GRE_ELEMENT) {
1152 fprintf(f, "/");
1153 }
1154 if (element->type == GRE_END_OF_LINE || element->type == GRE_BAR)
1155 {
1156 linebreak_or_bar_in_element = true;
1157 }
1158 element = element->next;
1159 }
1160 return linebreak_or_bar_in_element;
1161 }
1162
1163 /*
1164 *
1165 * Here it goes, we are writing a gregorio_syllable.
1166 *
1167 */
1168
1169 static void gabc_write_gregorio_syllable(FILE *f, gregorio_syllable *syllable,
1170 glyph_context *context)
1171 {
1172 bool linebreak_or_bar_in_element;
1173 gregorio_assert(syllable, gabc_write_gregorio_syllable,
1174 "call with NULL argument", return);
1175 write_state = GABC_NORMAL;
1176 if (syllable->no_linebreak_area == NLBA_BEGINNING) {
1177 fprintf(f, "<nlba>");
1178 }
1179 if (syllable->euouae == EUOUAE_BEGINNING) {
1180 fprintf(f, "<eu>");
1181 }
1182 if (syllable->clear) {
1183 fprintf(f, "<clear>");
1184 }
1185 if (syllable->text) {
1186 /* we call the magic function (defined in struct_utils.c), that will
1187 * write our text. */
1188 gregorio_write_text(WTP_NORMAL, syllable->text, f, &gabc_write_verb,
1189 &gabc_print_char, &gabc_write_begin, &gabc_write_end,
1190 &gabc_write_special_char);
1191 }
1192 if (syllable->translation) {
1193 fprintf(f, "[");
1194 gregorio_write_text(WTP_NORMAL, syllable->translation, f,
1195 &gabc_write_verb, &gabc_print_char, &gabc_write_begin,
1196 &gabc_write_end, &gabc_write_special_char);
1197 fprintf(f, "]");
1198 } else if (syllable->translation_type == TR_WITH_CENTER_END) {
1199 fprintf(f, "[/]");
1200 }
1201 if (syllable->euouae == EUOUAE_END) {
1202 fprintf(f, "</eu>");
1203 }
1204 if (syllable->no_linebreak_area == NLBA_END) {
1205 fprintf(f, "</nlba>");
1206 }
1207 fprintf(f, "(");
1208 /* we write all the elements of the syllable. */
1209 linebreak_or_bar_in_element = gabc_write_gregorio_elements(f, syllable->elements[0], context);
1210 if (linebreak_or_bar_in_element)
1211 {
1212 fprintf(f, ")\n");
1213 } else {
1214 if (syllable->position == WORD_END
1215 || syllable->position == WORD_ONE_SYLLABLE
1216 || gregorio_is_only_special(syllable->elements[0]))
1217 {
1218 fprintf(f, ") ");
1219 } else {
1220 fprintf(f, ")");
1221 }
1222 }
1223 }
1224
1225 /*
1226 *
1227 * This is the top function, the one called when we want to write a
1228 * gregorio_score in gabc.
1229 *
1230 */
1231
1232 void gabc_write_score(FILE *f, gregorio_score *score)
1233 {
1234 glyph_context context;
1235 gregorio_syllable *syllable;
1236 gregorio_header *header;
1237
1238 gregorio_assert(f, gabc_write_score, "call with NULL file", return);
1239
1240 context.he_adjustment_index[0] = 0;
1241 context.he_adjustment_index[1] = 0;
1242
1243 for (header = score->headers; header; header = header->next) {
1244 gabc_write_str_attribute(f, header->name, header->value);
1245 }
1246 /* And since the gabc is generated by this program, note this. */
1247 fprintf(f, "generated-by: %s %s;\n", "gregorio", GREGORIO_VERSION);
1248 gregorio_assert(score->number_of_voices == 1, gabc_write_score,
1249 "gregorio_score seems to be empty", return);
1250 fprintf(f, "%%%%\n");
1251 /* at present we only allow for one clef at the start of the gabc */
1252 if (score->first_voice_info) {
1253 fprintf(f, "(");
1254 gabc_write_clef(f, score->first_voice_info->initial_clef);
1255 fprintf(f, ")");
1256 }
1257 syllable = score->first_syllable;
1258 /* the we write every syllable */
1259 while (syllable) {
1260 context.syllable = syllable;
1261 gabc_write_gregorio_syllable(f, syllable, &context);
1262 syllable = syllable->next_syllable;
1263 }
1264 fprintf(f, "\n");
1265 }
1266
1267 /* And that's it... not really hard isn't it? */