Debian Codegraph @

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

raw
1 /*
2 * Gregorio is a program that translates gabc files to GregorioTeX
3 * This file contains functions for writing GregorioTeX from Gregorio structures.
4 *
5 * Copyright (C) 2008-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 /**
23 * @file
24 * @brief The plugin which writes a GregorioTeX score.
25 */
26
27 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <time.h>
33 #include "bool.h"
34 #include "struct.h"
35 #include "unicode.h"
36 #include "messages.h"
37 #include "characters.h"
38 #include "plugins.h"
39 #include "support.h"
40 #include "utf8strings.h"
41
42 #include "gregoriotex.h"
43
44 #define BUFSIZE 128
45
46 /* a structure containing the status */
47 typedef struct gregoriotex_status {
48 bool point_and_click;
49
50 /* true if the current_glyph will have an additional line under or not
51 * (useful to determine the length of the bar in case of a flexa starting
52 * at d */
53 bool bottom_line;
54
55 signed char top_height;
56 signed char bottom_height;
57
58 /* indicates if there is a translation on the line */
59 bool translation;
60
61 /* indicates if there is "above lines text" on the line */
62 bool abovelinestext;
63
64 bool suppressed_custos;
65 } gregoriotex_status;
66
67 #define UNDETERMINED_HEIGHT -127
68
69 #define MAX_AMBITUS 5
70 static const char *tex_ambitus[] = {
71 "", "One", "Two", "Three", "Four", "Five"
72 };
73
74 #define SHAPE(NAME) static const char *const SHAPE_##NAME = #NAME
75 SHAPE(Ancus);
76 SHAPE(AncusLongqueue);
77 SHAPE(AscendensOriscus);
78 SHAPE(AscendensOriscusLineBL);
79 SHAPE(AscendensOriscusLineTL);
80 SHAPE(AscendensOriscusScapus);
81 SHAPE(AscendensOriscusScapusLongqueue);
82 SHAPE(AscendensOriscusScapusOpenqueue);
83 SHAPE(AscendensPunctumInclinatum);
84 SHAPE(DescendensOriscus);
85 SHAPE(DescendensOriscusLineBL);
86 SHAPE(DescendensOriscusLineTL);
87 SHAPE(DescendensOriscusScapus);
88 SHAPE(DescendensOriscusScapusLongqueue);
89 SHAPE(DescendensOriscusScapusOpenqueue);
90 SHAPE(DescendensPunctumInclinatum);
91 SHAPE(Flat);
92 SHAPE(FlatParen);
93 SHAPE(FlatSoft);
94 SHAPE(Flexus);
95 SHAPE(FlexusLongqueue);
96 SHAPE(FlexusNobar);
97 SHAPE(FlexusOpenqueue);
98 SHAPE(FlexusOriscus);
99 SHAPE(FlexusOriscusInusitatus);
100 SHAPE(FlexusOriscusScapus);
101 SHAPE(FlexusOriscusScapusInusitatus);
102 SHAPE(FlexusOriscusScapusInusitatusLongqueue);
103 SHAPE(FlexusOriscusScapusInusitatusOpenqueue);
104 SHAPE(FlexusOriscusScapusLongqueue);
105 SHAPE(FlexusOriscusScapusOpenqueue);
106 SHAPE(Linea);
107 SHAPE(LineaPunctum);
108 SHAPE(Natural);
109 SHAPE(NaturalParen);
110 SHAPE(NaturalSoft);
111 SHAPE(OriscusDeminutus);
112 SHAPE(Pes);
113 SHAPE(PesAscendensOriscus);
114 SHAPE(PesDescendensOriscus);
115 SHAPE(PesQuadratum);
116 SHAPE(PesQuadratumLongqueue);
117 SHAPE(PesQuadratumOpenqueue);
118 SHAPE(PesQuassus);
119 SHAPE(PesQuassusInusitatus);
120 SHAPE(PesQuassusInusitatusLongqueue);
121 SHAPE(PesQuassusInusitatusOpenqueue);
122 SHAPE(PesQuassusLongqueue);
123 SHAPE(PesQuassusOpenqueue);
124 SHAPE(Porrectus);
125 SHAPE(PorrectusFlexus);
126 SHAPE(PorrectusFlexusLongqueue);
127 SHAPE(PorrectusFlexusNobar);
128 SHAPE(PorrectusLongqueue);
129 SHAPE(PorrectusNobar);
130 SHAPE(Punctum);
131 SHAPE(PunctumAscendens);
132 SHAPE(PunctumDeminutus);
133 SHAPE(PunctumDescendens);
134 SHAPE(PunctumInclinatumAuctus);
135 SHAPE(PunctumInclinatumDeminutus);
136 SHAPE(PunctumLineBL);
137 SHAPE(PunctumLineTL);
138 SHAPE(Quilisma);
139 SHAPE(QuilismaPes);
140 SHAPE(QuilismaPesQuadratum);
141 SHAPE(QuilismaPesQuadratumLongqueue);
142 SHAPE(QuilismaPesQuadratumOpenqueue);
143 SHAPE(StansPunctumInclinatum);
144 SHAPE(Salicus);
145 SHAPE(SalicusFlexus);
146 SHAPE(SalicusLongqueue);
147 SHAPE(Scandicus);
148 SHAPE(Sharp);
149 SHAPE(SharpParen);
150 SHAPE(SharpSoft);
151 SHAPE(Stropha);
152 SHAPE(StrophaAucta);
153 SHAPE(StrophaAuctaLongtail);
154 SHAPE(Torculus);
155 SHAPE(TorculusLiquescens);
156 SHAPE(TorculusLiquescensQuilisma);
157 SHAPE(TorculusQuilisma);
158 SHAPE(TorculusResupinus);
159 SHAPE(TorculusResupinusQuilisma);
160 SHAPE(Virga);
161 SHAPE(VirgaLongqueue);
162 SHAPE(VirgaOpenqueue);
163 SHAPE(VirgaReversa);
164 SHAPE(VirgaReversaAscendens);
165 SHAPE(VirgaReversaDescendens);
166 SHAPE(VirgaReversaLongqueue);
167 SHAPE(VirgaReversaLongqueueAscendens);
168 SHAPE(VirgaReversaLongqueueDescendens);
169 SHAPE(VirgaReversaOpenqueue);
170 SHAPE(VirgaReversaOpenqueueAscendens);
171 SHAPE(VirgaReversaOpenqueueDescendens);
172
173 #define LIQ(NAME) static const char *const LIQ_##NAME = #NAME
174 LIQ(Deminutus);
175 LIQ(Ascendens);
176 LIQ(Descendens);
177 LIQ(InitioDebilis);
178 LIQ(InitioDebilisDeminutus);
179 LIQ(InitioDebilisAscendens);
180 LIQ(InitioDebilisDescendens);
181 LIQ(Nothing);
182
183 #define FUSE(NAME) static const char *const FUSE_##NAME = #NAME
184 FUSE(Lower);
185 FUSE(LowerOblatus);
186 FUSE(Upper);
187 FUSE(UpperOblatus);
188 FUSE(Down);
189 FUSE(Up);
190
191 /* the value indicating to GregorioTeX that there is no flat */
192 #define NO_KEY_FLAT LOWEST_PITCH
193
194 static __inline signed char pitch_value(const signed char height) {
195 /* right now height == pitch, but this function allows us to change
196 * the offset easily */
197 return height;
198 }
199
200 static __inline int bool_to_int(bool value) {
201 return value? 1 : 0;
202 }
203
204 typedef enum queuetype {
205 Q_ON_SPACE_BELOW_BOTTOM_LINE,
206 Q_ON_SPACE_ABOVE_BOTTOM_LINE,
207 Q_ON_BOTTOM_LINE,
208 Q_ON_LINE_ABOVE_BOTTOM_LINE
209 } queuetype;
210
211 static queuetype adjusted_queuetype_of(const gregorio_note *const note,
212 const gregorio_note *const queue_note, const signed char adjustment)
213 {
214 switch (note->u.note.pitch + adjustment - LOWEST_PITCH) {
215 case 0:
216 return Q_ON_SPACE_BELOW_BOTTOM_LINE;
217 case 1:
218 return Q_ON_BOTTOM_LINE;
219 case 2:
220 return queue_note->low_ledger_line?
221 Q_ON_SPACE_ABOVE_BOTTOM_LINE : Q_ON_SPACE_BELOW_BOTTOM_LINE;
222 case 3:
223 return queue_note->low_ledger_line?
224 Q_ON_LINE_ABOVE_BOTTOM_LINE : Q_ON_BOTTOM_LINE;
225 case 5:
226 case 7:
227 case 9:
228 case 11:
229 case 13:
230 return Q_ON_LINE_ABOVE_BOTTOM_LINE;
231 default:
232 return Q_ON_SPACE_ABOVE_BOTTOM_LINE;
233 }
234 }
235
236 static queuetype queuetype_of(const gregorio_note *const note) {
237 return adjusted_queuetype_of(note, note, 0);
238 }
239
240 static grestyle_style gregoriotex_ignore_style = ST_NO_STYLE;
241 static grestyle_style gregoriotex_next_ignore_style = ST_NO_STYLE;
242
243 static bool glyph_hint(const gregorio_glyph *const glyph,
244 const char *const hint) {
245 const char *const shape_hints = gregorio_glyph_last_note(glyph)->shape_hint;
246 if (shape_hints == NULL) {
247 /* no hints */
248 return false;
249 } else {
250 const char *const found = strstr(shape_hints, hint);
251 if (found == NULL) {
252 /* no match */
253 return false;
254 } else {
255 const char suffix = found[strlen(hint)];
256 if (suffix == '\0' || suffix == ',') {
257 /* one must be true or it's not a match */
258 if (found == shape_hints) {
259 /* found it at the start of shape_hints */
260 return true;
261 } else {
262 /*
263 * we're somewhere after the start, so it's ok to get the
264 * character before; a comma before the start of the found
265 * string is the only remaining way it can be a match
266 */
267 return (*(found - 1) == ',');
268 }
269 } else {
270 return false;
271 }
272 }
273 }
274 }
275
276 /*
277 * The different liquescentiae are:
278 * 'Nothing'
279 * 'InitioDebilis'
280 * 'Deminutus
281 * 'Ascendens'
282 * 'Descendens'
283 * 'InitioDebilisDeminutus'
284 * 'InitioDebilisAscendens'
285 * 'InitioDebilisDescendens'
286 *
287 * They also are and must be the same as in squarize.py.
288 */
289
290 static const char *gregoriotex_determine_liquescentia(
291 gtex_glyph_liquescentia type, const gregorio_glyph *const glyph)
292 {
293 gregorio_liquescentia liquescentia = glyph->u.notes.liquescentia;
294
295 switch (type) {
296 case LG_ALL:
297 break;
298 case LG_NO_INITIO:
299 liquescentia &= ~L_INITIO_DEBILIS;
300 break;
301 case LG_ONLY_DEMINUTUS:
302 liquescentia &= L_INITIO_DEBILIS | L_DEMINUTUS;
303 break;
304 case LG_FUSIBLE_INITIO:
305 if (glyph->u.notes.fuse_to_next_glyph) {
306 liquescentia &= L_INITIO_DEBILIS;
307 break;
308 }
309 /* fall through */
310 case LG_NONE:
311 liquescentia = L_NO_LIQUESCENTIA;
312 break;
313 }
314
315 /* now we convert liquescentia into the good GregorioTeX liquescentia
316 * numbers */
317
318 switch (liquescentia & ~L_FUSED) {
319 case L_DEMINUTUS:
320 return LIQ_Deminutus;
321 case L_AUCTUS_ASCENDENS:
322 return LIQ_Ascendens;
323 case L_AUCTUS_DESCENDENS:
324 return LIQ_Descendens;
325 case L_INITIO_DEBILIS:
326 return LIQ_InitioDebilis;
327 case L_DEMINUTUS_INITIO_DEBILIS:
328 return LIQ_InitioDebilisDeminutus;
329 case L_AUCTUS_ASCENDENS_INITIO_DEBILIS:
330 return LIQ_InitioDebilisAscendens;
331 case L_AUCTUS_DESCENDENS_INITIO_DEBILIS:
332 return LIQ_InitioDebilisDescendens;
333 case L_NO_LIQUESCENTIA:
334 /* break out and return "Nothing" */
335 break;
336 }
337
338 return LIQ_Nothing;
339 }
340
341 static __inline int compute_ambitus(const gregorio_note *const current_note)
342 {
343 int first = current_note->u.note.pitch;
344 int second = current_note->next->u.note.pitch;
345 int ambitus;
346 if (first < second) {
347 ambitus = second - first;
348 } else {
349 ambitus = first - second;
350 }
351 gregorio_assert2(ambitus >= 1 && ambitus <= MAX_AMBITUS, compute_ambitus,
352 "unsupported ambitus: %d", ambitus, return 0);
353 return ambitus;
354 }
355
356 static const char *compute_glyph_name(const gregorio_glyph *const glyph,
357 const char *shape, const gtex_glyph_liquescentia ltype,
358 bool is_single_note)
359 {
360 static char buf[BUFSIZE];
361
362 const char *liquescentia = gregoriotex_determine_liquescentia(ltype, glyph);
363 const gregorio_note *current_note;
364 int ambitus1, ambitus2, ambitus3, fuse_ambitus = 0;
365 const char *fuse_head = "", *fuse_tail = "";
366 const gregorio_glyph *previous = gregorio_previous_non_texverb_glyph(glyph);
367 int fuse_to_next_note, fuse_from_previous_note =
368 (previous && previous->type == GRE_GLYPH)
369 ? previous->u.notes.fuse_to_next_glyph : 0;
370
371 /* then we start making our formula */
372 gregorio_assert(glyph, compute_glyph_name, "called with NULL pointer",
373 return "");
374 gregorio_assert(glyph->u.notes.first_note, compute_glyph_name,
375 "called with a glyph that have no note", return "");
376
377 fuse_to_next_note = glyph->u.notes.fuse_to_next_glyph;
378
379 /*
380 * Note: due to the way auto fusion is computed, a virga inside auto fusion
381 * will have glyph type G_PUNCTUM and note shape S_VIRGA here. However, a
382 * fused virga outside auto fusion will have glyph type G_VIRGA.
383 */
384 switch (glyph->u.notes.glyph_type) {
385 case G_PODATUS:
386 case G_PUNCTUM:
387 case G_FLEXA:
388 case G_VIRGA:
389 /* directionally head-fusible */
390 if (fuse_from_previous_note < 0) {
391 if (glyph->u.notes.first_note->u.note.shape != S_QUILISMA
392 && glyph->u.notes.first_note->u.note.shape
393 != S_QUILISMA_QUADRATUM
394 && glyph->u.notes.first_note->u.note.shape != S_VIRGA) {
395 if (fuse_from_previous_note < -1) {
396 fuse_head = FUSE_Lower;
397 } else if (glyph->u.notes.first_note->u.note.shape
398 == S_ORISCUS_DESCENDENS
399 || glyph->u.notes.first_note->u.note.shape
400 == S_ORISCUS_SCAPUS_DESCENDENS) {
401 /* fuse_from_previous_note will be -1 here */
402 fuse_head = FUSE_LowerOblatus;
403 }
404 }
405 } else if (fuse_from_previous_note > 0) {
406 if (fuse_from_previous_note > 1) {
407 fuse_head = FUSE_Upper;
408 } else if (glyph->u.notes.first_note->u.note.shape
409 == S_ORISCUS_ASCENDENS
410 || glyph->u.notes.first_note->u.note.shape
411 == S_ORISCUS_SCAPUS_ASCENDENS) {
412 /* fuse_from_previous_note will be 1 here */
413 fuse_head = FUSE_UpperOblatus;
414 }
415 }
416 break;
417
418 default:
419 /* not directionally head-fusible */
420 break;
421 }
422
423 switch (glyph->u.notes.glyph_type) {
424 case G_FLEXA:
425 if (fuse_to_next_note <= 0) {
426 /* a flexa is only fusible up */
427 break;
428 }
429 /* else fall through */
430 case G_VIRGA_REVERSA:
431 case G_PUNCTUM:
432 if (glyph->u.notes.first_note->u.note.shape != S_VIRGA) {
433 /* tail-fusible */
434 if (fuse_to_next_note < 0) {
435 fuse_tail = FUSE_Down;
436 fuse_ambitus = -fuse_to_next_note;
437 } else if (fuse_to_next_note > 0) {
438 fuse_tail = FUSE_Up;
439 fuse_ambitus = fuse_to_next_note;
440 }
441
442 if (*fuse_tail && liquescentia == LIQ_Nothing) {
443 liquescentia = "";
444 }
445 }
446 break;
447
448 default:
449 /* not tail-fusible */
450 break;
451 }
452
453 if (is_fused(glyph->u.notes.liquescentia)) {
454 if (shape == SHAPE_AscendensOriscusScapus
455 || shape == SHAPE_AscendensOriscusScapusLongqueue
456 || shape == SHAPE_AscendensOriscusScapusOpenqueue) {
457 shape = SHAPE_AscendensOriscus;
458 } else if (shape == SHAPE_DescendensOriscusScapus
459 || shape == SHAPE_DescendensOriscusScapusLongqueue
460 || shape == SHAPE_DescendensOriscusScapusOpenqueue) {
461 shape = SHAPE_DescendensOriscus;
462 } else if (shape == SHAPE_FlexusOriscusScapus
463 || shape == SHAPE_FlexusOriscusScapusLongqueue
464 || shape == SHAPE_FlexusOriscusScapusOpenqueue) {
465 shape = SHAPE_FlexusOriscus;
466 } else if (shape == SHAPE_FlexusOriscusScapusInusitatus
467 || shape == SHAPE_FlexusOriscusScapusInusitatusLongqueue
468 || shape == SHAPE_FlexusOriscusScapusInusitatusOpenqueue) {
469 shape = SHAPE_FlexusOriscusInusitatus;
470 }
471 }
472
473 current_note = glyph->u.notes.first_note;
474 if (is_single_note) {
475 if (liquescentia == LIQ_Nothing) {
476 liquescentia = "";
477 }
478 if (!(*fuse_tail) && fuse_head != FUSE_UpperOblatus
479 && fuse_head != FUSE_LowerOblatus) {
480 /* single note fused shapes have weird names */
481 if (fuse_head == FUSE_Upper) {
482 if (shape == SHAPE_Punctum) {
483 shape = SHAPE_PunctumLineBL;
484 } else if (shape == SHAPE_AscendensOriscus) {
485 shape = SHAPE_AscendensOriscusLineBL;
486 } else if (shape == SHAPE_DescendensOriscus) {
487 shape = SHAPE_DescendensOriscusLineBL;
488 }
489 } else if (fuse_head == FUSE_Lower) {
490 if (shape == SHAPE_Punctum) {
491 shape = SHAPE_PunctumLineTL;
492 } else if (shape == SHAPE_AscendensOriscus) {
493 shape = SHAPE_AscendensOriscusLineTL;
494 } else if (shape == SHAPE_DescendensOriscus) {
495 shape = SHAPE_DescendensOriscusLineTL;
496 }
497 }
498 if (shape != SHAPE_Virga && shape != SHAPE_VirgaLongqueue &&
499 shape != SHAPE_VirgaOpenqueue) {
500 fuse_head = "";
501 }
502 }
503 gregorio_snprintf(buf, BUFSIZE, "%s%s%s%s%s", fuse_head, shape,
504 tex_ambitus[fuse_ambitus], liquescentia, fuse_tail);
505 return buf;
506 }
507 gregorio_assert(current_note->next, compute_glyph_name,
508 "called with a multi-note glyph that has only one note", return "");
509 gregorio_assert((ambitus1 = compute_ambitus(current_note)),
510 compute_glyph_name, "unexpected unison on multi-note glyph",
511 return "");
512 if (is_fused(glyph->u.notes.liquescentia)) {
513 if (shape == SHAPE_Flexus || shape == SHAPE_FlexusLongqueue
514 || shape == SHAPE_FlexusOpenqueue) {
515 if (fuse_to_next_note) {
516 fuse_head = "";
517 }
518 if (*fuse_head) {
519 shape = SHAPE_Flexus;
520 } else {
521 shape = SHAPE_FlexusNobar;
522 }
523 } else if (shape == SHAPE_Porrectus
524 || shape == SHAPE_PorrectusLongqueue) {
525 shape = SHAPE_PorrectusNobar;
526 }
527 } else {
528 if (fuse_to_next_note && (shape == SHAPE_FlexusLongqueue
529 || shape == SHAPE_FlexusOpenqueue)) {
530 /* a porrectus-like flexus has no longqueue variant */
531 shape = SHAPE_Flexus;
532 }
533 }
534 if (shape == SHAPE_SalicusLongqueue && liquescentia != LIQ_Nothing) {
535 /* the salicus queue is at the end of the glyph, and it doesn't exist
536 * for the liquescent forms */
537 shape = SHAPE_Salicus;
538 }
539 if (!fuse_to_next_note && (shape == SHAPE_Flexus
540 || shape == SHAPE_FlexusLongqueue
541 || shape == SHAPE_FlexusOpenqueue
542 || shape == SHAPE_FlexusNobar)
543 && liquescentia == LIQ_Nothing
544 && glyph_hint(glyph, "stroke")) {
545 /* "fake" fusion to get the shape we want */
546 fuse_ambitus = 1;
547 liquescentia = "";
548 fuse_tail = FUSE_Up;
549 }
550 current_note = current_note->next;
551 if (!current_note->next) {
552 gregorio_snprintf(buf, BUFSIZE, "%s%s%s%s%s%s", fuse_head, shape,
553 tex_ambitus[ambitus1], tex_ambitus[fuse_ambitus],
554 liquescentia, fuse_tail);
555 return buf;
556 }
557 gregorio_assert((ambitus2 = compute_ambitus(current_note)),
558 compute_glyph_name, "unexpected unison on multi-note glyph",
559 return "");
560 current_note = current_note->next;
561 if (!current_note->next) {
562 gregorio_snprintf(buf, BUFSIZE, "%s%s%s%s%s%s%s", fuse_head, shape,
563 tex_ambitus[ambitus1], tex_ambitus[ambitus2],
564 tex_ambitus[fuse_ambitus], liquescentia, fuse_tail);
565 return buf;
566 }
567 gregorio_assert((ambitus3 = compute_ambitus(current_note)),
568 compute_glyph_name, "unexpected unison on multi-note glyph",
569 return "");
570 gregorio_snprintf(buf, BUFSIZE, "%s%s%s%s%s%s%s%s", fuse_head, shape,
571 tex_ambitus[ambitus1], tex_ambitus[ambitus2], tex_ambitus[ambitus3],
572 tex_ambitus[fuse_ambitus], liquescentia, fuse_tail);
573 return buf;
574 }
575
576 static const char *fusible_queued_shape(const gregorio_note *const note,
577 const gregorio_glyph *const glyph,
578 const char *const base_shape, const char *const longqueue_shape,
579 const char *const openqueue_shape)
580 {
581 const char *name = NULL;
582 if (glyph->u.notes.fuse_to_next_glyph < 0) {
583 /* queue size depends on the following note if fused down */
584 bool ambitus_one = (glyph->u.notes.fuse_to_next_glyph == -1);
585 switch (adjusted_queuetype_of(note, note,
586 glyph->u.notes.fuse_to_next_glyph)) {
587 case Q_ON_SPACE_BELOW_BOTTOM_LINE:
588 if (ambitus_one) {
589 name = openqueue_shape;
590 break;
591 }
592 /* else fall through */
593 case Q_ON_SPACE_ABOVE_BOTTOM_LINE:
594 /* at ambitus one, long and short are swapped becuase the queue where
595 * the second note is on a space is longer than on a line */
596 name = ambitus_one? longqueue_shape : base_shape;
597 break;
598 case Q_ON_BOTTOM_LINE:
599 if (ambitus_one) {
600 name = openqueue_shape;
601 break;
602 }
603 /* else fall through */
604 case Q_ON_LINE_ABOVE_BOTTOM_LINE:
605 /* at ambitus one, long and short are swapped becuase the queue where
606 * the second note is on a line is shorter than on a space */
607 name = ambitus_one? base_shape : longqueue_shape;
608 break;
609 }
610 } else if (glyph->u.notes.fuse_to_next_glyph) {
611 /* TODO (5.0?) handle queue size on upwards fusion */
612 switch (queuetype_of(note)) {
613 case Q_ON_SPACE_BELOW_BOTTOM_LINE:
614 case Q_ON_SPACE_ABOVE_BOTTOM_LINE:
615 case Q_ON_BOTTOM_LINE:
616 name = base_shape;
617 break;
618 case Q_ON_LINE_ABOVE_BOTTOM_LINE:
619 name = longqueue_shape;
620 break;
621 }
622 } else {
623 switch (queuetype_of(note)) {
624 case Q_ON_SPACE_ABOVE_BOTTOM_LINE:
625 name = base_shape;
626 break;
627 case Q_ON_SPACE_BELOW_BOTTOM_LINE:
628 case Q_ON_BOTTOM_LINE:
629 name = openqueue_shape;
630 break;
631 case Q_ON_LINE_ABOVE_BOTTOM_LINE:
632 name = longqueue_shape;
633 break;
634 }
635 }
636 gregorio_not_null(name, fusible_queued_shape, return base_shape);
637 return compute_glyph_name(glyph, name, LG_NONE, true);
638 }
639
640 static __inline char *code_point(const char *const shape, const bool is_cavum,
641 char *const buf, const size_t bufsize)
642 {
643 if (is_cavum) {
644 gregorio_snprintf(buf, bufsize, "\\GreCavum{%s}", shape);
645 } else {
646 gregorio_snprintf(buf, bufsize, "\\GreCP%s", shape);
647 }
648 return buf;
649 }
650
651 static const char *determine_note_glyph_name(const gregorio_note *const note,
652 const gregorio_glyph *glyph, gtex_alignment *type)
653 {
654 static char buf[128], cpbuf[96];
655 const char *name = "";
656
657 gregorio_assert(note, determine_note_glyph_name, "called with NULL pointer",
658 return "");
659
660 *type = AT_ONE_NOTE;
661 switch (note->u.note.shape) {
662 case S_PUNCTUM_INCLINATUM_ASCENDENS:
663 *type = AT_PUNCTUM_INCLINATUM;
664 return SHAPE_AscendensPunctumInclinatum;
665 case S_PUNCTUM_INCLINATUM_STANS:
666 *type = AT_PUNCTUM_INCLINATUM;
667 return SHAPE_StansPunctumInclinatum;
668 case S_PUNCTUM_INCLINATUM_DESCENDENS:
669 *type = AT_PUNCTUM_INCLINATUM;
670 return SHAPE_DescendensPunctumInclinatum;
671 case S_PUNCTUM_INCLINATUM_DEMINUTUS:
672 return SHAPE_PunctumInclinatumDeminutus;
673 case S_PUNCTUM_INCLINATUM_AUCTUS:
674 return SHAPE_PunctumInclinatumAuctus;
675 case S_PUNCTUM:
676 return compute_glyph_name(glyph, SHAPE_Punctum, LG_FUSIBLE_INITIO, true);
677 case S_PUNCTUM_AUCTUS_ASCENDENS:
678 return SHAPE_PunctumAscendens;
679 case S_PUNCTUM_AUCTUS_DESCENDENS:
680 return SHAPE_PunctumDescendens;
681 case S_PUNCTUM_DEMINUTUS:
682 return SHAPE_PunctumDeminutus;
683 case S_LINEA:
684 return SHAPE_Linea;
685 case S_LINEA_PUNCTUM:
686 return SHAPE_LineaPunctum;
687 case S_VIRGA:
688 return fusible_queued_shape(note, glyph, SHAPE_Virga,
689 SHAPE_VirgaLongqueue, SHAPE_VirgaOpenqueue);
690 /*
691 switch (queuetype_of(note)) {
692 case Q_ON_SPACE_ABOVE_BOTTOM_LINE:
693 return compute_glyph_name(glyph, SHAPE_Virga, LG_NONE, true);
694 case Q_ON_SPACE_BELOW_BOTTOM_LINE:
695 case Q_ON_BOTTOM_LINE:
696 return compute_glyph_name(glyph, SHAPE_VirgaOpenqueue, LG_NONE,
697 true);
698 case Q_ON_LINE_ABOVE_BOTTOM_LINE:
699 return compute_glyph_name(glyph, SHAPE_VirgaLongqueue, LG_NONE,
700 true);
701 }*/ /* all cases return, so this line is not hit; LCOV_EXCL_LINE */
702 /* LCOV_EXCL_START */
703 gregorio_fail2(determine_note_glyph_name, "unknown queuetype: %d",
704 queuetype_of(note));
705 return "";
706 /* LCOV_EXCL_STOP */
707 case S_VIRGA_REVERSA:
708 switch (note->u.note.liquescentia) {
709 case L_AUCTUS_ASCENDENS:
710 switch (queuetype_of(note)) {
711 case Q_ON_SPACE_BELOW_BOTTOM_LINE:
712 case Q_ON_SPACE_ABOVE_BOTTOM_LINE:
713 name = SHAPE_VirgaReversaAscendens;
714 break;
715 case Q_ON_BOTTOM_LINE:
716 name = SHAPE_VirgaReversaOpenqueueAscendens;
717 break;
718 case Q_ON_LINE_ABOVE_BOTTOM_LINE:
719 name = SHAPE_VirgaReversaLongqueueAscendens;
720 break;
721 }
722 if (note->u.note.pitch - LOWEST_PITCH == 3) {
723 /* if we're on the 'd' line, the queue could be long or short */
724 gregorio_snprintf(buf, sizeof buf,
725 "VirgaReversaAscendensOnDLine{%s}", code_point(name,
726 glyph->u.notes.is_cavum, cpbuf, sizeof cpbuf));
727 return buf;
728 }
729 return name;
730 case L_AUCTUS_DESCENDENS:
731 switch (queuetype_of(note)) {
732 case Q_ON_SPACE_BELOW_BOTTOM_LINE:
733 case Q_ON_SPACE_ABOVE_BOTTOM_LINE:
734 return SHAPE_VirgaReversaDescendens;
735 case Q_ON_BOTTOM_LINE:
736 return SHAPE_VirgaReversaOpenqueueDescendens;
737 case Q_ON_LINE_ABOVE_BOTTOM_LINE:
738 return SHAPE_VirgaReversaLongqueueDescendens;
739 } /* all cases return, so this line is not hit; LCOV_EXCL_LINE */
740 /* LCOV_EXCL_START */
741 gregorio_fail2(determine_note_glyph_name, "unknown queuetype: %d",
742 queuetype_of(note));
743 return "";
744 /* LCOV_EXCL_STOP */
745 default:
746 return fusible_queued_shape(note, glyph, SHAPE_VirgaReversa,
747 SHAPE_VirgaReversaLongqueue, SHAPE_VirgaReversaOpenqueue);
748 }
749 case S_ORISCUS_ASCENDENS:
750 *type = AT_ORISCUS;
751 return compute_glyph_name(glyph, SHAPE_AscendensOriscus, LG_NONE, true);
752 case S_ORISCUS_DESCENDENS:
753 *type = AT_ORISCUS;
754 return compute_glyph_name(glyph, SHAPE_DescendensOriscus, LG_NONE, true);
755 case S_ORISCUS_DEMINUTUS:
756 *type = AT_ORISCUS;
757 return SHAPE_OriscusDeminutus;
758 case S_QUILISMA:
759 *type = AT_QUILISMA;
760 return compute_glyph_name(glyph, SHAPE_Quilisma, LG_NONE, true);
761 case S_ORISCUS_SCAPUS_ASCENDENS:
762 return fusible_queued_shape(note, glyph, SHAPE_AscendensOriscusScapus,
763 SHAPE_AscendensOriscusScapusLongqueue,
764 SHAPE_AscendensOriscusScapusOpenqueue);
765 case S_ORISCUS_SCAPUS_DESCENDENS:
766 return fusible_queued_shape(note, glyph, SHAPE_DescendensOriscusScapus,
767 SHAPE_DescendensOriscusScapusLongqueue,
768 SHAPE_DescendensOriscusScapusOpenqueue);
769 case S_STROPHA:
770 *type = AT_STROPHA;
771 if (!(note->u.note.liquescentia &
772 (L_AUCTUS_ASCENDENS | L_AUCTUS_DESCENDENS))) {
773 return SHAPE_Stropha;
774 }
775 /* fall through */
776 case S_STROPHA_AUCTA:
777 *type = AT_STROPHA;
778 switch (queuetype_of(note)) {
779 case Q_ON_SPACE_BELOW_BOTTOM_LINE:
780 case Q_ON_SPACE_ABOVE_BOTTOM_LINE:
781 return SHAPE_StrophaAucta;
782 case Q_ON_BOTTOM_LINE:
783 case Q_ON_LINE_ABOVE_BOTTOM_LINE:
784 return SHAPE_StrophaAuctaLongtail;
785 } /* all cases return, so this line is not hit; LCOV_EXCL_LINE */
786 /* LCOV_EXCL_START */
787 gregorio_fail2(determine_note_glyph_name, "unknown queuetype: %d",
788 queuetype_of(note));
789 return "";
790 /* LCOV_EXCL_STOP */
791 case S_FLAT:
792 return SHAPE_Flat;
793 case S_FLAT_PAREN:
794 return SHAPE_FlatParen;
795 case S_FLAT_SOFT:
796 return SHAPE_FlatSoft;
797 case S_SHARP:
798 return SHAPE_Sharp;
799 case S_SHARP_PAREN:
800 return SHAPE_SharpParen;
801 case S_SHARP_SOFT:
802 return SHAPE_SharpSoft;
803 case S_NATURAL:
804 return SHAPE_Natural;
805 case S_NATURAL_PAREN:
806 return SHAPE_NaturalParen;
807 case S_NATURAL_SOFT:
808 return SHAPE_NaturalSoft;
809 default:
810 /* not reachable unless there's a programming error */
811 /* LCOV_EXCL_START */
812 gregorio_fail2(determine_note_glyph_name,
813 "called with unknown shape: %s",
814 gregorio_shape_to_string(note->u.note.shape));
815 return "";
816 /* LCOV_EXCL_STOP */
817 }
818 }
819
820 static __inline gregorio_note *first_note_of(
821 const gregorio_glyph *const glyph) {
822 assert(glyph->type == GRE_GLYPH);
823 assert(glyph->u.notes.first_note);
824 assert(glyph->u.notes.first_note->type == GRE_NOTE);
825 return glyph->u.notes.first_note;
826 }
827
828 static __inline signed char first_pitch_of(const gregorio_glyph *const glyph) {
829 return first_note_of(glyph)->u.note.pitch;
830 }
831
832 static __inline gregorio_note *second_note_of(
833 const gregorio_glyph *const glyph) {
834 assert(glyph->type == GRE_GLYPH);
835 assert(glyph->u.notes.first_note);
836 assert(glyph->u.notes.first_note->type == GRE_NOTE);
837 assert(glyph->u.notes.first_note->next);
838 assert(glyph->u.notes.first_note->next->type == GRE_NOTE);
839 return glyph->u.notes.first_note->next;
840 }
841
842 static __inline signed char second_pitch_of(const gregorio_glyph *const glyph) {
843 return second_note_of(glyph)->u.note.pitch;
844 }
845
846 static __inline gregorio_note *third_note_of(
847 const gregorio_glyph *const glyph) {
848 assert(glyph->type == GRE_GLYPH);
849 assert(glyph->u.notes.first_note);
850 assert(glyph->u.notes.first_note->type == GRE_NOTE);
851 assert(glyph->u.notes.first_note->next);
852 assert(glyph->u.notes.first_note->next->type == GRE_NOTE);
853 assert(glyph->u.notes.first_note->next->next);
854 assert(glyph->u.notes.first_note->next->next->type == GRE_NOTE);
855 return glyph->u.notes.first_note->next->next;
856 }
857
858 static __inline signed char third_pitch_of(const gregorio_glyph *const glyph) {
859 return third_note_of(glyph)->u.note.pitch;
860 }
861
862 static __inline const char *porrectus_shape(const gregorio_glyph *const glyph,
863 const char *base_shape, const char *longqueue_shape) {
864 const gregorio_note *const first_note = first_note_of(glyph);
865 if ((glyph->u.notes.glyph_type == G_PORRECTUS
866 && (glyph->u.notes.liquescentia & L_DEMINUTUS))
867 || first_note->u.note.pitch - second_pitch_of(glyph) == 1) {
868 switch (queuetype_of(first_note)) {
869 case Q_ON_SPACE_BELOW_BOTTOM_LINE:
870 case Q_ON_SPACE_ABOVE_BOTTOM_LINE:
871 case Q_ON_BOTTOM_LINE:
872 return base_shape;
873 case Q_ON_LINE_ABOVE_BOTTOM_LINE:
874 return longqueue_shape;
875 }
876 } /* must be optimized out; LCOV_EXCL_LINE */
877 return base_shape;
878 }
879
880 static __inline const char *flexus_shape(const gregorio_glyph *const glyph,
881 const signed char ambitus, const char *base_shape,
882 const char *longqueue_shape, const char *openqueue_shape) {
883 const bool ambitus_one = (ambitus == 1);
884 switch (adjusted_queuetype_of(second_note_of(glyph), first_note_of(glyph),
885 0)) {
886 case Q_ON_SPACE_BELOW_BOTTOM_LINE:
887 if (ambitus_one) {
888 return openqueue_shape;
889 }
890 /* else fall through */
891 case Q_ON_SPACE_ABOVE_BOTTOM_LINE:
892 /* at ambitus one, long and short are swapped becuase the queue where
893 * the second note is on a space is longer than on a line */
894 return ambitus_one? longqueue_shape : base_shape;
895 case Q_ON_BOTTOM_LINE:
896 if (ambitus_one) {
897 return openqueue_shape;
898 }
899 /* else fall through */
900 case Q_ON_LINE_ABOVE_BOTTOM_LINE:
901 /* at ambitus one, long and short are swapped becuase the queue where
902 * the second note is on a line is shorter than on a space */
903 return ambitus_one? base_shape : longqueue_shape;
904 }
905 /* not reachable unless there's a programming error */
906 /* LCOV_EXCL_START */
907 gregorio_fail(flexus_shape, "unexpected queue length");
908 return base_shape;
909 /* LCOV_EXCL_STOP */
910 }
911
912 static __inline const char *quadratum_shape(const gregorio_glyph *const glyph,
913 const char *base_shape, const char *longqueue_shape,
914 const char *openqueue_shape) {
915 const bool ambitus_one =
916 (second_pitch_of(glyph) - first_pitch_of(glyph) == 1);
917 if (!is_tail_liquescentia(glyph->u.notes.liquescentia)) {
918 switch (adjusted_queuetype_of(first_note_of(glyph),
919 second_note_of(glyph), 0)) {
920 case Q_ON_SPACE_BELOW_BOTTOM_LINE:
921 if (ambitus_one) {
922 return openqueue_shape;
923 }
924 /* else fall through */
925 case Q_ON_SPACE_ABOVE_BOTTOM_LINE:
926 /* at ambitus one, long and short are swapped becuase the queue
927 * where the first note is on a space is longer than on a line */
928 return ambitus_one? longqueue_shape : base_shape;
929 case Q_ON_BOTTOM_LINE:
930 if (ambitus_one) {
931 return openqueue_shape;
932 }
933 /* else fall through */
934 case Q_ON_LINE_ABOVE_BOTTOM_LINE:
935 /* at ambitus one, long and short are swapped becuase the queue
936 * where the first note is on a line is shorter than on a space */
937 return ambitus_one? base_shape : longqueue_shape;
938 }
939 /* not reachable unless there's a programming error */
940 /* LCOV_EXCL_START */
941 gregorio_fail(quadratum_shape, "unexpected queue length");
942 /* fall out of the conditional */
943 }
944 /* LCOV_EXCL_STOP */
945 return base_shape;
946 }
947
948 /* the function that calculates the number of the glyph. It also
949 * calculates the type, used for determining the position of signs. Type is
950 * very basic, it is only the global dimensions : torculus, one_note, etc. */
951
952 const char *gregoriotex_determine_glyph_name(const gregorio_glyph *const glyph,
953 gtex_alignment *const type, gtex_type *const gtype)
954 {
955 const char *shape = NULL;
956 gtex_glyph_liquescentia ltype;
957 signed char ambitus;
958 const gregorio_note *second_note;
959 gregorio_assert(glyph, gregoriotex_determine_glyph_name,
960 "called with NULL pointer", return "");
961 gregorio_assert(glyph->u.notes.first_note, gregoriotex_determine_glyph_name,
962 "called with a glyph that has no note", return "");
963 *gtype = T_ONE_NOTE;
964 switch (glyph->u.notes.glyph_type) {
965 case G_PODATUS:
966 switch (glyph->u.notes.first_note->u.note.shape) {
967 case S_QUILISMA:
968 *type = AT_QUILISMA;
969 /* the next if is because we made the choice that AUCTUS shapes
970 * look like pes quadratum. */
971 if (glyph->u.notes.liquescentia
972 & (L_AUCTUS_ASCENDENS | L_AUCTUS_DESCENDENS)) {
973 *gtype = T_QUILISMA_PES_QUADRATUM;
974 shape = SHAPE_QuilismaPesQuadratum;
975 } else {
976 *gtype = T_QUILISMA_PES;
977 shape = SHAPE_QuilismaPes;
978 }
979 ltype = LG_NO_INITIO;
980 break;
981 case S_ORISCUS_ASCENDENS:
982 case S_ORISCUS_SCAPUS_ASCENDENS:
983 *type = AT_ORISCUS;
984 *gtype = T_PES_QUASSUS;
985 shape = quadratum_shape(glyph, SHAPE_PesQuassus,
986 SHAPE_PesQuassusLongqueue, SHAPE_PesQuassusOpenqueue);
987 ltype = LG_NO_INITIO;
988 break;
989 case S_ORISCUS_DESCENDENS:
990 case S_ORISCUS_SCAPUS_DESCENDENS:
991 *type = AT_ORISCUS;
992 *gtype = T_PES_QUASSUS;
993 shape = quadratum_shape(glyph, SHAPE_PesQuassusInusitatus,
994 SHAPE_PesQuassusInusitatusLongqueue,
995 SHAPE_PesQuassusInusitatusOpenqueue);
996 ltype = LG_NO_INITIO;
997 break;
998 default:
999 *type = AT_ONE_NOTE;
1000 if (glyph->u.notes.liquescentia
1001 & (L_AUCTUS_ASCENDENS | L_AUCTUS_DESCENDENS)) {
1002 *gtype = T_PES_QUADRATUM;
1003 shape = SHAPE_PesQuadratum;
1004 } else {
1005 *gtype = T_PES;
1006 shape = SHAPE_Pes;
1007 }
1008 ltype = LG_ALL;
1009 break;
1010 }
1011 break;
1012 case G_PES_QUADRATUM:
1013 switch (glyph->u.notes.first_note->u.note.shape) {
1014 case S_QUILISMA:
1015 *type = AT_QUILISMA;
1016 *gtype = T_QUILISMA_PES_QUADRATUM;
1017 shape = quadratum_shape(glyph, SHAPE_QuilismaPesQuadratum,
1018 SHAPE_QuilismaPesQuadratumLongqueue,
1019 SHAPE_QuilismaPesQuadratumOpenqueue);
1020 ltype = LG_NO_INITIO;
1021 break;
1022 default:
1023 *type = AT_ONE_NOTE;
1024 *gtype = T_PES_QUADRATUM;
1025 shape = quadratum_shape(glyph, SHAPE_PesQuadratum,
1026 SHAPE_PesQuadratumLongqueue, SHAPE_PesQuadratumOpenqueue);
1027 ltype = LG_ALL;
1028 break;
1029 }
1030 break;
1031 case G_PES_ASCENDENS_ORISCUS:
1032 *type = AT_ONE_NOTE;
1033 *gtype = T_PES_ORISCUS;
1034 shape = SHAPE_PesAscendensOriscus;
1035 ltype = LG_ALL;
1036 break;
1037 case G_PES_DESCENDENS_ORISCUS:
1038 *type = AT_ONE_NOTE;
1039 *gtype = T_PES_ORISCUS;
1040 shape = SHAPE_PesDescendensOriscus;
1041 ltype = LG_ALL;
1042 break;
1043 case G_FLEXA:
1044 ambitus = first_pitch_of(glyph) - second_pitch_of(glyph);
1045 if (glyph->u.notes.liquescentia & L_DEMINUTUS) {
1046 *type = AT_FLEXUS_DEMINUTUS;
1047 } else {
1048 if (ambitus == 1) {
1049 *type = AT_FLEXUS_1;
1050 } else {
1051 *type = AT_FLEXUS;
1052 }
1053 }
1054 switch (glyph->u.notes.first_note->u.note.shape) {
1055 case S_ORISCUS_ASCENDENS:
1056 *gtype = T_FLEXUS_ORISCUS;
1057 shape = SHAPE_FlexusOriscusInusitatus;
1058 ltype = LG_NO_INITIO;
1059 break;
1060
1061 case S_ORISCUS_DESCENDENS:
1062 *gtype = T_FLEXUS_ORISCUS;
1063 shape = SHAPE_FlexusOriscus;
1064 ltype = LG_NO_INITIO;
1065 break;
1066
1067 case S_ORISCUS_SCAPUS_ASCENDENS:
1068 *gtype = T_FLEXUS_ORISCUS_SCAPUS;
1069 shape = flexus_shape(glyph, ambitus,
1070 SHAPE_FlexusOriscusScapusInusitatus,
1071 SHAPE_FlexusOriscusScapusInusitatusLongqueue,
1072 SHAPE_FlexusOriscusScapusInusitatusOpenqueue);
1073 ltype = LG_NO_INITIO;
1074 break;
1075
1076 case S_ORISCUS_SCAPUS_DESCENDENS:
1077 *gtype = T_FLEXUS_ORISCUS_SCAPUS;
1078 shape = flexus_shape(glyph, ambitus, SHAPE_FlexusOriscusScapus,
1079 SHAPE_FlexusOriscusScapusLongqueue,
1080 SHAPE_FlexusOriscusScapusOpenqueue);
1081 ltype = LG_NO_INITIO;
1082 break;
1083
1084 default:
1085 *gtype = glyph->u.notes.fuse_to_next_glyph? T_PORRECTUS : T_FLEXUS;
1086 shape = flexus_shape(glyph, ambitus, SHAPE_Flexus,
1087 SHAPE_FlexusLongqueue, SHAPE_FlexusOpenqueue);
1088 ltype = LG_NO_INITIO;
1089 break;
1090 }
1091 break;
1092 case G_TORCULUS:
1093 *gtype = T_TORCULUS;
1094 if (glyph->u.notes.first_note->u.note.shape == S_QUILISMA) {
1095 *type = AT_QUILISMA;
1096 shape = SHAPE_TorculusQuilisma;
1097 ltype = LG_NO_INITIO;
1098 } else {
1099 *type = AT_ONE_NOTE;
1100 shape = SHAPE_Torculus;
1101 ltype = LG_ALL;
1102 }
1103 break;
1104 case G_TORCULUS_LIQUESCENS:
1105 *gtype = T_TORCULUS_LIQUESCENS;
1106 if (glyph->u.notes.first_note->u.note.shape == S_QUILISMA) {
1107 *type = AT_QUILISMA;
1108 shape = SHAPE_TorculusLiquescensQuilisma;
1109 } else {
1110 *type = AT_ONE_NOTE;
1111 shape = SHAPE_TorculusLiquescens;
1112 }
1113 ltype = LG_ONLY_DEMINUTUS;
1114 break;
1115 case G_TORCULUS_RESUPINUS_FLEXUS:
1116 *type = AT_ONE_NOTE;
1117 *gtype = T_TORCULUS_RESUPINUS_FLEXUS;
1118 break;
1119 case G_PORRECTUS:
1120 *type = AT_PORRECTUS;
1121 *gtype = T_PORRECTUS;
1122 shape = porrectus_shape(glyph, SHAPE_Porrectus,
1123 SHAPE_PorrectusLongqueue);
1124 ltype = LG_NO_INITIO;
1125 break;
1126 case G_TORCULUS_RESUPINUS:
1127 *gtype = T_TORCULUS_RESUPINUS;
1128 if (glyph->u.notes.first_note->u.note.shape == S_QUILISMA) {
1129 *type = AT_QUILISMA;
1130 shape = SHAPE_TorculusResupinusQuilisma;
1131 } else {
1132 *type = AT_ONE_NOTE;
1133 shape = SHAPE_TorculusResupinus;
1134 }
1135 ltype = LG_ALL;
1136 break;
1137 case G_PORRECTUS_FLEXUS:
1138 *type = AT_PORRECTUS;
1139 *gtype = T_PORRECTUS_FLEXUS;
1140 shape = porrectus_shape(glyph, SHAPE_PorrectusFlexus,
1141 SHAPE_PorrectusFlexusLongqueue);
1142 ltype = LG_NO_INITIO;
1143 break;
1144 case G_PORRECTUS_NO_BAR:
1145 *type = AT_PORRECTUS;
1146 *gtype = T_TORCULUS_RESUPINUS;
1147 shape = SHAPE_PorrectusNobar;
1148 ltype = LG_NO_INITIO;
1149 break;
1150 case G_PORRECTUS_FLEXUS_NO_BAR:
1151 *type = AT_PORRECTUS;
1152 *gtype = T_TORCULUS_RESUPINUS_FLEXUS;
1153 shape = SHAPE_PorrectusFlexusNobar;
1154 ltype = LG_NO_INITIO;
1155 break;
1156 case G_ANCUS:
1157 *type = AT_ONE_NOTE;
1158 gregorio_assert(glyph->u.notes.liquescentia & L_DEMINUTUS,
1159 gregoriotex_determine_glyph_name,
1160 "encountered non-deminutus ancus", break);
1161 second_note = second_note_of(glyph);
1162 if (first_pitch_of(glyph) - second_note->u.note.pitch == 1) {
1163 *type = AT_FLEXUS_1;
1164 } else {
1165 *type = AT_FLEXUS;
1166 }
1167 *gtype = T_ANCUS;
1168 ambitus = first_pitch_of(glyph) - second_pitch_of(glyph);
1169 shape = flexus_shape(glyph, ambitus, SHAPE_Ancus,
1170 SHAPE_AncusLongqueue, SHAPE_Ancus);
1171 ltype = LG_ONLY_DEMINUTUS;
1172 break;
1173 case G_SCANDICUS:
1174 *type = AT_ONE_NOTE;
1175 *gtype = T_SCANDICUS;
1176 shape = SHAPE_Scandicus;
1177 ltype = LG_NO_INITIO;
1178 break;
1179 case G_SALICUS:
1180 *type = AT_ONE_NOTE;
1181 *gtype = T_SALICUS;
1182 ambitus = third_pitch_of(glyph) - second_pitch_of(glyph);
1183 shape = flexus_shape(glyph, ambitus, SHAPE_Salicus,
1184 SHAPE_SalicusLongqueue, SHAPE_Salicus);
1185 ltype = LG_NO_INITIO;
1186 break;
1187 case G_SALICUS_FLEXUS:
1188 *type = AT_ONE_NOTE;
1189 *gtype = T_SALICUS_FLEXUS;
1190 shape = SHAPE_SalicusFlexus;
1191 ltype = LG_NO_INITIO;
1192 break;
1193 case G_PUNCTUM_INCLINATUM:
1194 case G_PUNCTA_INCLINATA:
1195 case G_2_PUNCTA_INCLINATA_DESCENDENS:
1196 case G_3_PUNCTA_INCLINATA_DESCENDENS:
1197 case G_4_PUNCTA_INCLINATA_DESCENDENS:
1198 case G_5_PUNCTA_INCLINATA_DESCENDENS:
1199 case G_2_PUNCTA_INCLINATA_ASCENDENS:
1200 case G_3_PUNCTA_INCLINATA_ASCENDENS:
1201 case G_4_PUNCTA_INCLINATA_ASCENDENS:
1202 case G_5_PUNCTA_INCLINATA_ASCENDENS:
1203 case G_PUNCTUM:
1204 case G_STROPHA:
1205 case G_VIRGA:
1206 case G_VIRGA_REVERSA:
1207 case G_STROPHA_AUCTA:
1208 case G_DISTROPHA:
1209 case G_DISTROPHA_AUCTA:
1210 case G_TRISTROPHA:
1211 case G_TRISTROPHA_AUCTA:
1212 case G_BIVIRGA:
1213 case G_TRIVIRGA:
1214 case G_ALTERATION:
1215 *type = AT_ONE_NOTE;
1216 break;
1217 default:
1218 /* not reachable unless there's a programming error */
1219 /* LCOV_EXCL_START */
1220 gregorio_fail2(gregoriotex_determine_glyph_name,
1221 "called with unknown glyph: %s",
1222 gregorio_glyph_type_to_string(glyph->u.notes.glyph_type));
1223 break;
1224 /* LCOV_EXCL_STOP */
1225 }
1226 if (shape) {
1227 shape = compute_glyph_name(glyph, shape, ltype, false);
1228 }
1229 /* we fix *type with initio_debilis */
1230 if (*type == AT_ONE_NOTE) {
1231 if (is_initio_debilis(glyph->u.notes.liquescentia)) {
1232 *type = AT_INITIO_DEBILIS;
1233 }
1234 }
1235
1236 return shape;
1237 }
1238
1239 /**
1240 * This now does nothing useful, but we keep it here in case it may
1241 * be needed in future.
1242 */
1243 static void gregoriotex_write_voice_info(FILE *f, gregorio_voice_info *voice_info)
1244 {
1245 gregorio_assert(f && voice_info, gregoriotex_write_voice_info,
1246 "file or voice_info passed as NULL", return);
1247 }
1248
1249 /* this function indicates if the syllable is the last of the line or score. */
1250 static bool is_last_of_line(gregorio_syllable *syllable)
1251 {
1252 gregorio_element *current_element = NULL;
1253 if (!(syllable->next_syllable) || ((syllable->next_syllable->elements)[0]
1254 && (syllable->next_syllable->elements)[0]->type
1255 == GRE_END_OF_LINE)) {
1256 /* the next syllable start by an end of line */
1257 return true;
1258 }
1259 if (syllable->elements) {
1260 current_element = (syllable->elements)[0];
1261 while (current_element) {
1262 if (current_element->type == GRE_END_OF_LINE) {
1263 /* we return true only if the end of line is the last element */
1264 if (!(current_element->next)) {
1265 return true;
1266 } else {
1267 return false;
1268 }
1269 } else {
1270 current_element = current_element->next;
1271 }
1272 }
1273 }
1274 return false;
1275 }
1276
1277 /*
1278 * ! @brief Prints the beginning of each text style
1279 */
1280 static void gtex_write_begin(FILE *f, grestyle_style style)
1281 {
1282 if (style == gregoriotex_ignore_style) {
1283 return;
1284 }
1285 switch (style) {
1286 case ST_ITALIC:
1287 fprintf(f, "\\GreItalic{");
1288 break;
1289 case ST_SMALL_CAPS:
1290 fprintf(f, "\\GreSmallCaps{");
1291 break;
1292 case ST_BOLD:
1293 fprintf(f, "\\GreBold{");
1294 break;
1295 case ST_FORCED_CENTER:
1296 case ST_CENTER:
1297 fprintf(f, "}{");
1298 break;
1299 case ST_TT:
1300 fprintf(f, "\\GreTypewriter{");
1301 break;
1302 case ST_UNDERLINED:
1303 fprintf(f, "\\GreUnderline{");
1304 break;
1305 case ST_COLORED:
1306 fprintf(f, "\\GreColored{");
1307 break;
1308 case ST_ELISION:
1309 fprintf(f, "\\GreElision{");
1310 break;
1311 case ST_FIRST_WORD:
1312 fprintf(f, "\\GreFirstWord{");
1313 break;
1314 case ST_FIRST_SYLLABLE:
1315 fprintf(f, "\\GreFirstSyllable{");
1316 break;
1317 case ST_FIRST_SYLLABLE_INITIAL:
1318 fprintf(f, "\\GreFirstSyllableInitial{");
1319 break;
1320 case ST_PROTRUSION_FACTOR:
1321 fprintf(f, "\\GreProtrusion{");
1322 break;
1323 case ST_PROTRUSION:
1324 fprintf(f, "{");
1325 break;
1326 default:
1327 break;
1328 }
1329 }
1330
1331 /**
1332 * @brief Ends each text style
1333 */
1334 static void gtex_write_end(FILE *f, grestyle_style style)
1335 {
1336 if (style == gregoriotex_ignore_style) {
1337 return;
1338 }
1339 switch (style) {
1340 case ST_FORCED_CENTER:
1341 case ST_CENTER:
1342 case ST_SYLLABLE_INITIAL:
1343 fprintf(f, "}{");
1344 break;
1345 case ST_INITIAL:
1346 break;
1347 default:
1348 fprintf(f, "}");
1349 break;
1350 }
1351 }
1352
1353 static __inline void tex_escape_text(FILE *const f, const char *text)
1354 {
1355 /* We escape these characters into \string\ddd (where ddd is the decimal
1356 * ASCII value of the character) for most escapes, and into \string\n for
1357 * newlines. We do it this way to get the "raw" string values through TeX
1358 * and into Lua, where the sequences become \ddd and \n respectively and
1359 * are translated into their byte values. Lua can then decide whether the
1360 * full strings should be evaluated by TeX as TeX or as strings */
1361 for (; *text; ++text) {
1362 switch (*text) {
1363 case '\\':
1364 case '{':
1365 case '}':
1366 case '~':
1367 case '%': /* currently, we'll never get %, but handle it anyway */
1368 case '#':
1369 case '"':
1370 /* these characters have special meaning to TeX */
1371 fprintf(f, "\\string\\%03d", *text);
1372 break;
1373 case '\n':
1374 fprintf(f, "\\string\\n");
1375 break;
1376 case '\r':
1377 /* ignore */
1378 break;
1379 default:
1380 /* UTF-8 multibyte sequences will fall into here, which is fine */
1381 fputc(*text, f);
1382 break;
1383 }
1384 }
1385 }
1386
1387 static __inline void tex_escape_wtext(FILE *const f, const grewchar *text)
1388 {
1389 /* We escape these characters into \string\ddd (where ddd is the decimal
1390 * ASCII value of the character) for most escapes, and into \string\n for
1391 * newlines. We do it this way to get the "raw" string values through TeX
1392 * and into Lua, where the sequences become \ddd and \n respectively and
1393 * are translated into their byte values. Lua can then decide whether the
1394 * full strings should be evaluated by TeX as TeX or as strings */
1395 for (; *text; ++text) {
1396 switch (*text) {
1397 case L'\\':
1398 case L'{':
1399 case L'}':
1400 case L'~':
1401 case L'%': /* currently, we'll never get %, but handle it anyway */
1402 case L'#':
1403 case L'"':
1404 /* these characters have special meaning to TeX */
1405 fprintf(f, "\\string\\%03d", *text);
1406 break;
1407 /* There is currently no way to get a carriage return or a newline into
1408 * an <sp>, but we'll leave this code here, but #if'd out, in case we
1409 * need this function in a different context */
1410 #if 0
1411 case L'\n':
1412 fprintf(f, "\\string\\n");
1413 break;
1414 case L'\r':
1415 / * ignore * /
1416 break;
1417 #endif
1418 default:
1419 gregorio_print_unichar(f, *text);
1420 break;
1421 }
1422 }
1423 }
1424
1425 /*
1426 * ! @brief Writes GregorioTeX special characters. This function takes the
1427 * special characters as input (i.e. from gabc representation), and writes them
1428 * * in GregorioTeX form.
1429 */
1430 static void gtex_write_special_char(FILE *f, const grewchar *special_char)
1431 {
1432 fprintf(f, "\\GreSpecial{");
1433 tex_escape_wtext(f, special_char);
1434 fprintf(f, "}");
1435 }
1436
1437 static void gtex_write_verb(FILE *f, const grewchar *first_char)
1438 {
1439 gregorio_print_unistring(f, first_char);
1440 }
1441
1442 static void gtex_print_char(FILE *f, const grewchar to_print)
1443 {
1444 switch (to_print) {
1445 case L'*':
1446 fprintf(f, "\\GreStar{}");
1447 break;
1448 case L'%':
1449 /* there's currently no way to get a % into gabc, so this wont be hit,
1450 * but we leave it here for safety and possible future use */
1451 /* LCOV_EXCL_START */
1452 fprintf(f, "\\%%{}");
1453 break;
1454 /* LCOV_EXCL_STOP */
1455 case L'\\':
1456 fprintf(f, "\\textbackslash{}");
1457 break;
1458 case L'&':
1459 fprintf(f, "\\&{}");
1460 break;
1461 case L'#':
1462 fprintf(f, "\\#{}");
1463 break;
1464 case L'+':
1465 fprintf(f, "\\GreDagger{}");
1466 break;
1467 case L'_':
1468 fprintf(f, "\\_{}");
1469 break;
1470 case L'-':
1471 fprintf(f, "\\GreHyph{}");
1472 break;
1473 default:
1474 gregorio_print_unichar(f, to_print);
1475 break;
1476 }
1477 return;
1478 }
1479
1480 /* a function to map the internal ST_* styles to gregoriotex styles as defined
1481 * in gregoriotex-syllables.tex */
1482 static unsigned char gregoriotex_internal_style_to_gregoriotex(
1483 const grestyle_style style)
1484 {
1485 switch (style) {
1486 case ST_ITALIC:
1487 return 1;
1488 case ST_BOLD:
1489 return 2;
1490 case ST_SMALL_CAPS:
1491 return 3;
1492 case ST_TT:
1493 return 4;
1494 case ST_UNDERLINED:
1495 return 5;
1496 case ST_COLORED:
1497 return 6;
1498 default:
1499 /* not reachable unless there's a programming error */
1500 /* LCOV_EXCL_START */
1501 gregorio_fail2(gregoriotex_internal_style_to_gregoriotex,
1502 "unrecognized style: %s", grestyle_style_to_string(style));
1503 return 0;
1504 /* LCOV_EXCL_STOP */
1505 }
1506 }
1507
1508 /*
1509 * A quite hacky function: when we have only one style (typically italic) and
1510 * when this style is on all the parts, then we return this style.
1511 *
1512 */
1513 static grestyle_style gregoriotex_fix_style(
1514 const gregorio_character *current_char)
1515 {
1516 grestyle_style possible_fixed_style = ST_NO_STYLE;
1517 grestyle_style in_fixed_style = ST_NO_STYLE;
1518 while (current_char) {
1519 if (!in_fixed_style) {
1520 /* before the first style */
1521 if (current_char->is_character) {
1522 /* got some character, so no future style can apply to the
1523 * entire syllable */
1524 return ST_NO_STYLE;
1525 }
1526 if (current_char->cos.s.type == ST_T_BEGIN) {
1527 switch (current_char->cos.s.style) {
1528 case ST_VERBATIM:
1529 case ST_SPECIAL_CHAR:
1530 /* these are pseudo-characters, and if they appear
1531 * before the first style, then the style does not
1532 * apply to the entire syllable */
1533 return ST_NO_STYLE;
1534
1535 case ST_ITALIC:
1536 case ST_BOLD:
1537 case ST_TT:
1538 case ST_SMALL_CAPS:
1539 case ST_UNDERLINED:
1540 case ST_COLORED:
1541 if (possible_fixed_style) {
1542 if (current_char->cos.s.style != possible_fixed_style) {
1543 /* found a differing style */
1544 return ST_NO_STYLE;
1545 } else {
1546 /* same style which may apply to the entire
1547 * syllable */
1548 in_fixed_style = possible_fixed_style;
1549 }
1550 } else {
1551 /* we found a candidate fixed style */
1552 in_fixed_style = possible_fixed_style =
1553 current_char->cos.s.style;
1554 }
1555 break;
1556
1557 default:
1558 /* anything else is "transparent" before the first real
1559 * or pseudo-character */
1560 break;
1561 }
1562 }
1563 /* else it's an end of a style, which we treat as "transparent"
1564 * noise */
1565 } else {
1566 /* we have a possible style */
1567 if (!current_char->is_character) {
1568 if (current_char->cos.s.type == ST_T_BEGIN) {
1569 switch (current_char->cos.s.style) {
1570 case ST_ITALIC:
1571 case ST_BOLD:
1572 case ST_TT:
1573 case ST_SMALL_CAPS:
1574 case ST_UNDERLINED:
1575 case ST_COLORED:
1576 if (current_char->cos.s.style != possible_fixed_style) {
1577 /* found a differing style */
1578 return ST_NO_STYLE;
1579 }
1580 /* else it's a (nested) open of the same style, which
1581 * doesn't change the style; however, nested styles
1582 * (should) have been eliminated by now */
1583 /* LCOV_EXCL_START */
1584 gregorio_fail(gregoriotex_fix_style, "encountered a "
1585 "nested style which should have been removed "
1586 "by now");
1587 break;
1588 /* LCOV_EXCL_STOP */
1589
1590 default:
1591 /* anything else is a pseudo-character or a
1592 * "transparent" style, which don't affect the fixed
1593 * style */
1594 break;
1595 }
1596 } else if (current_char->cos.s.type == ST_T_END) {
1597 if (current_char->cos.s.style == possible_fixed_style) {
1598 /* we closed the possible fixed style; we don't return
1599 * yet to give it the chance to re-open */
1600 in_fixed_style = ST_NO_STYLE;
1601 }
1602 /* else it's a close of something we don't care about or a
1603 * style that wasn't opened, so for the purposes of this
1604 * function, these are "transparent" and don't affect the
1605 * fixed style */
1606 }
1607 }
1608 /* else it's a character, and that doesn't affect the fixed style */
1609 }
1610 current_char = current_char->next_character;
1611 }
1612 return possible_fixed_style;
1613 }
1614
1615 /*
1616 * @brief Writes the translation.
1617 *
1618 * There is no special handling of translation text; that is, we just print the
1619 * entire string of text under the normal text line, without considering any
1620 * special centering or linebreaks.
1621 */
1622 static __inline void write_translation(FILE *f,
1623 gregorio_character *translation)
1624 {
1625 gregorio_not_null(translation, write_translation, return);
1626 gregorio_write_text(WTP_NORMAL, translation, f, &gtex_write_verb,
1627 &gtex_print_char, &gtex_write_begin, &gtex_write_end,
1628 &gtex_write_special_char);
1629 }
1630
1631 /* a function to compute the height of the flat of a key
1632 * the flat is always on the line of the */
1633
1634 static char clef_flat_height(gregorio_clef clef, signed char line, bool flatted)
1635 {
1636 char offset = 6;
1637
1638 if (!flatted) {
1639 return NO_KEY_FLAT;
1640 }
1641
1642 switch (clef) {
1643 case CLEF_C:
1644 switch (line) {
1645 case 1:
1646 offset = 2;
1647 break;
1648 case 2:
1649 offset = 4;
1650 break;
1651 case 3:
1652 offset = 6;
1653 break;
1654 case 4:
1655 offset = 8;
1656 break;
1657 case 5:
1658 offset = 10;
1659 break;
1660 default:
1661 /* not reachable unless there's a programming error */
1662 /* LCOV_EXCL_START */
1663 gregorio_fail2(clef_flat_height, "unknown line number: %d", line);
1664 break;
1665 /* LCOV_EXCL_STOP */
1666 }
1667 break;
1668 case CLEF_F:
1669 switch (line) {
1670 case 1:
1671 offset = 6;
1672 break;
1673 case 2:
1674 offset = 8;
1675 break;
1676 case 3:
1677 offset = 3;
1678 break;
1679 case 4:
1680 offset = 5;
1681 break;
1682 case 5:
1683 offset = 7;
1684 break;
1685 default:
1686 /* not reachable unless there's a programming error */
1687 /* LCOV_EXCL_START */
1688 gregorio_fail2(clef_flat_height, "unknown line number: %d", line);
1689 break;
1690 /* LCOV_EXCL_STOP */
1691 }
1692 break;
1693 default:
1694 /* not reachable unless there's a programming error */
1695 /* LCOV_EXCL_START */
1696 gregorio_fail2(clef_flat_height, "unknown clef type: %d", clef);
1697 break;
1698 /* LCOV_EXCL_STOP */
1699 }
1700
1701 return pitch_value(LOWEST_PITCH + offset);
1702 }
1703
1704 static __inline bool is_manual_custos(const gregorio_element *const element)
1705 {
1706 return element->type == GRE_CUSTOS && element->u.misc.pitched.force_pitch;
1707 }
1708
1709 OFFSET_CASE(BarStandard);
1710 OFFSET_CASE(BarVirgula);
1711 OFFSET_CASE(BarDivisioFinalis);
1712 OFFSET_CASE(BarParen);
1713 OFFSET_CASE(BarVirgulaParen);
1714
1715 static void write_bar(FILE *f, const gregorio_score *const score,
1716 const gregorio_syllable *const syllable,
1717 const gregorio_element *const element,
1718 const unsigned char first_of_disc)
1719 {
1720 const gregorio_bar type = element->u.misc.unpitched.info.bar;
1721 const gregorio_sign signs = element->u.misc.unpitched.special_sign;
1722 const bool is_inside_bar = element->next && !is_manual_custos(element->next)
1723 && element->next->type != GRE_END_OF_LINE;
1724 const bool has_text = !element->previous && syllable->text;
1725 /* the type number of function vepisemaorrare */
1726 const char *offset_case = BarStandard;
1727 signed char far_pitch_adjustment = 0;
1728 /* don't use "In" version of bars in the first argument of a GreDiscretionary */
1729 if (is_inside_bar && first_of_disc != 1) {
1730 fprintf(f, "\\GreIn");
1731 } else {
1732 fprintf(f, "\\Gre");
1733 }
1734 switch (type) {
1735 case B_VIRGULA:
1736 fprintf(f, "Virgula");
1737 offset_case = BarVirgula;
1738 break;
1739 case B_DIVISIO_MINIMA:
1740 fprintf(f, "DivisioMinima");
1741 break;
1742 case B_DIVISIO_MINOR:
1743 fprintf(f, "DivisioMinor");
1744 break;
1745 case B_DIVISIO_MAIOR:
1746 fprintf(f, "DivisioMaior");
1747 break;
1748 case B_DIVISIO_FINALIS:
1749 fprintf(f, "DivisioFinalis");
1750 offset_case = BarDivisioFinalis;
1751 break;
1752 case B_DIVISIO_MINOR_D1:
1753 fprintf(f, "Dominica{1}");
1754 break;
1755 case B_DIVISIO_MINOR_D2:
1756 fprintf(f, "Dominica{2}");
1757 break;
1758 case B_DIVISIO_MINOR_D3:
1759 fprintf(f, "Dominica{3}");
1760 break;
1761 case B_DIVISIO_MINOR_D4:
1762 fprintf(f, "Dominica{4}");
1763 break;
1764 case B_DIVISIO_MINOR_D5:
1765 fprintf(f, "Dominica{5}");
1766 break;
1767 case B_DIVISIO_MINOR_D6:
1768 fprintf(f, "Dominica{6}");
1769 break;
1770 case B_DIVISIO_MINOR_D7:
1771 fprintf(f, "Dominica{7}");
1772 break;
1773 case B_DIVISIO_MINOR_D8:
1774 fprintf(f, "Dominica{8}");
1775 break;
1776 case B_VIRGULA_HIGH:
1777 fprintf(f, "VirgulaHigh");
1778 offset_case = BarVirgula;
1779 break;
1780 case B_DIVISIO_MINIMA_HIGH:
1781 fprintf(f, "DivisioMinimaHigh");
1782 break;
1783 case B_DIVISIO_MAIOR_DOTTED:
1784 fprintf(f, "DivisioMaiorDotted");
1785 break;
1786 case B_DIVISIO_MINIMIS:
1787 fprintf(f, "DivisioMinimis");
1788 break;
1789 case B_DIVISIO_MINIMIS_HIGH:
1790 fprintf(f, "DivisioMinimisHigh");
1791 break;
1792 case B_VIRGULA_PAREN:
1793 fprintf(f, "VirgulaParen");
1794 offset_case = BarVirgulaParen;
1795 break;
1796 case B_VIRGULA_PAREN_HIGH:
1797 fprintf(f, "VirgulaParenHigh");
1798 offset_case = BarVirgulaParen;
1799 break;
1800 case B_DIVISIO_MINIMA_PAREN:
1801 fprintf(f, "DivisioMinimaParen");
1802 offset_case = BarParen;
1803 break;
1804 case B_DIVISIO_MINIMA_PAREN_HIGH:
1805 fprintf(f, "DivisioMinimaParenHigh");
1806 offset_case = BarParen;
1807 break;
1808 default:
1809 /* not reachable unless there's a programming error */
1810 /* LCOV_EXCL_START */
1811 gregorio_fail2(write_bar, "unknown bar type: %d", type);
1812 break;
1813 /* LCOV_EXCL_STOP */
1814 }
1815 fprintf(f, "{%c}", has_text? '1' : '0');
1816 switch (signs) {
1817 case _V_EPISEMA:
1818 fprintf(f, "{\\GreBarVEpisema{\\GreOCase%s}}", offset_case);
1819 break;
1820 case _BAR_H_EPISEMA:
1821 fprintf(f, "{\\GreBarBrace{\\GreOCase%s}}", offset_case);
1822 break;
1823 case _V_EPISEMA_BAR_H_EPISEMA:
1824 fprintf(f, "{\\GreBarBrace{\\GreOCase%s}"
1825 "\\GreBarVEpisema{\\GreOCase%s}}",
1826 offset_case, offset_case);
1827 break;
1828 default:
1829 fprintf(f, "{}");
1830 break;
1831 }
1832 switch (type) {
1833 case B_DIVISIO_MINIMIS_HIGH:
1834 ++ far_pitch_adjustment;
1835 /* fall through */
1836
1837 case B_VIRGULA_HIGH:
1838 case B_VIRGULA_PAREN_HIGH:
1839 case B_DIVISIO_MINIMA_HIGH:
1840 case B_DIVISIO_MINIMA_PAREN_HIGH:
1841 ++ far_pitch_adjustment;
1842 /* fall through */
1843
1844 case B_DIVISIO_MINIMIS:
1845 ++ far_pitch_adjustment;
1846 /* fall through */
1847
1848 case B_VIRGULA:
1849 case B_VIRGULA_PAREN:
1850 case B_DIVISIO_MINIMA:
1851 case B_DIVISIO_MINIMA_PAREN:
1852 {
1853 char is_far = '0';
1854 const gregorio_element *e;
1855 const gregorio_syllable *s;
1856 const gregorio_glyph *g;
1857 const gregorio_note *n;
1858 signed char pitch;
1859
1860 /* find the prior element */
1861 e = element->previous;
1862 if (!e) {
1863 for (s = syllable->previous_syllable; s; s = s->previous_syllable) {
1864 /* loop to find the previous syllable with elements */
1865 if (s->elements && *s->elements) {
1866 for (e = *s->elements; e->next; e = e->next) {
1867 /* just loop to find the last element */
1868 }
1869 break;
1870 }
1871 }
1872 }
1873 if (e && e->type == GRE_ELEMENT) {
1874 g = e->u.first_glyph;
1875 if (g) {
1876 while (g->next) {
1877 /* loop to find the last glyph in the prior element */
1878 g = g->next;
1879 }
1880 }
1881 if (g && g->type == GRE_GLYPH && (n = g->u.notes.first_note)) {
1882 while (n->next) {
1883 /* loop to find the last note */
1884 n = n->next;
1885 }
1886 pitch = n->u.note.pitch;
1887 if (g->u.notes.liquescentia & L_DEMINUTUS && n->previous
1888 && n->previous->u.note.pitch > pitch) {
1889 pitch = n->previous->u.note.pitch;
1890 }
1891 if (pitch < score->virgula_far_pitch + far_pitch_adjustment) {
1892 /* find next element */
1893 e = element->next;
1894 if (!e) {
1895 for (s = syllable->next_syllable; s;
1896 s = s->next_syllable) {
1897 /* loop to find the next syllable with elements */
1898 if (s->elements && *s->elements) {
1899 e = *s->elements;
1900 break;
1901 }
1902 }
1903 }
1904 if (e) {
1905 if (e->type == GRE_END_OF_LINE) {
1906 is_far = '1';
1907 } else if (e->type == GRE_ELEMENT) {
1908 g = e->u.first_glyph;
1909 if (g && g->type == GRE_GLYPH) {
1910 n = g->u.notes.first_note;
1911 if (n) {
1912 if (g->u.notes.glyph_type == G_PODATUS
1913 && n->next) {
1914 n = n->next;
1915 }
1916 if (n->u.note.pitch
1917 < score->virgula_far_pitch
1918 + far_pitch_adjustment) {
1919 is_far = '1';
1920 }
1921 }
1922 }
1923 }
1924 }
1925 }
1926 }
1927 }
1928 fprintf(f, "{%c}%%\n", is_far);
1929 }
1930 break;
1931
1932 default:
1933 fprintf(f, "%%\n");
1934 break;
1935 }
1936 }
1937
1938 static __inline char *suppose_high_ledger_line(const gregorio_note *const note)
1939 {
1940 return note->high_ledger_line? "\\GreSupposeHighLedgerLine" : "";
1941 }
1942
1943 static __inline char *suppose_low_ledger_line(const gregorio_note *const note)
1944 {
1945 return note->low_ledger_line? "\\GreSupposeLowLedgerLine" : "";
1946 }
1947
1948 /*
1949 * ! @brief Writes augmentum duplexes (double dots) We suppose we are on the
1950 * last note. \n The algorithm is the following: if there is a previous note,
1951 * we consider that the two puncta of the augumentum duplex must correspond to
1952 * the last note and the previous note. If we are adding to a single note
1953 * glyph, which would be weird but sure why not, we just typeset two puncta
1954 * spaced of 2.
1955 */
1956 static void write_auctum_duplex(FILE *f,
1957 const gregorio_note *const current_note)
1958 {
1959 char pitch = current_note->u.note.pitch;
1960 char previous_pitch = 0;
1961 /* second_pitch is the second argument of the \augmentumduplex macro,
1962 * that's what this function is all about. */
1963 char second_pitch = 0;
1964 /* this variable will be set to 1 if we are on the note before the last
1965 * note of a podatus or a porrectus or a torculus resupinus */
1966 unsigned char special_punctum = 0;
1967 if (current_note->previous) {
1968 if (current_note->previous->u.note.pitch - current_note->u.note.pitch ==
1969 -1
1970 || current_note->previous->u.note.pitch -
1971 current_note->u.note.pitch == 1) {
1972 special_punctum = 1;
1973 }
1974 second_pitch = current_note->previous->u.note.pitch;
1975 previous_pitch = second_pitch;
1976 }
1977
1978 if (!previous_pitch || previous_pitch == pitch) {
1979 if (is_on_a_line(pitch)) {
1980 second_pitch = pitch;
1981 special_punctum = 1;
1982 } else {
1983 second_pitch = pitch + 1;
1984 }
1985 }
1986 /* the first argument should always be the lowest one, that's what we do
1987 * here: */
1988 if (pitch > second_pitch) {
1989 previous_pitch = pitch;
1990 pitch = second_pitch;
1991 second_pitch = previous_pitch;
1992 }
1993 /* maybe the third argument should be changed */
1994 fprintf(f, "\\GreAugmentumDuplex{%d}{%d}{%d}%%\n", pitch_value(pitch),
1995 pitch_value(second_pitch), special_punctum);
1996 }
1997
1998 /**
1999 * @brief Adds a dot.
2000 *
2001 * Writes \c \\GrePunctumMora in the gtex file, with the appropriate arguments. You might think this function
2002 * more straightforward than it actually is...
2003 */
2004 static void write_punctum_mora(FILE *f, const gregorio_glyph *glyph,
2005 const gregorio_note *current_note, const int fuse_to_next_note)
2006 {
2007 /* in this if we consider that the puncta are only on the last two notes
2008 * (maybe it would be useful to consider it more entirely, but it would be
2009 * really weird...) */
2010 /* the variable that will be set to true if we have to shift the punctum
2011 * inclinatum before the last note */
2012 bool shift_before = false;
2013 /* this variable will be set to 1 if we are on the note before the last
2014 * note of a podatus or a porrectus or a torculus resupinus */
2015 unsigned char special_punctum = 0;
2016 /* 0 if space is normal, 1 if there should be no space after a punctum */
2017 unsigned char no_space = 0;
2018 /* the pitch where to set the punctum */
2019 char pitch = current_note->u.note.pitch;
2020 /* a variable to know if we are on a punctum inclinatum or not */
2021 unsigned char punctum_inclinatum = 0;
2022 /* a temp variable */
2023 const gregorio_note *tmpnote;
2024 /* we go into this switch only if it is the note before the last note */
2025 if (current_note->next) {
2026 switch (glyph->u.notes.glyph_type) {
2027 case G_FLEXA:
2028 case G_TORCULUS:
2029 case G_TORCULUS_RESUPINUS_FLEXUS:
2030 case G_PORRECTUS_FLEXUS:
2031 if (!(glyph->u.notes.liquescentia & L_DEMINUTUS)) {
2032 shift_before = true;
2033 }
2034 if (is_between_lines(pitch)
2035 && pitch - current_note->next->u.note.pitch == 1) {
2036 special_punctum = 2;
2037 }
2038 break;
2039 case G_PODATUS:
2040 if ((current_note->u.note.shape != S_PUNCTUM
2041 && current_note->u.note.shape != S_QUILISMA)
2042 || (glyph->u.notes.liquescentia
2043 & (L_AUCTUS_DESCENDENS | L_AUCTUS_ASCENDENS))) {
2044 shift_before = true;
2045 /* fine tuning */
2046 if (current_note->next->u.note.pitch -
2047 current_note->u.note.pitch == 1) {
2048 if (is_on_a_line(current_note->u.note.pitch)) {
2049 special_punctum = 1;
2050 } else {
2051 pitch = current_note->u.note.pitch - 1;
2052 }
2053 }
2054 } else {
2055 /* case for f.g */
2056 if (current_note->next->u.note.pitch -
2057 current_note->u.note.pitch == 1) {
2058 special_punctum = 1;
2059 }
2060 }
2061 break;
2062 case G_PES_QUADRATUM:
2063 shift_before = true;
2064 if (current_note->next->u.note.pitch - current_note->u.note.pitch ==
2065 1) {
2066 if (is_on_a_line(current_note->u.note.pitch)) {
2067 special_punctum = 1;
2068 } else {
2069 pitch = current_note->u.note.pitch - 1;
2070 }
2071 }
2072 break;
2073 case G_PORRECTUS:
2074 case G_TORCULUS_RESUPINUS:
2075 /* this case is only for the note before the previous note */
2076 if ((current_note->next->u.note.pitch -
2077 current_note->u.note.pitch == -1
2078 || current_note->next->u.note.pitch -
2079 current_note->u.note.pitch == 1)
2080 && !(current_note->next->next))
2081 special_punctum = 1;
2082 break;
2083 default:
2084 break;
2085 }
2086 }
2087 /* we enter here in any case */
2088 switch (glyph->u.notes.glyph_type) {
2089 case G_PUNCTA_INCLINATA:
2090 case G_2_PUNCTA_INCLINATA_DESCENDENS:
2091 case G_3_PUNCTA_INCLINATA_DESCENDENS:
2092 case G_4_PUNCTA_INCLINATA_DESCENDENS:
2093 case G_5_PUNCTA_INCLINATA_DESCENDENS:
2094 case G_TORCULUS:
2095 if (!current_note->next) {
2096 special_punctum = 1;
2097 }
2098 break;
2099 default:
2100 break;
2101 }
2102 switch (current_note->u.note.shape) {
2103 case S_PUNCTUM_INCLINATUM_ASCENDENS:
2104 case S_PUNCTUM_INCLINATUM_STANS:
2105 case S_PUNCTUM_INCLINATUM_DESCENDENS:
2106 case S_PUNCTUM_INCLINATUM_DEMINUTUS:
2107 punctum_inclinatum = 1;
2108 break;
2109 default:
2110 break;
2111 }
2112 /* when the punctum mora is on a note on a line, and the prior note is on
2113 * the space immediately above, the dot is placed on the space below the
2114 * line instead */
2115 if (current_note->previous
2116 && (current_note->previous->u.note.pitch -
2117 current_note->u.note.pitch == 1)
2118 && is_on_a_line(current_note->u.note.pitch)
2119 && (current_note->previous->signs == _PUNCTUM_MORA
2120 || current_note->previous->signs == _V_EPISEMA_PUNCTUM_MORA
2121 || current_note->previous->choral_sign)) {
2122 special_punctum = 1;
2123 }
2124
2125 if (current_note->mora_vposition) {
2126 special_punctum = current_note->mora_vposition == VPOS_BELOW? 1 : 0;
2127 }
2128
2129 if (shift_before) {
2130 if (current_note->next->u.note.pitch - current_note->u.note.pitch == -1
2131 || current_note->next->u.note.pitch -
2132 current_note->u.note.pitch == 1) {
2133 fprintf(f, "\\GrePunctumMora{%d}{3}{%d}{%d}%%\n",
2134 pitch_value(pitch), special_punctum, punctum_inclinatum);
2135 } else {
2136 fprintf(f, "\\GrePunctumMora{%d}{2}{%d}{%d}%%\n",
2137 pitch_value(pitch), special_punctum, punctum_inclinatum);
2138 }
2139 return;
2140 }
2141 /* There are two special cases. The first: if the next glyph is a
2142 * ZERO_WIDTH_SPACE, and the current glyph is a PES, and the punctum mora
2143 * is on the first note, and the first note of the next glyph is at least
2144 * two (or three depending on something) pitches higher than the current
2145 * note.
2146 * You'll all have understood, this case is quite rare... but when it
2147 * appears, we pass 1 as a second argument of \punctummora so that it
2148 * removes the space introduced by the punctummora. */
2149 if (glyph->u.notes.glyph_type == G_PODATUS && glyph->next
2150 && glyph->next->type == GRE_SPACE
2151 && glyph->next->u.misc.unpitched.info.space == SP_ZERO_WIDTH
2152 && current_note->next && glyph->next->next
2153 && glyph->next->next->type == GRE_GLYPH
2154 && glyph->next->next->u.notes.first_note
2155 && (glyph->next->next->u.notes.first_note->u.note.pitch -
2156 current_note->u.note.pitch > 1)) {
2157 fprintf(f, "\\GrePunctumMora{%d}{1}{%d}{%d}%%\n", pitch_value(pitch),
2158 special_punctum, punctum_inclinatum);
2159 return;
2160 }
2161 /* if there is a punctum or a auctum dumplex on a note after, we put a
2162 * zero-width punctum */
2163 tmpnote = current_note->next;
2164 while (tmpnote) {
2165 if (tmpnote->signs == _PUNCTUM_MORA || tmpnote->signs == _AUCTUM_DUPLEX
2166 || tmpnote->signs == _V_EPISEMA_PUNCTUM_MORA
2167 || tmpnote->signs == _V_EPISEMA_AUCTUM_DUPLEX
2168 || tmpnote->choral_sign) {
2169 no_space = 1;
2170 break;
2171 }
2172 tmpnote = tmpnote->next;
2173 }
2174 /* use a special no-space punctum mora for ascending fusion */
2175 if (fuse_to_next_note) {
2176 no_space = 1;
2177 if (fuse_to_next_note > 0) {
2178 special_punctum = 1;
2179 }
2180 }
2181
2182 /* the normal operation */
2183 fprintf(f, "\\GrePunctumMora{%d}{%d}{%d}{%d}%%\n", pitch_value(pitch),
2184 no_space, special_punctum, punctum_inclinatum);
2185 }
2186
2187 static __inline int get_punctum_inclinatum_space_case(
2188 const gregorio_note *const note)
2189 {
2190 char temp;
2191
2192 switch (note->u.note.shape) {
2193 case S_PUNCTUM_INCLINATUM_ASCENDENS:
2194 if (note->previous) {
2195 /* means that it is not the first note of the puncta inclinata
2196 * sequence */
2197 temp = note->previous->u.note.pitch - note->u.note.pitch;
2198 /* negative values = ascending ambitus */
2199 /* not sure we ever need to consider a larger ambitus here */
2200 switch (temp) {
2201 case 1:
2202 case 2:
2203 case 3:
2204 case 4:
2205 return 25;
2206 case -1:
2207 default:
2208 return 12;
2209 case -2:
2210 return 14;
2211 case -3:
2212 case -4:
2213 return 15;
2214 }
2215 }
2216 break;
2217 case S_PUNCTUM_INCLINATUM_STANS:
2218 if (note->previous) {
2219 /* means that it is not the first note of the puncta inclinata
2220 * sequence */
2221 return 26;
2222 }
2223 break;
2224 case S_PUNCTUM_INCLINATUM_DESCENDENS:
2225 if (note->previous) {
2226 /* means that it is not the first note of the puncta inclinata
2227 * sequence */
2228 temp = note->previous->u.note.pitch - note->u.note.pitch;
2229 /* negative values = ascending ambitus */
2230 /* not sure we ever need to consider a larger ambitus here */
2231 switch (temp) {
2232 case 1:
2233 default:
2234 return 3;
2235 case 2:
2236 return 10;
2237 case 3:
2238 case 4:
2239 return 11;
2240 case -1:
2241 case -2:
2242 case -3:
2243 case -4:
2244 return 24;
2245 }
2246 }
2247 break;
2248 case S_PUNCTUM_INCLINATUM_DEMINUTUS:
2249 if (note->previous) {
2250 /* means that it is not the first note of the puncta inclinata
2251 * sequence */
2252 temp = note->previous->u.note.pitch - note->u.note.pitch;
2253 if (temp < -2) {
2254 return 15;
2255 } else if (temp > 2) {
2256 return 11;
2257 } else {
2258 if (note->previous
2259 && note->previous->u.note.shape ==
2260 S_PUNCTUM_INCLINATUM_DEMINUTUS) {
2261 if (temp < -1) {
2262 /* really if the ambitus = 3rd at this point */
2263 return 14;
2264 } else if (temp > 1) {
2265 /* really if the ambitus = 3rd at this point */
2266 return 10;
2267 } else {
2268 /* temp == 0, so there is no ascending case */
2269 return 8;
2270 }
2271 } else {
2272 if (temp < 0) {
2273 /* puncta inclinatum followed by puncta inclinatum
2274 * debilis */
2275 return 13;
2276 } else if (temp > 0) {
2277 /* puncta inclinatum followed by puncta inclinatum
2278 * debilis */
2279 return 7;
2280 }
2281 }
2282 }
2283 } /* LCOV_EXCL_LINE */
2284 break;
2285 case S_PUNCTUM_INCLINATUM_AUCTUS:
2286 if (note->previous) {
2287 /* means that it is not the first note of the puncta inclinata
2288 * sequence */
2289 temp = note->previous->u.note.pitch - note->u.note.pitch;
2290 if (temp < -1 || temp > 1) {
2291 /* this is the normal interglyph space, so we'll use it for
2292 * either direction */
2293 return 1;
2294 } else {
2295 /* temp == 0, so there is no ascending case */
2296 /* we approximate that it is the same space */
2297 return 3;
2298 }
2299 }
2300 break;
2301 default:
2302 break;
2303 }
2304
2305 return -1;
2306 }
2307
2308 static __inline int get_punctum_inclinatum_to_nobar_space_case(
2309 const gregorio_glyph *const glyph)
2310 {
2311 if (glyph->u.notes.glyph_type <= G_PUNCTA_INCLINATA) {
2312 const gregorio_glyph *next = gregorio_next_non_texverb_glyph(glyph);
2313 if (next && next->type == GRE_GLYPH
2314 && (next->u.notes.glyph_type == G_PUNCTUM
2315 || (next->u.notes.glyph_type == G_FLEXA
2316 && !next->u.notes.fuse_to_next_glyph))) {
2317 int descent;
2318 const gregorio_note *note = gregorio_glyph_last_note(glyph);
2319 descent = note->u.note.pitch -
2320 glyph->next->u.notes.first_note->u.note.pitch;
2321 /* a negative descent is an ascent */
2322 switch(descent) {
2323 case -1:
2324 return 19;
2325 case 1:
2326 return 16;
2327 case -2:
2328 return 20;
2329 case 2:
2330 return 17;
2331 case -3:
2332 case -4:
2333 return 21;
2334 case 3:
2335 case 4:
2336 return 18;
2337 }
2338 }
2339 }
2340
2341 return -1;
2342 }
2343
2344 static __inline void write_single_hepisema(FILE *const f, int hepisema_case,
2345 const gregorio_note *const note, gregorio_sign_orientation orientation,
2346 bool connect, char height, const grehepisema_size size, const int i,
2347 const gregorio_glyph *const glyph,
2348 const int porrectus_long_episema_index,
2349 bool (*const is_episema_shown)(const gregorio_note *))
2350 {
2351 if (height) {
2352 const gregorio_hepisema_adjustment *adj =
2353 gregorio_get_hepisema_adjustment(
2354 note->he_adjustment_index[orientation]);
2355 char ambitus = 0;
2356 char size_arg;
2357
2358 switch (size) {
2359 case H_SMALL_LEFT:
2360 size_arg = 'l';
2361 connect = false;
2362 break;
2363 case H_SMALL_CENTRE:
2364 size_arg = 'c';
2365 connect = false;
2366 break;
2367 case H_SMALL_RIGHT:
2368 size_arg = 'r';
2369 break;
2370 default:
2371 size_arg = 'f';
2372 break;
2373 }
2374
2375 if (i == porrectus_long_episema_index && note->next
2376 && is_episema_shown(note->next)) {
2377 ambitus = compute_ambitus(note);
2378 }
2379
2380 if (i - 1 != porrectus_long_episema_index || !note->previous
2381 || !is_episema_shown(note->previous)) {
2382 if (connect) {
2383 const gregorio_glyph *next;
2384 if (!note->next
2385 && (!(next = gregorio_next_non_texverb_glyph(glyph))
2386 || next->type != GRE_SPACE
2387 || next->u.misc.unpitched.info.space
2388 != SP_ZERO_WIDTH)) {
2389 /* not followed by a zero-width space */
2390 /* try to fuse from punctum inclinatum to nobar glyph */
2391 fprintf(f,
2392 "\\GreHEpisemaBridge{%d}{%d}{%d}{%s%s}{%s}{%d}%%\n",
2393 pitch_value(height), hepisema_case,
2394 get_punctum_inclinatum_to_nobar_space_case(glyph),
2395 suppose_high_ledger_line(note),
2396 suppose_low_ledger_line(note),
2397 adj->nudge? adj->nudge : "", adj->vbasepos);
2398 } else if (note->next
2399 && (note->next->u.note.shape
2400 == S_PUNCTUM_INCLINATUM_ASCENDENS
2401 || note->next->u.note.shape
2402 == S_PUNCTUM_INCLINATUM_DESCENDENS
2403 || note->next->u.note.shape
2404 == S_PUNCTUM_INCLINATUM_DEMINUTUS
2405 || note->next->u.note.shape
2406 == S_PUNCTUM_INCLINATUM_AUCTUS)) {
2407 /* is a punctum inclinatum of some sort */
2408 fprintf(f, "\\GreHEpisemaBridge{%d}{%d}{%d}{%s%s}{%s}{%d}%%\n",
2409 pitch_value(height), hepisema_case,
2410 get_punctum_inclinatum_space_case(note->next),
2411 suppose_high_ledger_line(note),
2412 suppose_low_ledger_line(note),
2413 adj->nudge? adj->nudge : "", adj->vbasepos);
2414 }
2415 }
2416 fprintf(f, "\\GreHEpisema{%d}{\\GreOCase%s}{%d}{%d}{%c}{%d}"
2417 "{%s%s}{%s}{%d}%%\n", pitch_value(height),
2418 note->gtex_offset_case, ambitus, hepisema_case, size_arg,
2419 pitch_value(height), suppose_high_ledger_line(note),
2420 suppose_low_ledger_line(note), adj->nudge? adj->nudge : "",
2421 adj->vbasepos);
2422 }
2423 }
2424 }
2425
2426 /**
2427 * @brief A function that writes the good \c \\hepisema in GregorioTeX.
2428 * @param i The position of the note in the glyph.
2429 */
2430 static void gregoriotex_write_hepisema(FILE *const f,
2431 const gregorio_note *const note, const int i, const gtex_type type,
2432 const gregorio_glyph *const glyph)
2433 {
2434 int porrectus_long_episema_index = -1;
2435
2436 gregorio_assert(note, gregoriotex_write_hepisema,
2437 "called with NULL pointer", return);
2438
2439 switch (type) {
2440 case T_PORRECTUS:
2441 case T_PORRECTUS_FLEXUS:
2442 porrectus_long_episema_index = 1;
2443 break;
2444 case T_TORCULUS_RESUPINUS:
2445 case T_TORCULUS_RESUPINUS_FLEXUS:
2446 porrectus_long_episema_index = 2;
2447 break;
2448 default:
2449 /* do nothing */
2450 break;
2451 }
2452
2453 write_single_hepisema(f, 1, note, SO_UNDER, note->h_episema_below_connect,
2454 note->h_episema_below, note->h_episema_below_size, i, glyph,
2455 porrectus_long_episema_index, &gtex_is_h_episema_below_shown);
2456 write_single_hepisema(f, 0, note, SO_OVER, note->h_episema_above_connect,
2457 note->h_episema_above, note->h_episema_above_size, i, glyph,
2458 porrectus_long_episema_index, &gtex_is_h_episema_above_shown);
2459 }
2460
2461 /* a macro to write an additional line */
2462
2463 static void write_additional_line(FILE *f, int i, gtex_type type, bool bottom,
2464 const gregorio_note *current_note, const gregorio_score *const score)
2465 {
2466 char ambitus = 0;
2467 gregorio_assert(current_note, write_additional_line, "called with no note",
2468 return);
2469 /* patch to get a line under the full glyph in the case of dbc (for
2470 * example) */
2471 switch (type) {
2472 case T_PORRECTUS:
2473 case T_PORRECTUS_FLEXUS:
2474 switch (i) {
2475 case 1:
2476 i = HEPISEMA_FIRST_TWO;
2477 break;
2478 case 2:
2479 if (current_note->previous->u.note.pitch > LOW_LEDGER_LINE_PITCH
2480 && current_note->previous->u.note.pitch
2481 < score->high_ledger_line_pitch) {
2482 i = HEPISEMA_FIRST_TWO;
2483 /* HEPISEMA_FIRST_TWO works only for first note */
2484 current_note = current_note->previous;
2485 } else {
2486 return;
2487 }
2488 break;
2489 case 3:
2490 if (bottom || current_note->previous->u.note.pitch
2491 >= score->high_ledger_line_pitch) {
2492 /* we don't need to add twice the same line */
2493 return;
2494 }
2495 break;
2496 }
2497 break;
2498 case T_TORCULUS_RESUPINUS:
2499 case T_TORCULUS_RESUPINUS_FLEXUS:
2500 switch (i) {
2501 case 2:
2502 i = HEPISEMA_FIRST_TWO;
2503 break;
2504 case 3:
2505 if (current_note->previous->u.note.pitch > LOW_LEDGER_LINE_PITCH
2506 && current_note->previous->u.note.pitch
2507 < score->high_ledger_line_pitch) {
2508 i = HEPISEMA_FIRST_TWO;
2509 /* HEPISEMA_FIRST_TWO works only for first note */
2510 current_note = current_note->previous;
2511 } else {
2512 return;
2513 }
2514 break;
2515 case 4:
2516 if (bottom || current_note->previous->u.note.pitch
2517 >= score->high_ledger_line_pitch) {
2518 /* we don't need to add twice the same line */
2519 return;
2520 }
2521 break;
2522 }
2523 break;
2524 default:
2525 break;
2526 }
2527
2528 if (i == HEPISEMA_FIRST_TWO) {
2529 /* here we must compare the first note of the big bar with the second
2530 * one, but it may be tricky sometimes, because of the previous patch */
2531 gregorio_assert_only(!current_note->previous ||
2532 current_note->u.note.pitch
2533 > current_note->previous->u.note.pitch,
2534 write_additional_line, "HEPISEMA_FIRST_TWO on the wrong note");
2535 ambitus = current_note->u.note.pitch - current_note->next->u.note.pitch;
2536 }
2537 fprintf(f, "\\GreAdditionalLine{\\GreOCase%s}{%d}{%d}%%\n",
2538 current_note->gtex_offset_case, ambitus, bottom ? 3 : 2);
2539 }
2540
2541 /*
2542 *
2543 * a function that writes the good value of \vepisema in GregorioTeX. i is the
2544 * position of the note in the glyph
2545 *
2546 */
2547
2548 static void write_vepisema(FILE *f, const gregorio_note *const note)
2549 {
2550 fprintf(f, "\\GreVEpisema{%d}{\\GreOCase%s}%%\n",
2551 pitch_value(note->v_episema_height), note->gtex_offset_case);
2552 }
2553
2554 /*
2555 * a function that writes the rare signs in GregorioTeX. i is the position of
2556 * the note in the glyph */
2557
2558 static void write_rare_sign(FILE *f, const gregorio_note *const current_note,
2559 const gregorio_sign rare)
2560 {
2561 switch (rare) {
2562 case _ACCENTUS:
2563 fprintf(f, "\\GreAccentus{%d}{\\GreOCase%s}%%\n",
2564 pitch_value(current_note->u.note.pitch),
2565 current_note->gtex_offset_case);
2566 break;
2567 case _ACCENTUS_REVERSUS:
2568 fprintf(f, "\\GreReversedAccentus{%d}{\\GreOCase%s}%%\n",
2569 pitch_value(current_note->u.note.pitch),
2570 current_note->gtex_offset_case);
2571 break;
2572 case _CIRCULUS:
2573 fprintf(f, "\\GreCirculus{%d}{\\GreOCase%s}%%\n",
2574 pitch_value(current_note->u.note.pitch),
2575 current_note->gtex_offset_case);
2576 break;
2577 case _SEMI_CIRCULUS:
2578 fprintf(f, "\\GreSemicirculus{%d}{\\GreOCase%s}%%\n",
2579 pitch_value(current_note->u.note.pitch),
2580 current_note->gtex_offset_case);
2581 break;
2582 case _SEMI_CIRCULUS_REVERSUS:
2583 fprintf(f, "\\GreReversedSemicirculus{%d}{\\GreOCase%s}%%\n",
2584 pitch_value(current_note->u.note.pitch),
2585 current_note->gtex_offset_case);
2586 break;
2587 case _MUSICA_FICTA_FLAT:
2588 fprintf(f, "\\GreMusicaFictaFlat{%d}{\\GreOCase%s}%%\n",
2589 pitch_value(current_note->u.note.pitch),
2590 current_note->gtex_offset_case);
2591 break;
2592 case _MUSICA_FICTA_NATURAL:
2593 fprintf(f, "\\GreMusicaFictaNatural{%d}{\\GreOCase%s}%%\n",
2594 pitch_value(current_note->u.note.pitch),
2595 current_note->gtex_offset_case);
2596 break;
2597 case _MUSICA_FICTA_SHARP:
2598 fprintf(f, "\\GreMusicaFictaSharp{%d}{\\GreOCase%s}%%\n",
2599 pitch_value(current_note->u.note.pitch),
2600 current_note->gtex_offset_case);
2601 break;
2602 /* the cases of the bar signs are dealt in another function
2603 * (write_bar) */
2604 default:
2605 /* not reachable unless there's a programming error */
2606 /* LCOV_EXCL_START */
2607 assert(false);
2608 break;
2609 /* LCOV_EXCL_STOP */
2610 }
2611 }
2612
2613 static __inline const char *alteration_name(
2614 const gregorio_shape next_note_alteration)
2615 {
2616 switch (next_note_alteration) {
2617 case S_FLAT:
2618 return "Flat";
2619 case S_FLAT_PAREN:
2620 return "FlatParen";
2621 case S_FLAT_SOFT:
2622 return "FlatSoft";
2623 case S_SHARP:
2624 return "Sharp";
2625 case S_SHARP_PAREN:
2626 return "SharpParen";
2627 case S_SHARP_SOFT:
2628 return "SharpSoft";
2629 case S_NATURAL:
2630 return "Natural";
2631 case S_NATURAL_PAREN:
2632 return "NaturalParen";
2633 case S_NATURAL_SOFT:
2634 return "NaturalSoft";
2635 default:
2636 return "";
2637 }
2638 }
2639
2640 static const char *next_custos(const signed char next_note_pitch,
2641 const gregorio_shape next_note_alteration,
2642 gregoriotex_status *const status)
2643 {
2644 static char buf[50];
2645
2646 gregorio_snprintf(buf, sizeof buf, "%s\\GreNextCustos{%d}{%s}",
2647 status->suppressed_custos? "\\GreResetEolCustos" : "",
2648 pitch_value(next_note_pitch),
2649 alteration_name(next_note_alteration));
2650
2651 status->suppressed_custos = false;
2652
2653 return buf;
2654 }
2655
2656 /*
2657 * function used when the glyph is only one note long
2658 */
2659
2660 static void write_note(FILE *f, gregorio_note *note,
2661 const gregorio_glyph *glyph, const char next_note_pitch,
2662 const gregorio_shape next_note_alteration,
2663 gregoriotex_status *const status)
2664 {
2665 unsigned int initial_shape = note->u.note.shape;
2666 const char *shape;
2667 int space_case;
2668 static char cpbuf[96];
2669 /* type in the sense of GregorioTeX alignment type */
2670 gtex_alignment type = AT_ONE_NOTE;
2671 gregorio_assert(note, write_note, "called with NULL pointer",
2672 return);
2673 if (note->u.note.shape == S_PUNCTUM) {
2674 switch (note->u.note.liquescentia) {
2675 case L_AUCTUS_ASCENDENS:
2676 note->u.note.shape = S_PUNCTUM_AUCTUS_ASCENDENS;
2677 break;
2678 case L_AUCTUS_DESCENDENS:
2679 note->u.note.shape = S_PUNCTUM_AUCTUS_DESCENDENS;
2680 break;
2681 case L_INITIO_DEBILIS:
2682 if (glyph->u.notes.fuse_to_next_glyph > 0) {
2683 break;
2684 }
2685 /* fall through */
2686 case L_DEMINUTUS:
2687 /* this is a currenly unused, but we keep it as a fallback case */
2688 /* LCOV_EXCL_START */
2689 note->u.note.shape = S_PUNCTUM_DEMINUTUS;
2690 break;
2691 /* LCOV_EXCL_STOP */
2692 default:
2693 break;
2694 }
2695 }
2696 shape = determine_note_glyph_name(note, glyph, &type);
2697 note->u.note.shape = initial_shape;
2698 /* special things for puncta inclinata */
2699 space_case = get_punctum_inclinatum_space_case(note);
2700 if (space_case >= 0) {
2701 fprintf(f, "\\GreEndOfGlyph{%d}%%\n", space_case);
2702 }
2703
2704 switch (note->u.note.shape) {
2705 case S_FLAT:
2706 case S_FLAT_PAREN:
2707 case S_FLAT_SOFT:
2708 case S_NATURAL:
2709 case S_NATURAL_PAREN:
2710 case S_NATURAL_SOFT:
2711 case S_SHARP:
2712 case S_SHARP_PAREN:
2713 case S_SHARP_SOFT:
2714 fprintf(f, "\\Gre%s{%d}{0}", alteration_name(note->u.note.shape),
2715 pitch_value(note->u.note.pitch));
2716 break;
2717 default:
2718 fprintf(f, "\\GreGlyph{%s}{%d}{%s}{%d}",
2719 code_point(shape, glyph->u.notes.is_cavum, cpbuf, sizeof cpbuf),
2720 pitch_value(note->u.note.pitch),
2721 next_custos(next_note_pitch, next_note_alteration, status),
2722 type);
2723 break;
2724 }
2725 }
2726
2727 static void syllable_first_type(gregorio_syllable *syllable,
2728 gtex_alignment *type, gtex_alteration *alteration)
2729 {
2730 gtex_type gtype = T_ONE_NOTE;
2731 /* alteration says if there is a flat or a natural first in the next
2732 * syllable, see gregoriotex.tex for more details */
2733 gregorio_glyph *glyph;
2734 gregorio_element *element;
2735 gregorio_assert(type && alteration && syllable && syllable->elements,
2736 syllable_first_type, "called with a NULL argument", return);
2737 *type = AT_ONE_NOTE;
2738 *alteration = ALT_NONE;
2739 for (element = syllable->elements[0]; element; element = element->next) {
2740 if (element->type == GRE_BAR) {
2741 switch (element->u.misc.unpitched.info.bar) {
2742 case B_VIRGULA:
2743 case B_VIRGULA_HIGH:
2744 *type = AT_VIRGULA;
2745 break;
2746 case B_VIRGULA_PAREN:
2747 case B_VIRGULA_PAREN_HIGH:
2748 *type = AT_VIRGULA_PAREN;
2749 break;
2750 case B_DIVISIO_MINIMIS:
2751 case B_DIVISIO_MINIMIS_HIGH:
2752 case B_DIVISIO_MINIMA:
2753 case B_DIVISIO_MINIMA_HIGH:
2754 *type = AT_DIVISIO_MINIMA;
2755 break;
2756 case B_DIVISIO_MINIMA_PAREN:
2757 case B_DIVISIO_MINIMA_PAREN_HIGH:
2758 *type = AT_DIVISIO_MINIMA_PAREN;
2759 break;
2760 case B_DIVISIO_MINOR:
2761 *type = AT_DIVISIO_MINOR;
2762 break;
2763 case B_DIVISIO_MAIOR:
2764 case B_DIVISIO_MAIOR_DOTTED:
2765 *type = AT_DIVISIO_MAIOR;
2766 break;
2767 case B_DIVISIO_MINOR_D1:
2768 case B_DIVISIO_MINOR_D2:
2769 case B_DIVISIO_MINOR_D3:
2770 case B_DIVISIO_MINOR_D4:
2771 case B_DIVISIO_MINOR_D5:
2772 case B_DIVISIO_MINOR_D6:
2773 case B_DIVISIO_MINOR_D7:
2774 case B_DIVISIO_MINOR_D8:
2775 *type = AT_DIVISIO_DOMINICAN;
2776 break;
2777 case B_DIVISIO_FINALIS:
2778 *type = AT_DIVISIO_FINALIS;
2779 break;
2780 default:
2781 /* not reachable unless there's a programming error */
2782 /* LCOV_EXCL_START */
2783 gregorio_fail(syllable_first_type, "invalid bar");
2784 *type = AT_ONE_NOTE;
2785 break;
2786 /* LCOV_EXCL_STOP */
2787 }
2788 *alteration = ALT_NONE;
2789 return;
2790 }
2791 if (element->type == GRE_ELEMENT && element->u.first_glyph) {
2792 for (glyph = element->u.first_glyph; glyph; glyph = glyph->next) {
2793 if (glyph->type == GRE_GLYPH && glyph->u.notes.first_note) {
2794 switch (glyph->u.notes.glyph_type) {
2795 case G_ALTERATION:
2796 if (*alteration == ALT_NONE) {
2797 switch (glyph->u.notes.first_note->u.note.shape) {
2798 case S_FLAT:
2799 *alteration = ALT_FLAT;
2800 break;
2801 case S_FLAT_PAREN:
2802 *alteration = ALT_FLAT_PAREN;
2803 break;
2804 case S_FLAT_SOFT:
2805 *alteration = ALT_FLAT_SOFT;
2806 break;
2807 case S_NATURAL:
2808 *alteration = ALT_NATURAL;
2809 break;
2810 case S_NATURAL_PAREN:
2811 *alteration = ALT_NATURAL_PAREN;
2812 break;
2813 case S_NATURAL_SOFT:
2814 *alteration = ALT_NATURAL_SOFT;
2815 break;
2816 case S_SHARP:
2817 *alteration = ALT_SHARP;
2818 break;
2819 case S_SHARP_PAREN:
2820 *alteration = ALT_SHARP_PAREN;
2821 break;
2822 case S_SHARP_SOFT:
2823 *alteration = ALT_SHARP_SOFT;
2824 break;
2825 default:
2826 /* not reachable unless there's a programming error */
2827 /* LCOV_EXCL_START */
2828 gregorio_fail(syllable_first_type,
2829 "invalid alteration");
2830 break;
2831 /* LCOV_EXCL_STOP */
2832 }
2833 }
2834 continue;
2835 case G_PUNCTA_INCLINATA:
2836 case G_2_PUNCTA_INCLINATA_DESCENDENS:
2837 case G_3_PUNCTA_INCLINATA_DESCENDENS:
2838 case G_4_PUNCTA_INCLINATA_DESCENDENS:
2839 case G_5_PUNCTA_INCLINATA_DESCENDENS:
2840 case G_2_PUNCTA_INCLINATA_ASCENDENS:
2841 case G_3_PUNCTA_INCLINATA_ASCENDENS:
2842 case G_4_PUNCTA_INCLINATA_ASCENDENS:
2843 case G_5_PUNCTA_INCLINATA_ASCENDENS:
2844 case G_PUNCTUM:
2845 case G_STROPHA:
2846 case G_VIRGA:
2847 case G_VIRGA_REVERSA:
2848 case G_STROPHA_AUCTA:
2849 case G_DISTROPHA:
2850 case G_DISTROPHA_AUCTA:
2851 case G_TRISTROPHA:
2852 case G_TRISTROPHA_AUCTA:
2853 case G_BIVIRGA:
2854 case G_TRIVIRGA:
2855 determine_note_glyph_name
2856 (glyph->u.notes.first_note, glyph, type);
2857 break;
2858 default:
2859 gregoriotex_determine_glyph_name(glyph, type, &gtype);
2860 break;
2861 }
2862 return;
2863 }
2864 }
2865 }
2866 }
2867 if (syllable->elements[0]) {
2868 /* if there is anything else in the next syllable */
2869 *alteration = ALT_NONE;
2870 *type = AT_ONE_NOTE;
2871 return;
2872 }
2873 /* the only thing that should reach this point is an empty next syllable
2874 * we treat that kind of syllable as a bar */
2875 *alteration = ALT_NONE;
2876 *type = AT_EMPTY_SYLLABLE;
2877 return;
2878 }
2879
2880 static __inline void write_low_choral_sign(FILE *const f,
2881 const gregorio_note *const note, int special)
2882 {
2883 if (note->choral_sign_is_nabc) {
2884 fprintf(f, "\\GreLowChoralSign{%d}{\\GreNABCChar{",
2885 pitch_value(note->u.note.pitch));
2886 tex_escape_text(f, note->choral_sign);
2887 fprintf(f, "}}{%d}%%\n", special);
2888 } else {
2889 fprintf(f, "\\GreLowChoralSign{%d}{%s}{%d}%%\n",
2890 pitch_value(note->u.note.pitch), note->choral_sign,
2891 special);
2892 }
2893 }
2894
2895 static __inline void write_high_choral_sign(FILE *const f,
2896 const gregorio_note *const note, int pitch_offset)
2897 {
2898 if (note->choral_sign_is_nabc) {
2899 fprintf(f, "\\GreHighChoralSign{%d}{\\GreNABCChar{",
2900 pitch_value(note->u.note.pitch + pitch_offset));
2901 tex_escape_text(f, note->choral_sign);
2902 fprintf(f, "}}{\\GreOCase%s}%%\n", note->gtex_offset_case);
2903 } else {
2904 fprintf(f, "\\GreHighChoralSign{%d}{%s}{\\GreOCase%s}%%\n",
2905 pitch_value(note->u.note.pitch + pitch_offset),
2906 note->choral_sign, note->gtex_offset_case);
2907 }
2908 }
2909
2910 static void write_choral_sign(FILE *f, const gregorio_glyph *glyph,
2911 const gregorio_note *current_note, bool low)
2912 {
2913 bool kind_of_pes;
2914 /* false in the normal case (sign above the note), true in the case of it's
2915 * next to the note (same height as a punctum) */
2916 bool low_sign = choral_sign_here_is_low(glyph, current_note, &kind_of_pes);
2917
2918 /* the low choral signs must be typeset after the punctum, whereas the high
2919 * must be typeset before the h episema */
2920 if ((low_sign && !low) || (!low_sign && low)) {
2921 return;
2922 }
2923
2924 if (low_sign) {
2925 /* very approximative heuristic, some things may have to be adapted
2926 * here... */
2927 if (is_on_a_line(current_note->u.note.pitch)) {
2928 if (kind_of_pes && current_note->u.note.pitch -
2929 current_note->next->u.note.pitch == -1) {
2930 write_low_choral_sign(f, current_note, 1);
2931 return;
2932 }
2933 if (current_note->previous
2934 && (current_note->previous->signs == _PUNCTUM_MORA
2935 || current_note->previous->signs ==
2936 _V_EPISEMA_PUNCTUM_MORA)) {
2937 write_low_choral_sign(f, current_note, 1);
2938 return;
2939 }
2940 }
2941
2942 write_low_choral_sign(f, current_note, 0);
2943 } else {
2944 /* let's cheat a little */
2945 if (is_on_a_line(current_note->u.note.pitch)) {
2946 write_high_choral_sign(f, current_note, 0);
2947 } else {
2948 write_high_choral_sign(f, current_note, 2);
2949 }
2950 }
2951 }
2952
2953 /*
2954 *
2955 * A function that write the signs of a glyph, which has the type type (T_*,
2956 * not G_*, which is in the glyph->glyph_type), and (important), we start only
2957 * at the note current_note. It is due to the way we call it : if type is
2958 * T_ONE_NOTE, we just do the signs on current_note, not all. This is the case
2959 * for example for the first note of the torculus resupinus, or the
2960 * G_*_PUNCTA_INCLINATA.
2961 *
2962 */
2963
2964 /* small helper */
2965 static __inline bool _found(FILE *const f, const bool found)
2966 {
2967 if (!found) {
2968 fprintf (f, "%%\n");\
2969 return true;
2970 }
2971 return found;
2972 }
2973
2974 static void compute_height_extrema(const gregorio_glyph *const glyph,
2975 const gregorio_note *note, signed char *const top_height,
2976 signed char *const bottom_height)
2977 {
2978 char height;
2979 /* get the minima/maxima pitches */
2980 for (; note; note = note->next) {
2981 if (note->h_episema_above) {
2982 height = note->h_episema_above;
2983 } else {
2984 if (note->v_episema_height && note->v_episema_height
2985 > note->u.note.pitch) {
2986 height = note->v_episema_height;
2987 } else {
2988 height = note->u.note.pitch;
2989 }
2990
2991 if (note->choral_sign
2992 && !choral_sign_here_is_low(glyph, note, NULL)) {
2993 ++height;
2994 }
2995 }
2996 if (*top_height == UNDETERMINED_HEIGHT || height > *top_height) {
2997 *top_height = height;
2998 }
2999
3000 if (note->h_episema_below) {
3001 height = note->h_episema_below;
3002 } else if (note->v_episema_height && note->v_episema_height
3003 < note->u.note.pitch) {
3004 height = note->v_episema_height;
3005 } else {
3006 height = note->u.note.pitch;
3007
3008 /* no vertical episema, so we need to check for a stem */
3009 gregorio_assert(glyph->type == GRE_GLYPH, compute_height_extrema,
3010 "glyph->type must be GRE_GLYPH here", return);
3011 switch (glyph->u.notes.glyph_type) {
3012 case G_BIVIRGA:
3013 case G_TRIVIRGA:
3014 case G_VIRGA:
3015 case G_VIRGA_REVERSA:
3016 --height;
3017 break;
3018
3019 case G_PUNCTUM:
3020 switch (note->u.note.shape) {
3021 case S_ORISCUS_SCAPUS_ASCENDENS:
3022 case S_ORISCUS_SCAPUS_DESCENDENS:
3023 --height;
3024 break;
3025
3026 default:
3027 /* default to avoid the warning */
3028 break;
3029 }
3030 break;
3031
3032 default:
3033 /* default to avoid the warning */
3034
3035 /* other shapes like the porrectus have stems, but the height
3036 * computation will cover other notes in the shape, negating
3037 * the need to account for the stem on the shape */
3038 break;
3039 }
3040 }
3041
3042 if (*bottom_height == UNDETERMINED_HEIGHT || height < *bottom_height) {
3043 *bottom_height = height;
3044 }
3045 }
3046 }
3047
3048 static void compute_element_height_extrema(
3049 const gregorio_element *const element, signed char *const top_height,
3050 signed char *const bottom_height)
3051 {
3052 const gregorio_glyph *glyph;
3053 gregorio_not_null(element, compute_element_height_extrema, return);
3054 if (element->type != GRE_ELEMENT) {
3055 return;
3056 }
3057 /* get the minima/maxima pitches */
3058 for (glyph = element->u.first_glyph; glyph; glyph = glyph->next) {
3059 if (glyph->type == GRE_GLYPH) {
3060 compute_height_extrema(glyph, glyph->u.notes.first_note,
3061 top_height, bottom_height);
3062 }
3063 }
3064 }
3065
3066 static __inline void fixup_height_extrema(signed char *const top_height,
3067 signed char *const bottom_height)
3068 {
3069 if (*top_height == UNDETERMINED_HEIGHT) {
3070 *top_height = 9; /* 'g' */
3071 }
3072 if (*bottom_height == UNDETERMINED_HEIGHT) {
3073 *bottom_height = 9; /* 'g' */
3074 }
3075 }
3076
3077 static __inline bool is_ledger_drawn(const bool setting,
3078 const gregorio_ledger_specificity specificity)
3079 {
3080 return ((specificity & LEDGER_DRAWN) && setting);
3081 }
3082
3083 static void write_signs(FILE *f, gtex_type type,
3084 const gregorio_glyph *glyph, const gregorio_note *note,
3085 int fuse_to_next_note, gregoriotex_status *const status,
3086 const gregorio_score *const score)
3087 {
3088 /* i is the number of the note for which we are typesetting the sign. */
3089 int i;
3090 const gregorio_note *current_note;
3091 signed char high_pitch = UNDETERMINED_HEIGHT;
3092 signed char low_pitch = UNDETERMINED_HEIGHT;
3093 bool found = false;
3094 compute_height_extrema(glyph, note, &high_pitch, &low_pitch);
3095 fixup_height_extrema(&high_pitch, &low_pitch);
3096 fprintf(f, "%%\n{%%\n\\GreGlyphHeights{%d}{%d}%%\n",
3097 pitch_value(high_pitch), pitch_value(low_pitch));
3098 for (current_note = note, i = 1; current_note;
3099 current_note = current_note->next, ++i) {
3100 /* we start by the additional lines */
3101 if (is_ledger_drawn(current_note->low_ledger_line,
3102 current_note->low_ledger_specificity)) {
3103 write_additional_line(f, i, type, true, current_note, score);
3104 status->bottom_line = 1;
3105 }
3106 if (is_ledger_drawn(current_note->high_ledger_line,
3107 current_note->high_ledger_specificity)) {
3108 write_additional_line(f, i, type, false, current_note, score);
3109 }
3110 if (current_note->texverb) {
3111 fprintf(f, "%% verbatim text at note level:\n%s%%\n"
3112 "%% end of verbatim text\n",
3113 gregorio_texverb(current_note->texverb));
3114 }
3115 if (type == T_ONE_NOTE) {
3116 break;
3117 }
3118 }
3119 fprintf(f, "}{");
3120 /* now a first loop for the choral signs, because high signs must be taken
3121 * into account before any hepisema */
3122 for (current_note = note, i = 1; current_note;
3123 current_note = current_note->next, ++i) {
3124 if (current_note->choral_sign) {
3125 found = _found(f, found);
3126 write_choral_sign(f, glyph, current_note, false);
3127 }
3128 if (type == T_ONE_NOTE) {
3129 break;
3130 }
3131 }
3132 /* a loop for rare signs, vertical episema, and horizontal episema */
3133 for (current_note = note, i = 1; current_note;
3134 current_note = current_note->next, ++i) {
3135 /* we continue with the hepisema */
3136 if (current_note->h_episema_above || current_note->h_episema_below) {
3137 found = _found(f, found);
3138 gregoriotex_write_hepisema(f, current_note, i, type, glyph);
3139 }
3140 /* write_rare_sign also writes the vepisema */
3141 if (current_note->special_sign) {
3142 found = _found(f, found);
3143 write_rare_sign(f, current_note, current_note->special_sign);
3144 }
3145 if (current_note->signs != _NO_SIGN) {
3146 found = _found(f, found);
3147 }
3148 switch (current_note->signs) {
3149 case _V_EPISEMA:
3150 case _V_EPISEMA_PUNCTUM_MORA:
3151 case _V_EPISEMA_AUCTUM_DUPLEX:
3152 write_vepisema(f, current_note);
3153 break;
3154 default:
3155 /* do nothing */
3156 break;
3157 }
3158 if (type == T_ONE_NOTE) {
3159 break;
3160 }
3161 }
3162 /* final loop for choral signs and punctum mora */
3163 for (current_note = note, i = 1; current_note;
3164 current_note = current_note->next, ++i) {
3165 switch (current_note->signs) {
3166 case _PUNCTUM_MORA:
3167 case _V_EPISEMA_PUNCTUM_MORA:
3168 write_punctum_mora(f, glyph, current_note, fuse_to_next_note);
3169 break;
3170 case _AUCTUM_DUPLEX:
3171 case _V_EPISEMA_AUCTUM_DUPLEX:
3172 write_auctum_duplex(f, current_note);
3173 break;
3174 default:
3175 break;
3176 }
3177 if (current_note->choral_sign) {
3178 found = _found(f, found);
3179 write_choral_sign(f, glyph, current_note, true);
3180 }
3181 if (type == T_ONE_NOTE) {
3182 break;
3183 }
3184 }
3185 fprintf(f, "}{");
3186 if (status->point_and_click) {
3187 fprintf(f, "%u:%u:%u", note->src_line, note->src_offset,
3188 note->src_column + 1);
3189 }
3190 fprintf(f, "}%%\n");
3191 }
3192
3193 static char *determine_leading_shape(const gregorio_glyph *const glyph)
3194 {
3195 static char buf[BUFSIZE];
3196 int ambitus = compute_ambitus(glyph->u.notes.first_note);
3197 const char *head, *head_liquescence;
3198
3199 switch (glyph->u.notes.first_note->u.note.shape) {
3200 case S_QUILISMA:
3201 head = "Quilisma";
3202 break;
3203 case S_ORISCUS_ASCENDENS:
3204 case S_ORISCUS_DESCENDENS:
3205 head = "Oriscus";
3206 break;
3207 default:
3208 head = "Punctum";
3209 break;
3210 }
3211
3212 if (glyph->u.notes.liquescentia & L_INITIO_DEBILIS) {
3213 head_liquescence = "InitioDebilis";
3214 } else {
3215 head_liquescence = "";
3216 }
3217
3218 gregorio_snprintf(buf, BUFSIZE, "Leading%s%s%s", head, tex_ambitus[ambitus],
3219 head_liquescence);
3220 return buf;
3221 }
3222
3223 static __inline void write_composed_multinote_glyph(FILE *const f,
3224 gregorio_note *current_note, const gregorio_glyph *glyph,
3225 const signed char next_note_pitch,
3226 const gregorio_shape next_note_alteration,
3227 const int fuse_to_next_note, gregoriotex_status *const status,
3228 const gregorio_score *const score, const char *glyph_end_between_notes)
3229 {
3230 while (current_note) {
3231 const bool has_next = current_note->next != NULL;
3232 if (has_next) {
3233 write_note(f, current_note, glyph, current_note->next->u.note.pitch,
3234 S_UNDETERMINED, status);
3235 } else {
3236 write_note(f, current_note, glyph, next_note_pitch,
3237 next_note_alteration, status);
3238 }
3239 write_signs(f, T_ONE_NOTE, glyph, current_note,
3240 has_next ? 0 : fuse_to_next_note, status, score);
3241 current_note = current_note->next;
3242 if (current_note && glyph_end_between_notes) {
3243 fputs(glyph_end_between_notes, f);
3244 }
3245 }
3246 }
3247
3248 static void write_glyph(FILE *const f, const gregorio_syllable *const syllable,
3249 const gregorio_element *const element,
3250 gregorio_glyph *const glyph,
3251 gregoriotex_status *const status, const gregorio_score *const score)
3252 {
3253 static char cpbuf[96], cpbuf2[96];
3254 /* type is the type of the glyph. Understand the type of the glyph for
3255 * gregoriotex, for the alignement between text and notes. (AT_ONE_NOTE,
3256 * etc.) */
3257 gtex_alignment type = 0;
3258 /* the type of the glyph, in the sense of the shape (T_PES, etc.) */
3259 gtex_type gtype = 0;
3260 signed char next_note_pitch = 0;
3261 gregorio_note *current_note;
3262 const char *leading_shape, *shape;
3263 const gregorio_glyph *prev_glyph =
3264 gregorio_previous_non_texverb_glyph(glyph);
3265 int fuse_to_next_note, fuse_from_previous_note =
3266 (prev_glyph && prev_glyph->type == GRE_GLYPH)
3267 ? prev_glyph->u.notes.fuse_to_next_glyph : 0;
3268 gregorio_shape next_note_alteration;
3269 gregorio_assert(glyph, write_glyph, "called with NULL pointer", return);
3270 gregorio_assert(glyph->type == GRE_GLYPH && glyph->u.notes.first_note,
3271 write_glyph, "called with glyph without note", return);
3272 fuse_to_next_note = glyph->u.notes.fuse_to_next_glyph;
3273 if (fuse_from_previous_note) {
3274 fprintf(f, "\\GreFuse");
3275 }
3276 next_note_pitch = gregorio_determine_next_pitch(syllable, element, glyph,
3277 &next_note_alteration);
3278 current_note = glyph->u.notes.first_note;
3279 /* first we check if it is really a unique glyph in gregoriotex... the
3280 * glyphs that are not a unique glyph are : trigonus and pucta inclinata
3281 * in general, and torculus resupinus and torculus resupinus flexus, so
3282 * we first divide the glyph into real gregoriotex glyphs */
3283 switch (glyph->u.notes.glyph_type) {
3284 case G_PUNCTA_INCLINATA:
3285 case G_2_PUNCTA_INCLINATA_DESCENDENS:
3286 case G_3_PUNCTA_INCLINATA_DESCENDENS:
3287 case G_4_PUNCTA_INCLINATA_DESCENDENS:
3288 case G_5_PUNCTA_INCLINATA_DESCENDENS:
3289 case G_2_PUNCTA_INCLINATA_ASCENDENS:
3290 case G_3_PUNCTA_INCLINATA_ASCENDENS:
3291 case G_4_PUNCTA_INCLINATA_ASCENDENS:
3292 case G_5_PUNCTA_INCLINATA_ASCENDENS:
3293 write_composed_multinote_glyph(f, current_note, glyph,
3294 next_note_pitch, next_note_alteration, fuse_to_next_note,
3295 status, score, NULL);
3296 break;
3297 case G_SCANDICUS:
3298 if ((glyph->u.notes.liquescentia & L_DEMINUTUS)
3299 || glyph->u.notes.liquescentia == L_NO_LIQUESCENTIA
3300 || glyph->u.notes.liquescentia == L_FUSED) {
3301 shape = gregoriotex_determine_glyph_name(glyph, &type, &gtype);
3302 fprintf(f, "\\GreGlyph{%s}{%d}{%s}{%d}", code_point(shape,
3303 glyph->u.notes.is_cavum, cpbuf, sizeof cpbuf),
3304 pitch_value(glyph->u.notes.first_note->u.note.pitch),
3305 next_custos(next_note_pitch, next_note_alteration, status),
3306 type);
3307 write_signs(f, gtype, glyph, glyph->u.notes.first_note,
3308 fuse_to_next_note, status, score);
3309 } else {
3310 write_composed_multinote_glyph(f, current_note, glyph,
3311 next_note_pitch, next_note_alteration, fuse_to_next_note,
3312 status, score, NULL);
3313 }
3314 break;
3315 case G_ANCUS:
3316 gregorio_assert(glyph->u.notes.liquescentia & L_DEMINUTUS,
3317 write_glyph, "encountered a non-deminutus ancus", break);
3318 shape = gregoriotex_determine_glyph_name(glyph, &type, &gtype);
3319 fprintf(f, "\\GreGlyph{%s}{%d}{%s}{%d}", code_point(shape,
3320 glyph->u.notes.is_cavum, cpbuf, sizeof cpbuf),
3321 pitch_value(glyph->u.notes.first_note->u.note.pitch),
3322 next_custos(next_note_pitch, next_note_alteration, status),
3323 type);
3324 write_signs(f, gtype, glyph, glyph->u.notes.first_note,
3325 fuse_to_next_note, status, score);
3326 break;
3327 case G_TORCULUS_RESUPINUS_FLEXUS:
3328 /* we retain this "old-style" fusion as it does look marginally better
3329 * on screen */
3330 leading_shape = determine_leading_shape(glyph);
3331 /* trick to have the good position for these glyphs */
3332 glyph->u.notes.glyph_type = G_PORRECTUS_FLEXUS_NO_BAR;
3333 glyph->u.notes.first_note = current_note->next;
3334 shape = gregoriotex_determine_glyph_name(glyph, &type, &gtype);
3335 fprintf(f, "\\GreGlyph{\\GreFuseTwo{%s}{%s}}{%d}{%s}{%d}",
3336 code_point(leading_shape, glyph->u.notes.is_cavum, cpbuf,
3337 sizeof cpbuf),
3338 code_point(shape, glyph->u.notes.is_cavum, cpbuf2,
3339 sizeof cpbuf2),
3340 pitch_value(glyph->u.notes.first_note->u.note.pitch),
3341 next_custos(next_note_pitch, next_note_alteration, status),
3342 type);
3343 glyph->u.notes.first_note = current_note;
3344 glyph->u.notes.glyph_type = G_TORCULUS_RESUPINUS_FLEXUS;
3345 write_signs(f, gtype, glyph, glyph->u.notes.first_note,
3346 fuse_to_next_note, status, score);
3347 break;
3348 case G_BIVIRGA:
3349 case G_TRIVIRGA:
3350 write_composed_multinote_glyph(f, current_note, glyph,
3351 next_note_pitch, next_note_alteration, fuse_to_next_note,
3352 status, score, "\\GreEndOfGlyph{4}%%\n");
3353 break;
3354 case G_TRISTROPHA:
3355 case G_TRISTROPHA_AUCTA:
3356 case G_DISTROPHA:
3357 case G_DISTROPHA_AUCTA:
3358 write_composed_multinote_glyph(f, current_note, glyph,
3359 next_note_pitch, next_note_alteration, fuse_to_next_note,
3360 status, score, "\\GreEndOfGlyph{5}%%\n");
3361 break;
3362
3363 case G_PUNCTUM:
3364 switch (glyph->u.notes.first_note->u.note.shape) {
3365 case S_ORISCUS_ASCENDENS:
3366 case S_ORISCUS_DESCENDENS:
3367 case S_ORISCUS_DEMINUTUS:
3368 case S_ORISCUS_SCAPUS_ASCENDENS:
3369 case S_ORISCUS_SCAPUS_DESCENDENS:
3370 /* don't change the oriscus */
3371 break;
3372
3373 default:
3374 switch (glyph->u.notes.liquescentia) {
3375 case L_AUCTUS_ASCENDENS:
3376 glyph->u.notes.first_note->u.note.shape =
3377 S_PUNCTUM_AUCTUS_ASCENDENS;
3378 break;
3379 case L_AUCTUS_DESCENDENS:
3380 glyph->u.notes.first_note->u.note.shape =
3381 S_PUNCTUM_AUCTUS_DESCENDENS;
3382 break;
3383 case L_INITIO_DEBILIS:
3384 if (glyph->u.notes.fuse_to_next_glyph > 0) {
3385 break;
3386 }
3387 /* fall through */
3388 case L_DEMINUTUS:
3389 glyph->u.notes.first_note->u.note.shape = S_PUNCTUM_DEMINUTUS;
3390 default:
3391 break;
3392 }
3393 break;
3394 }
3395
3396 /* fall through */
3397 case G_PUNCTUM_INCLINATUM:
3398 case G_VIRGA:
3399 case G_VIRGA_REVERSA:
3400 case G_STROPHA:
3401 case G_STROPHA_AUCTA:
3402 case G_ALTERATION:
3403 write_note(f, glyph->u.notes.first_note, glyph, next_note_pitch,
3404 next_note_alteration, status);
3405 write_signs(f, T_ONE_NOTE, glyph, current_note, fuse_to_next_note,
3406 status, score);
3407 break;
3408 default:
3409 /* we retain this "old-style" fusion as it does look marginally better
3410 * on screen */
3411 /* special case of the torculus resupinus which first note is not a
3412 * punctum */
3413 if (glyph->u.notes.glyph_type == G_TORCULUS_RESUPINUS
3414 && current_note->u.note.shape != S_PUNCTUM
3415 && current_note->u.note.shape != S_QUILISMA) {
3416 leading_shape = determine_leading_shape(glyph);
3417 /* trick to have the good position for these glyphs */
3418 glyph->u.notes.glyph_type = G_PORRECTUS_NO_BAR;
3419 glyph->u.notes.first_note = current_note->next;
3420 shape = gregoriotex_determine_glyph_name(glyph, &type, &gtype);
3421 fprintf(f, "\\GreGlyph{\\GreFuseTwo{%s}{%s}}{%d}{%s}{%d}",
3422 code_point(leading_shape, glyph->u.notes.is_cavum, cpbuf,
3423 sizeof cpbuf),
3424 code_point(shape, glyph->u.notes.is_cavum, cpbuf2,
3425 sizeof cpbuf2),
3426 pitch_value(glyph->u.notes.first_note->u.note.pitch),
3427 next_custos(next_note_pitch, next_note_alteration, status),
3428 type);
3429 glyph->u.notes.first_note = current_note;
3430 glyph->u.notes.glyph_type = G_TORCULUS_RESUPINUS;
3431 write_signs(f, gtype, glyph, glyph->u.notes.first_note,
3432 fuse_to_next_note, status, score);
3433 break;
3434 } else {
3435 shape = gregoriotex_determine_glyph_name(glyph, &type, &gtype);
3436 fprintf(f, "\\GreGlyph{%s}{%d}{%s}{%d}", code_point(shape,
3437 glyph->u.notes.is_cavum, cpbuf, sizeof cpbuf),
3438 pitch_value(glyph->u.notes.first_note->u.note.pitch),
3439 next_custos(next_note_pitch, next_note_alteration, status),
3440 type);
3441 write_signs(f, gtype, glyph, glyph->u.notes.first_note,
3442 fuse_to_next_note, status, score);
3443 break;
3444 }
3445 }
3446 }
3447
3448 static __inline unsigned int glyph_note_units(const gregorio_glyph *glyph)
3449 {
3450 unsigned int count = 0;
3451 const gregorio_note *note;
3452 if (glyph->u.notes.glyph_type != G_ALTERATION) {
3453 for (note = glyph->u.notes.first_note; note; note = note->next) {
3454 ++count;
3455 }
3456 }
3457 return count;
3458 }
3459
3460 /* here we absolutely need to pass the syllable as an argument, because we
3461 * will need the next note, that may be contained in the next syllable */
3462
3463 static unsigned int write_element(FILE *const f,
3464 const gregorio_syllable *const syllable,
3465 const gregorio_element *const element,
3466 gregoriotex_status *const status,
3467 const gregorio_score *const score)
3468 {
3469 unsigned int note_unit_count = 0;
3470 if (element->type == GRE_ELEMENT) {
3471 gregorio_glyph *glyph;
3472 for (glyph = element->u.first_glyph; glyph; glyph = glyph->next) {
3473 switch (glyph->type) {
3474 case GRE_SPACE:
3475 switch (glyph->u.misc.unpitched.info.space) {
3476 case SP_ZERO_WIDTH:
3477 fprintf(f, "\\GreEndOfGlyph{1}%%\n");
3478 break;
3479 case SP_HALF_SPACE:
3480 fprintf(f, "\\GreEndOfGlyph{22}%%\n");
3481 break;
3482 case SP_INTERGLYPH_SPACE:
3483 fprintf(f, "\\GreEndOfGlyph{0}%%\n");
3484 break;
3485 default:
3486 /* not reachable unless there's a programming error */
3487 /* LCOV_EXCL_START */
3488 gregorio_fail(write_element,
3489 "encountered an unexpected glyph-level space");
3490 break;
3491 /* LCOV_EXCL_STOP */
3492 }
3493 break;
3494
3495 case GRE_TEXVERB_GLYPH:
3496 if (glyph->texverb) {
3497 fprintf(f, "%% verbatim text at glyph level:\n%s%%\n"
3498 "%% end of verbatim text\n",
3499 gregorio_texverb(glyph->texverb));
3500 }
3501 break;
3502
3503 default:
3504 /* at this point glyph->type is GRE_GLYPH */
3505 assert(glyph->type == GRE_GLYPH);
3506 write_glyph(f, syllable, element, glyph, status, score);
3507 note_unit_count += glyph_note_units(glyph);
3508 if (glyph->next && glyph->next->type == GRE_GLYPH &&
3509 glyph->next->u.notes.glyph_type != G_ALTERATION) {
3510 if (is_fused(glyph->next->u.notes.liquescentia)) {
3511 int space_case =
3512 get_punctum_inclinatum_to_nobar_space_case(glyph);
3513 if (space_case >= 0) {
3514 /* fuse from punctum inclinatum to nobar glyph */
3515 fprintf(f, "\\GreEndOfGlyph{%d}%%\n", space_case);
3516 } else {
3517 fprintf(f, "\\GreEndOfGlyph{1}%%\n");
3518 }
3519 } else if (is_puncta_inclinata(
3520 glyph->next->u.notes.glyph_type)
3521 || glyph->next->u.notes.glyph_type
3522 == G_PUNCTA_INCLINATA) {
3523 if ((is_puncta_inclinata(glyph->u.notes.glyph_type)
3524 || glyph->u.notes.glyph_type
3525 == G_PUNCTA_INCLINATA)
3526 && glyph->next->u.notes.first_note->u.note.pitch
3527 == gregorio_glyph_last_note(glyph)->u.note.pitch) {
3528 /* special case for unison puncta inclinata */
3529 fprintf(f, "\\GreEndOfGlyph{23}%%\n");
3530 } else {
3531 fprintf(f, "\\GreEndOfGlyph{9}%%\n");
3532 }
3533 } else if (glyph->u.notes.glyph_type != G_ALTERATION
3534 || !glyph->next) {
3535 fprintf(f, "\\GreEndOfGlyph{0}%%\n");
3536 }
3537 }
3538 break;
3539 }
3540 }
3541 }
3542 return note_unit_count;
3543 }
3544
3545 static void write_fixed_text_styles(FILE *f, gregorio_character *syllable_text,
3546 gregorio_character *next_syllable_text)
3547 {
3548 if (syllable_text) {
3549 gregoriotex_ignore_style = gregoriotex_fix_style(syllable_text);
3550 if (gregoriotex_ignore_style) {
3551 fprintf(f, "\\GreSetFixedTextFormat{%d}",
3552 gregoriotex_internal_style_to_gregoriotex
3553 (gregoriotex_ignore_style));
3554 }
3555 }
3556 if (next_syllable_text) {
3557 gregoriotex_next_ignore_style = gregoriotex_fix_style(
3558 next_syllable_text);
3559 if (gregoriotex_next_ignore_style) {
3560 fprintf(f, "\\GreSetFixedNextTextFormat{%d}",
3561 gregoriotex_internal_style_to_gregoriotex
3562 (gregoriotex_next_ignore_style));
3563 }
3564 }
3565 }
3566
3567 static void write_text(FILE *const f, const gregorio_character *const text)
3568 {
3569 if (text == NULL) {
3570 fprintf(f, "{}{}{}{}{}");
3571 return;
3572 }
3573 fprintf(f, "{");
3574 gregorio_write_text(WTP_NORMAL, text, f, &gtex_write_verb,
3575 &gtex_print_char, &gtex_write_begin, &gtex_write_end,
3576 &gtex_write_special_char);
3577 fprintf(f, "}{");
3578 gregorio_write_first_letter_alignment_text(WTP_NORMAL, text,
3579 f, &gtex_write_verb, &gtex_print_char, &gtex_write_begin,
3580 &gtex_write_end, &gtex_write_special_char);
3581 gregoriotex_ignore_style = gregoriotex_next_ignore_style;
3582 gregoriotex_next_ignore_style = ST_NO_STYLE;
3583 fprintf(f, "}");
3584 }
3585
3586 /*
3587 * Function printing the line clef change (only updating \localleftbox, not
3588 * printing the key). Useful for \GreDiscretionary.
3589 * TODO: I'm not sure about the third argument, but that's how it's called in
3590 * \GreChangeClef.
3591 */
3592 static void gregoriotex_print_change_line_clef(FILE *f,
3593 const gregorio_element *const current_element)
3594 {
3595 if (current_element->type == GRE_CLEF) {
3596 /* the third argument is 0 or 1 according to the need for a space
3597 * before the clef */
3598 fprintf(f, "\\GreSetLinesClef{%c}{%d}{1}{%d}{%c}{%d}{%d}%%\n",
3599 gregorio_clef_to_char(current_element->u.misc.clef.clef),
3600 current_element->u.misc.clef.line,
3601 clef_flat_height(current_element->u.misc.clef.clef,
3602 current_element->u.misc.clef.line,
3603 current_element->u.misc.clef.flatted),
3604 gregorio_clef_to_char(
3605 current_element->u.misc.clef.secondary_clef),
3606 current_element->u.misc.clef.secondary_line,
3607 clef_flat_height(current_element->u.misc.clef.secondary_clef,
3608 current_element->u.misc.clef.secondary_line,
3609 current_element->u.misc.clef.secondary_flatted));
3610 }
3611 }
3612
3613 static __inline bool next_is_bar(const gregorio_syllable *syllable,
3614 const gregorio_element *element)
3615 {
3616 bool got_custos = false;
3617 if (element) {
3618 element = element->next;
3619 }
3620
3621 while (syllable) {
3622 if (element) {
3623 if (element->type == GRE_BAR) {
3624 return true;
3625 }
3626 /* allow no more than one manual custos before a bar */
3627 if (got_custos || !is_manual_custos(element)) {
3628 return false;
3629 }
3630 got_custos = true;
3631 if (element->next) {
3632 /* look at the next element */
3633 element = element->next;
3634 continue;
3635 }
3636 }
3637
3638 syllable = syllable->next_syllable;
3639 if (!syllable) {
3640 return false;
3641 }
3642
3643 element = syllable->elements[0];
3644 }
3645
3646 /* not reachable unless there's a programming error */
3647 /* LCOV_EXCL_START */
3648 gregorio_fail(next_is_bar, "unexpected end of syllables/elements");
3649 return false;
3650 /* LCOV_EXCL_STOP */
3651 }
3652
3653 static void finish_syllable(FILE *f, gregorio_syllable *syllable) {
3654 /* Very last, if the syllable is the end of a no-linebreak area: */
3655 if (syllable->no_linebreak_area == NLBA_END) {
3656 fprintf(f, "\\GreEndNLBArea{%d}{0}%%\n",
3657 next_is_bar(syllable, NULL)? 3 : 1);
3658 }
3659 if (syllable->euouae == EUOUAE_END) {
3660 fprintf(f, "\\GreEndEUOUAE{%d}%%\n",
3661 next_is_bar(syllable, NULL)? 3 : 1);
3662 }
3663 }
3664
3665 static void handle_final_bar(FILE *f, const char *type, gregorio_syllable *syllable)
3666 {
3667 gregorio_element *element;
3668 fprintf(f, "\\GreFinal%s{%%\n", type);
3669 /* first element will be the bar, which we just handled, so skip it */
3670 for (element = (*syllable->elements)->next; element;
3671 element = element->next) {
3672 switch (element->type) {
3673 case GRE_TEXVERB_ELEMENT:
3674 if (element->texverb) {
3675 fprintf(f, "%% verbatim text at element level:\n%s%%\n"
3676 "%% end of verbatim text\n",
3677 gregorio_texverb(element->texverb));
3678 }
3679 break;
3680
3681 case GRE_CUSTOS:
3682 assert(element->u.misc.pitched.force_pitch);
3683 fprintf(f, "\\GreFinalCustos{%d}{}%%\n",
3684 pitch_value(element->u.misc.pitched.pitch));
3685 break;
3686
3687 case GRE_END_OF_LINE:
3688 fprintf(f, "\\GreFinalNewLine%%\n");
3689 break;
3690
3691 default:
3692 /* do nothing */
3693 break;
3694 }
3695 }
3696 fprintf(f, "}%%\n");
3697 finish_syllable(f, syllable);
3698 }
3699
3700 static __inline void write_syllable_point_and_click(FILE *const f,
3701 const gregorio_syllable *const syllable,
3702 const gregoriotex_status *const status)
3703 {
3704 if (status->point_and_click && syllable->src_line) {
3705 fprintf(f, "%u:%u:%u", syllable->src_line, syllable->src_offset,
3706 syllable->src_column + 1);
3707 }
3708 }
3709
3710 static void write_syllable_text(FILE *f, const char *const syllable_type,
3711 const gregorio_syllable *const syllable,
3712 const bool ignored __attribute__((unused)))
3713 {
3714 if (syllable_type != NULL) {
3715 fprintf(f, "%s{%s%s\\GreSetThisSyllable", syllable_type,
3716 syllable->clear? "\\GreClearSyllableText" : "",
3717 syllable->forced_center? "\\GreGABCForceCenters" : "");
3718 write_text(f, syllable->text);
3719 fprintf(f, "}");
3720 }
3721 }
3722
3723 static void write_first_syllable_text(FILE *f, const char *const syllable_type,
3724 const gregorio_syllable *const syllable,
3725 const bool end_of_word)
3726 {
3727 const gregorio_character *const text = syllable->text;
3728 gregorio_not_null(syllable_type, write_first_syllable_text, return);
3729 if (text == NULL) {
3730 fprintf(f, "}{%s}{\\GreSetNoFirstSyllableText}", syllable_type);
3731 } else {
3732 gregorio_character *text_with_initial = gregorio_clone_characters(text),
3733 *text_without_initial = gregorio_clone_characters(text);
3734 const gregorio_character *t;
3735
3736 /* find out if there is a forced center -> has_forced_center */
3737 gregorio_center_determination center = syllable->forced_center?
3738 CENTER_FULLY_DETERMINED : CENTER_NOT_DETERMINED;
3739
3740 gregorio_rebuild_first_syllable(&text_with_initial, false);
3741 gregorio_rebuild_characters(&text_with_initial, center, false);
3742 gregorio_set_first_word(&text_with_initial);
3743
3744 gregorio_rebuild_first_syllable(&text_without_initial, true);
3745 gregorio_rebuild_characters(&text_without_initial, center, true);
3746 gregorio_set_first_word(&text_without_initial);
3747
3748 fprintf(f, "}{%s}{%s%s\\GreSetFirstSyllableText", syllable_type,
3749 syllable->clear? "\\GreClearSyllableText" : "",
3750 syllable->forced_center? "\\GreGABCForceCenters" : "");
3751
3752 fprintf(f, "{");
3753 gregorio_write_first_letter_alignment_text(WTP_FIRST_SYLLABLE,
3754 text_with_initial, f, &gtex_write_verb, &gtex_print_char,
3755 &gtex_write_begin, &gtex_write_end, &gtex_write_special_char);
3756 fprintf(f, "}{{");
3757 gregorio_write_text(WTP_FIRST_SYLLABLE, text_without_initial, f,
3758 &gtex_write_verb, &gtex_print_char, &gtex_write_begin,
3759 &gtex_write_end, &gtex_write_special_char);
3760 fprintf(f, "}}{{");
3761 gregorio_write_text(WTP_NORMAL, text_with_initial, f, &gtex_write_verb,
3762 &gtex_print_char, &gtex_write_begin, &gtex_write_end,
3763 &gtex_write_special_char);
3764 gregoriotex_ignore_style = gregoriotex_next_ignore_style;
3765 gregoriotex_next_ignore_style = ST_NO_STYLE;
3766 fprintf(f, "}}");
3767
3768 /* Check to see if we need to force a hyphen (empty first syllable) */
3769 for (t = text_without_initial; t; t = t->next_character) {
3770 if (t->is_character) {
3771 break;
3772 } else if (t->cos.s.type == ST_T_BEGIN) {
3773 if (t->cos.s.style == ST_VERBATIM ||
3774 t->cos.s.style == ST_SPECIAL_CHAR) {
3775 break;
3776 } else if (t->cos.s.style == ST_INITIAL) {
3777 for (; t; t = t->next_character) {
3778 if (!t->is_character && t->cos.s.type == ST_T_END
3779 && t->cos.s.style == ST_INITIAL) {
3780 break;
3781 }
3782 }
3783 if (!t) break;
3784 }
3785 }
3786 }
3787 if (t || end_of_word) {
3788 fprintf(f, "{}");
3789 } else {
3790 fprintf(f, "{\\GreEmptyFirstSyllableHyphen}");
3791 }
3792
3793 fprintf(f, "}");
3794
3795 gregorio_free_characters(text_with_initial);
3796 gregorio_free_characters(text_without_initial);
3797 }
3798 }
3799
3800 static __inline void scan_syllable_for_eol(
3801 const gregorio_syllable *const syllable, char *const eol_forces_custos)
3802 {
3803 const gregorio_element *element;
3804 if (syllable->elements) {
3805 for (element = *(syllable->elements); element; element = element->next) {
3806 if (element->type == GRE_END_OF_LINE) {
3807 if (element->u.misc.unpitched.info.eol_forces_custos) {
3808 *eol_forces_custos = element->u.misc.unpitched.info
3809 .eol_forces_custos_on? '1' : '0';
3810 }
3811 }
3812 }
3813 }
3814 }
3815
3816 /*
3817 * euouae_follows will be
3818 * - '\0' if no euouae follows
3819 * - '0' if euouae follows with no intervening linebreak
3820 * - '1' if euouae follows with an intervening linebreak
3821 *
3822 * eol_forces_custos will be
3823 * - '\0' if no linebreak follows or doesn't force a custos
3824 * - '0' if a linebreak follows and forces custos off
3825 * - '1' if a linebreak follows and forces custos on
3826 */
3827 static __inline void anticipate_event(gregorio_syllable *syllable,
3828 char *const euouae_follows, char *const eol_forces_custos,
3829 unsigned short *const next_euouae_id)
3830 {
3831 bool has_intervening_linebreak = false;
3832
3833 *euouae_follows = '\0';
3834 *eol_forces_custos = '\0';
3835 *next_euouae_id = 0;
3836
3837 if (syllable->next_syllable) {
3838 for (syllable = syllable->next_syllable;
3839 syllable && syllable->elements && *(syllable->elements)
3840 && (*(syllable->elements))->type == GRE_END_OF_LINE;
3841 syllable = syllable->next_syllable) {
3842 has_intervening_linebreak = true;
3843 /* we are at an end-of-line, so check if custos is forced */
3844 scan_syllable_for_eol(syllable, eol_forces_custos);
3845 }
3846 if (syllable) {
3847 scan_syllable_for_eol(syllable, eol_forces_custos);
3848
3849 if (syllable->euouae == EUOUAE_BEGINNING) {
3850 *next_euouae_id = syllable->euouae_id = ++tex_position_id;
3851 *euouae_follows = has_intervening_linebreak? '1' : '0';
3852 }
3853 }
3854 }
3855 }
3856
3857 static __inline void write_anticipated_event(FILE *f, const char euouae_follows,
3858 const char eol_forces_custos, const short next_euouae_id)
3859 {
3860 if (euouae_follows) {
3861 fprintf(f, "%%\n\\GreNextSyllableBeginsEUOUAE{%hu}{%c}%%\n",
3862 next_euouae_id, euouae_follows);
3863 }
3864 if (eol_forces_custos) {
3865 fprintf(f, "%%\n\\GreUpcomingNewLineForcesCustos{%c}%%\n",
3866 eol_forces_custos);
3867 }
3868 }
3869
3870 static __inline unsigned int count_note_units(const gregorio_element *element)
3871 {
3872 unsigned int count = 0;
3873 for (; element; element = element->next) {
3874 if (element->type == GRE_ELEMENT) {
3875 const gregorio_glyph *glyph;
3876 for (glyph = element->u.first_glyph; glyph; glyph = glyph->next) {
3877 if (glyph->type == GRE_GLYPH) {
3878 count += glyph_note_units(glyph);
3879 }
3880 }
3881 }
3882 }
3883 return count;
3884 }
3885
3886 static __inline void handle_last_of_voice(FILE *const f,
3887 const gregorio_syllable *syllable,
3888 const gregorio_element *const element,
3889 const gregorio_element *const last_of_voice)
3890 {
3891 if (element == last_of_voice) {
3892 if (syllable->next_syllable) {
3893 /* check for no-element syllables that follow */
3894 for (syllable = syllable->next_syllable;
3895 syllable && (!syllable->elements || !*(syllable->elements));
3896 syllable = syllable->next_syllable) {
3897 /* just loop */
3898 }
3899 /* if syllable is NULL here, then all syllables that follow
3900 * have no elements */
3901 }
3902 /* emit GreLastOfScore if we are either on the last syllable (and
3903 * thus the loop above is not executed, leaving syllable at the
3904 * current syllable) or if a syllable which follows the current
3905 * syllable has an element of some sort (and thus the loop above
3906 * stopped before running out of syllables); in any case, the check
3907 * is that syllable, at this point, is not NULL */
3908 if (syllable) {
3909 fprintf(f, "\\GreLastOfScore");
3910 }
3911 }
3912 }
3913
3914 static bool is_before_linebreak(const gregorio_syllable *syllable,
3915 const gregorio_element *element)
3916 {
3917 if (element) {
3918 element = element->next;
3919 }
3920
3921 while (syllable) {
3922 if (element) {
3923 if (element->type == GRE_END_OF_LINE) {
3924 return true;
3925 }
3926 break;
3927 }
3928
3929 syllable = syllable->next_syllable;
3930 if (syllable) {
3931 element = syllable->elements[0];
3932 }
3933 }
3934
3935 return false;
3936 }
3937
3938 static void write_default_end_of_element(FILE *f,
3939 const gregorio_element *const element,
3940 const unsigned int note_unit_count) {
3941 const gregorio_element *next_element;
3942 const gregorio_glyph *last_glyph;
3943 const gregorio_note *last_note, *next_note;
3944 signed char last_pitch = NO_PITCH;
3945 int break_flag = 0;
3946
3947 /* This is only used in write_syllable, so element is assumed to be:
3948 * 1. not NULL
3949 * 2. of type GRE_ELEMENT
3950 */
3951 if (element->next) {
3952 next_element = element->next;
3953 if (next_element->type == GRE_ALT) {
3954 next_element = next_element->next;
3955 }
3956 if (next_element && next_element->type == GRE_ELEMENT) {
3957 for (last_glyph = element->u.first_glyph; last_glyph->next;
3958 last_glyph = last_glyph->next) {
3959 /* iterate to find the last glyph */
3960 }
3961 if (last_glyph->type == GRE_GLYPH) {
3962 last_note = gregorio_glyph_last_note(last_glyph);
3963 assert(last_note != NULL);
3964 if (last_note->type == GRE_NOTE)
3965 switch(last_note->u.note.shape) {
3966 case S_FLAT:
3967 case S_FLAT_PAREN:
3968 case S_FLAT_SOFT:
3969 case S_SHARP:
3970 case S_SHARP_PAREN:
3971 case S_SHARP_SOFT:
3972 case S_NATURAL:
3973 case S_NATURAL_PAREN:
3974 case S_NATURAL_SOFT:
3975 break;
3976 default:
3977 last_pitch = last_note->u.note.pitch;
3978 break;
3979 }
3980 }
3981 if (last_pitch != NO_PITCH && next_element->u.first_glyph
3982 && next_element->u.first_glyph->type == GRE_GLYPH) {
3983 next_note = next_element->u.first_glyph->u.notes.first_note;
3984 if (next_note->type == GRE_NOTE) {
3985 switch(next_note->u.note.shape) {
3986 case S_FLAT:
3987 case S_FLAT_PAREN:
3988 case S_FLAT_SOFT:
3989 case S_SHARP:
3990 case S_SHARP_PAREN:
3991 case S_SHARP_SOFT:
3992 case S_NATURAL:
3993 case S_NATURAL_PAREN:
3994 case S_NATURAL_SOFT:
3995 break;
3996 default:
3997 if (next_note->u.note.pitch != NO_PITCH &&
3998 next_note->u.note.pitch - last_pitch == 0) {
3999 /* at a unison */
4000 break_flag = 2;
4001 }
4002 break;
4003 }
4004 }
4005 }
4006
4007 fprintf(f, "\\GreEndOfElement{0}{%d}{%u}%%\n", break_flag,
4008 note_unit_count);
4009 }
4010 }
4011 }
4012
4013 /*
4014 * Arguments are relatively obvious. The most obscure is certainly first_of_disc
4015 * which is 0 all the time, except in the case of a "clef change syllable". In
4016 * this case we make a \GreDiscretionary with two arguments:
4017 * 1.what should be printed if the syllable is the last of its line (which
4018 * basically means everything but clefs and custos), and
4019 * 2. what should be printed if it's in a middle of a line (which means
4020 * everything)
4021 * So the first_of_disc argument is:
4022 * 0 if we don't know (general case)
4023 * 1 in case of the first argument of a \GreDiscretionary
4024 * 2 if we are in the second argument (necessary in order to avoid infinite loops)
4025 */
4026 static void write_syllable(FILE *f, gregorio_syllable *syllable,
4027 unsigned char first_of_disc, gregoriotex_status *const status,
4028 const gregorio_score *const score,
4029 const gregorio_element *const *const last_of_voice,
4030 void (*const write_this_syllable_text)
4031 (FILE *, const char *, const gregorio_syllable *, bool))
4032 {
4033 const gregorio_element *clef_change_element = NULL, *element;
4034 const char *syllable_type = NULL;
4035 bool anticipated_event_written = false;
4036 bool end_of_word;
4037 bool end_of_line;
4038 char euouae_follows;
4039 char eol_forces_custos;
4040 unsigned short next_euouae_id;
4041 unsigned int note_unit_count;
4042 gtex_alignment alignment = AT_ONE_NOTE;
4043 gtex_alteration alteration = ALT_NONE;
4044
4045 gregorio_not_null(syllable, write_syllable, return);
4046 end_of_word = syllable->position == WORD_END
4047 || syllable->position == WORD_ONE_SYLLABLE || !syllable->text
4048 || !syllable->next_syllable;
4049 /* Very first: before anything, if the syllable is the beginning of a
4050 * no-linebreak area: */
4051 if (syllable->no_linebreak_area == NLBA_BEGINNING) {
4052 fprintf(f, "\\GreBeginNLBArea{1}{0}%%\n");
4053 }
4054 if (syllable->euouae == EUOUAE_BEGINNING) {
4055 fprintf(f, "\\GreBeginEUOUAE{%hu}%%\n", syllable->euouae_id);
4056 }
4057 if (syllable->elements && *(syllable->elements)) {
4058 /*
4059 * first we check if the syllable is only a end of line. If it is the
4060 * case, we don't print anything but a comment (to be able to read it
4061 * if we read GregorioTeX). The end of lines are treated separately in
4062 * GregorioTeX, it is buit inside the TeX structure.
4063 */
4064 if ((syllable->elements)[0]->type == GRE_END_OF_LINE) {
4065 gregorio_assert(syllable != score->first_syllable, write_syllable,
4066 "line break is not supported on the first syllable",
4067 return);
4068 if ((syllable->elements)[0]->u.misc.unpitched.info.eol_ragged) {
4069 fprintf(f, "%%\n%%\n\\GreNewParLine %%\n%%\n%%\n");
4070 } else {
4071 fprintf(f, "%%\n%%\n\\GreNewLine %%\n%%\n%%\n");
4072 }
4073 write_this_syllable_text(f, NULL, syllable, end_of_word);
4074 return;
4075 }
4076 /*
4077 * This case is not simple: if the syllable contains a clef change,
4078 * whether it is (c4) or (::c4) or (z0::c4), we put it in a
4079 * discretionary. Warning: only these three cases will have the expected
4080 * effect. So first we detect it:
4081 */
4082 if (first_of_disc == 0) { /* to avoid infinite loops */
4083 clef_change_element = gregorio_get_clef_change(syllable);
4084 if (clef_change_element) {
4085 gregorio_assert(syllable != score->first_syllable,
4086 write_syllable,
4087 "clef change is not supported on the first syllable",
4088 return);
4089 /*
4090 * In this case, the first thing to do is to change the line clef
4091 */
4092 gregoriotex_print_change_line_clef(f, clef_change_element);
4093 fprintf(f, "\\GreDiscretionary{0}{%%\n");
4094 write_syllable(f, syllable, 1, status, score, last_of_voice,
4095 write_syllable_text);
4096 fprintf(f, "}{%%\n");
4097 write_syllable(f, syllable, 2, status, score, last_of_voice,
4098 write_syllable_text);
4099 fprintf(f, "}%%\n");
4100 write_this_syllable_text(f, NULL, syllable, end_of_word);
4101 return;
4102 }
4103 }
4104 write_fixed_text_styles(f, syllable->text,
4105 syllable->next_syllable? syllable->next_syllable->text : NULL);
4106 if ((syllable->elements)[0]->type == GRE_BAR) {
4107 if (syllable != score->first_syllable) {
4108 if (!syllable->next_syllable && !syllable->text
4109 && (syllable->elements)[0]->u.misc.unpitched.info.bar
4110 == B_DIVISIO_FINALIS) {
4111 handle_final_bar(f, "DivisioFinalis", syllable);
4112 write_this_syllable_text(f, NULL, syllable, end_of_word);
4113 return;
4114 }
4115 if (!syllable->next_syllable && !syllable->text
4116 && (syllable->elements)[0]->u.misc.unpitched.info.bar
4117 == B_DIVISIO_MAIOR) {
4118 handle_final_bar(f, "DivisioMaior", syllable);
4119 write_this_syllable_text(f, NULL, syllable, end_of_word);
4120 return;
4121 }
4122 }
4123 /* the special case of first syllable will be treated as a normal
4124 * bar syllable */
4125 syllable_type = "\\GreBarSyllable";
4126 } else {
4127 if (first_of_disc != 0) {
4128 syllable_type = "\\GreBarSyllable";
4129 } else {
4130 syllable_type = "\\GreSyllable";
4131 }
4132 }
4133 } else {
4134 write_fixed_text_styles(f, syllable->text,
4135 syllable->next_syllable? syllable->next_syllable->text : NULL);
4136 syllable_type = "\\GreNoNoteSyllable";
4137 }
4138 write_this_syllable_text(f, syllable_type, syllable, end_of_word);
4139 fprintf(f, "{}{\\Gre%s}", syllable->first_word ? "FirstWord" : "Unstyled");
4140 if (end_of_word) {
4141 fprintf(f, "{1}");
4142 } else {
4143 fprintf(f, "{0}");
4144 }
4145 end_of_line = is_last_of_line(syllable);
4146 anticipate_event(syllable, &euouae_follows, &eol_forces_custos,
4147 &next_euouae_id);
4148 if (syllable->next_syllable) {
4149 fprintf(f, "{%s\\GreSetNextSyllable",
4150 syllable->next_syllable->forced_center?
4151 "\\GreGABCNextForceCenters" : "");
4152 write_text(f, syllable->next_syllable->text);
4153 if (end_of_line || first_of_disc == 1) {
4154 fprintf(f, "\\GreLastOfLine");
4155 } else if (euouae_follows) {
4156 fprintf(f, "\\GreLastSyllableBeforeEUOUAE{%hu}{%c}",
4157 next_euouae_id, euouae_follows);
4158 }
4159 fprintf(f, "}{");
4160 write_syllable_point_and_click(f, syllable, status);
4161 syllable_first_type(syllable->next_syllable, &alignment, &alteration);
4162 fprintf(f, "}{{%d}{%d}}{", alignment, alteration);
4163 } else {
4164 fprintf(f, "{\\GreSetNextSyllable{}{}{}{}{}");
4165 if (end_of_line || first_of_disc == 1) {
4166 fprintf(f, "\\GreLastOfLine");
4167 }
4168 fprintf(f, "}{");
4169 write_syllable_point_and_click(f, syllable, status);
4170 fprintf(f, "}{{%d}{%d}}{", AT_EMPTY_SYLLABLE, ALT_NONE);
4171 }
4172 if (syllable->translation) {
4173 if (syllable->translation_type == TR_WITH_CENTER_BEGINNING) {
4174 fprintf(f, "%%\n\\GreWriteTranslationWithCenterBeginning{");
4175 } else {
4176 fprintf(f, "%%\n\\GreWriteTranslation{");
4177 }
4178 write_translation(f, syllable->translation);
4179 fprintf(f, "}%%\n");
4180 }
4181 if (syllable->translation_type) {
4182 if (syllable->translation_type == TR_WITH_CENTER_END)
4183 fprintf(f, "%%\n\\GreTranslationCenterEnd %%\n");
4184 }
4185 if (syllable->abovelinestext) {
4186 fprintf(f, "%%\n\\GreSetTextAboveLines{%s}%%\n",
4187 syllable->abovelinestext);
4188 }
4189 fprintf(f, "}{%%\n");
4190
4191 fprintf(f, "\\GreSyllableNoteCount{%u}%%\n", syllable->elements?
4192 count_note_units(*syllable->elements) : 0);
4193
4194 note_unit_count = 0;
4195 if (syllable->elements) {
4196 for (element = *syllable->elements; element; element = element->next) {
4197 if (element->nabc_lines && element->nabc) {
4198 signed char high_pitch = UNDETERMINED_HEIGHT;
4199 signed char low_pitch = UNDETERMINED_HEIGHT;
4200 size_t i;
4201 compute_element_height_extrema(element, &high_pitch,
4202 &low_pitch);
4203 fixup_height_extrema(&high_pitch, &low_pitch);
4204 for (i = 0; i < element->nabc_lines; i++) {
4205 if (element->nabc[i]) {
4206 fprintf(f, "\\GreNABCNeumes{%d}{", (int)(i+1));
4207 tex_escape_text(f, element->nabc[i]);
4208 fprintf(f, "}{%d}{%d}%%\n", pitch_value(high_pitch),
4209 pitch_value(low_pitch));
4210 }
4211 }
4212 }
4213 switch (element->type) {
4214 case GRE_SPACE:
4215 switch (element->u.misc.unpitched.info.space) {
4216 case SP_LARGER_SPACE:
4217 fprintf(f, "\\GreEndOfElement{1}{0}{%u}%%\n",
4218 note_unit_count);
4219 break;
4220 case SP_GLYPH_SPACE:
4221 fprintf(f, "\\GreEndOfElement{2}{0}{%u}%%\n",
4222 note_unit_count);
4223 break;
4224 case SP_NEUMATIC_CUT:
4225 fprintf(f, "\\GreEndOfElement{0}{0}{%u}%%\n",
4226 note_unit_count);
4227 break;
4228 case SP_AD_HOC_SPACE:
4229 fprintf(f, "\\GreAdHocSpaceEndOfElement{%s}{0}{%u}%%\n",
4230 element->u.misc.unpitched.info.ad_hoc_space_factor,
4231 note_unit_count);
4232 break;
4233 case SP_GLYPH_SPACE_NB:
4234 fprintf(f, "\\GreEndOfElement{2}{1}{%u}%%\n",
4235 note_unit_count);
4236 break;
4237 case SP_LARGER_SPACE_NB:
4238 fprintf(f, "\\GreEndOfElement{1}{1}{%u}%%\n",
4239 note_unit_count);
4240 break;
4241 case SP_NEUMATIC_CUT_NB:
4242 fprintf(f, "\\GreEndOfElement{0}{1}{%u}%%\n",
4243 note_unit_count);
4244 break;
4245 case SP_AD_HOC_SPACE_NB:
4246 fprintf(f, "\\GreAdHocSpaceEndOfElement{%s}{1}{%u}%%\n",
4247 element->u.misc.unpitched.info.ad_hoc_space_factor,
4248 note_unit_count);
4249 break;
4250 default:
4251 /* not reachable unless there's a programming error */
4252 /* LCOV_EXCL_START */
4253 gregorio_fail(write_syllable,
4254 "encountered an unexpected element-level space");
4255 break;
4256 /* LCOV_EXCL_STOP */
4257 }
4258 break;
4259
4260 case GRE_TEXVERB_ELEMENT:
4261 if (element->texverb) {
4262 fprintf(f, "%% verbatim text at element level:\n%s%%\n"
4263 "%% end of verbatim text\n",
4264 gregorio_texverb(element->texverb));
4265 }
4266 break;
4267
4268 case GRE_NLBA:
4269 if (element->u.misc.unpitched.info.nlba == NLBA_BEGINNING) {
4270 fprintf(f, "\\GreBeginNLBArea{0}{0}%%\n");
4271 } else {
4272 fprintf(f, "\\GreEndNLBArea{%d}{0}%%\n",
4273 next_is_bar(syllable, element)? 3 : 0);
4274 }
4275 break;
4276
4277 case GRE_ALT:
4278 if (element->texverb) {
4279 fprintf(f, "\\GreSetTextAboveLines{%s}%%\n",
4280 gregorio_texverb(element->texverb));
4281 }
4282 break;
4283
4284 case GRE_CLEF:
4285 /* We don't print clef changes at the end of a line */
4286 if (first_of_disc != 1) {
4287 if (is_before_linebreak(syllable, element)) {
4288 signed char next_note_pitch;
4289 gregorio_shape next_note_alteration;
4290
4291 next_note_pitch = gregorio_adjust_pitch_into_staff(
4292 score, gregorio_determine_next_pitch(
4293 syllable, element, NULL, &next_note_alteration)
4294 - element->u.misc.clef.pitch_difference);
4295
4296 fputs(next_custos(next_note_pitch, next_note_alteration,
4297 status), f);
4298 gregoriotex_print_change_line_clef(f, element);
4299 } else {
4300 /* the third argument is 0 or 1 according to the need
4301 * for a space before the clef */
4302 fprintf(f, "\\GreChangeClef{%c}{%d}{%c}{%d}{%c}{%d}{%d}%%\n",
4303 gregorio_clef_to_char(element->u.misc.clef.clef),
4304 element->u.misc.clef.line,
4305 (!element->previous || element->previous->type
4306 == GRE_BAR)? '0' : '1',
4307 clef_flat_height(element->u.misc.clef.clef,
4308 element->u.misc.clef.line,
4309 element->u.misc.clef.flatted),
4310 gregorio_clef_to_char(
4311 element->u.misc.clef.secondary_clef),
4312 element->u.misc.clef.secondary_line,
4313 clef_flat_height(
4314 element->u.misc.clef.secondary_clef,
4315 element->u.misc.clef.secondary_line,
4316 element->u.misc.clef.secondary_flatted));
4317 }
4318 }
4319 break;
4320
4321 case GRE_CUSTOS:
4322 if (first_of_disc != 1) {
4323 signed char next_note_pitch;
4324 gregorio_shape next_note_alteration;
4325 const char *alteration = "";
4326 /* We don't print custos before a bar at the end of a line.
4327 * We also print an unbreakable larger space before the
4328 * custos */
4329 handle_last_of_voice(f, syllable, element, *last_of_voice);
4330 next_note_pitch = gregorio_determine_next_pitch(syllable,
4331 element, NULL, &next_note_alteration);
4332 if (!element->u.misc.pitched.force_pitch) {
4333 alteration = alteration_name(next_note_alteration);
4334 }
4335 fprintf(f, "\\GreCustos{%d}{%s}%s%%\n",
4336 pitch_value(element->u.misc.pitched.pitch), alteration,
4337 next_custos(next_note_pitch, next_note_alteration,
4338 status));
4339 ++note_unit_count;
4340 }
4341 break;
4342
4343 case GRE_SUPPRESS_CUSTOS:
4344 handle_last_of_voice(f, syllable, element, *last_of_voice);
4345 fprintf(f, "\\GreSuppressEolCustos %%\n");
4346 status->suppressed_custos = true;
4347 break;
4348
4349 case GRE_BAR:
4350 handle_last_of_voice(f, syllable, element, *last_of_voice);
4351 write_bar(f, score, syllable, element, first_of_disc);
4352 break;
4353
4354 case GRE_END_OF_LINE:
4355 if (!element->next) {
4356 write_anticipated_event(f, euouae_follows,
4357 eol_forces_custos, next_euouae_id);
4358 anticipated_event_written = true;
4359 }
4360 /* here we suppose we don't have two linebreaks in the same
4361 * syllable */
4362 if (element->u.misc.unpitched.info.eol_ragged) {
4363 fprintf(f, "%%\n%%\n\\GreNewParLine %%\n%%\n%%\n");
4364 } else {
4365 fprintf(f, "%%\n%%\n\\GreNewLine %%\n%%\n%%\n");
4366 }
4367 break;
4368
4369 default:
4370 /* here element->type is GRE_ELEMENT */
4371 assert(element->type == GRE_ELEMENT);
4372 handle_last_of_voice(f, syllable, element, *last_of_voice);
4373 note_unit_count += write_element(f, syllable, element, status,
4374 score);
4375 write_default_end_of_element(f, element, note_unit_count);
4376 break;
4377 }
4378 }
4379 }
4380 if (!anticipated_event_written) {
4381 write_anticipated_event(f, euouae_follows, eol_forces_custos,
4382 next_euouae_id);
4383 }
4384 fprintf(f, "}%%\n");
4385 if (syllable->position == WORD_END
4386 || syllable->position == WORD_ONE_SYLLABLE || !syllable->text) {
4387 fprintf(f, "%%\n");
4388 }
4389 finish_syllable(f, syllable);
4390 }
4391
4392 static char *digest_to_hex(const unsigned char digest[SHA1_DIGEST_SIZE])
4393 {
4394 static const char *const hex = "0123456789abcdef";
4395 static char result[41];
4396
4397 char *p = result;
4398 unsigned char byte;
4399
4400 int i;
4401 for (i = 0; i < SHA1_DIGEST_SIZE; ++i) {
4402 byte = digest[i];
4403
4404 *(p++) = hex[(byte >> 4) & 0x0FU];
4405 *(p++) = hex[byte & 0x0FU];
4406 }
4407
4408 *p = '\0';
4409
4410 return result;
4411 }
4412
4413 static void initialize_score(gregoriotex_status *const status,
4414 gregorio_score *score, const bool point_and_click,
4415 const gregorio_element **const last_of_voice)
4416 {
4417 gregorio_syllable *syllable;
4418
4419 status->bottom_line = false;
4420 status->top_height = status->bottom_height = UNDETERMINED_HEIGHT;
4421 status->abovelinestext = status->translation = false;
4422 status->suppressed_custos = false;
4423
4424 /* first pass to compute positioning */
4425 for (syllable = score->first_syllable; syllable;
4426 syllable = syllable->next_syllable) {
4427 int voice;
4428
4429 for (voice = 0; voice < score->number_of_voices; ++voice) {
4430 gregoriotex_compute_positioning(syllable->elements[voice], score);
4431 }
4432 }
4433
4434 gregoriotex_compute_cross_syllable_positioning(score);
4435
4436 for (syllable = score->first_syllable; syllable;
4437 syllable = syllable->next_syllable) {
4438 int voice;
4439
4440 if (syllable->translation) {
4441 status->translation = true;
4442 }
4443
4444 if (syllable->abovelinestext) {
4445 status->abovelinestext = true;
4446 }
4447
4448 /* simultaneously compute height extrema and determine the last "real"
4449 * element in each voice */
4450 for (voice = 0; voice < score->number_of_voices; ++voice) {
4451 gregorio_element *element;
4452
4453 for (element = syllable->elements[voice]; element;
4454 element = element->next) {
4455 gregorio_glyph *glyph;
4456
4457 switch (element->type) {
4458 case GRE_ALT:
4459 status->abovelinestext = true;
4460 break;
4461
4462 case GRE_CUSTOS:
4463 last_of_voice[voice] = element;
4464 break;
4465
4466 case GRE_ELEMENT:
4467 for (glyph = element->u.first_glyph; glyph;
4468 glyph = glyph->next) {
4469 if (glyph->type == GRE_GLYPH) {
4470 last_of_voice[voice] = element;
4471 compute_height_extrema(glyph,
4472 glyph->u.notes.first_note,
4473 &(status->top_height),
4474 &(status->bottom_height));
4475 }
4476 }
4477 break;
4478
4479 case GRE_BAR:
4480 last_of_voice[voice] = element;
4481 break;
4482
4483 default:
4484 /* to eliminate the warning */
4485 break;
4486 }
4487 }
4488 }
4489 }
4490
4491 fixup_height_extrema(&(status->top_height), &(status->bottom_height));
4492
4493 status->point_and_click = point_and_click;
4494 }
4495
4496 static void write_header(FILE *const f, const char *const name,
4497 const char *const value)
4498 {
4499 if (value) {
4500 fprintf(f, "\\GreHeader{");
4501 tex_escape_text(f, name);
4502 fprintf(f, "}{");
4503 tex_escape_text(f, value);
4504 fprintf(f, "}%%\n");
4505 }
4506 }
4507
4508 static void write_headers(FILE *const f, gregorio_score *const score)
4509 {
4510 gregorio_header *header;
4511
4512 fprintf(f, "\\GreBeginHeaders %%\n");
4513 for (header = score->headers; header; header = header->next) {
4514 write_header(f, header->name, header->value);
4515 }
4516 fprintf(f, "\\GreEndHeaders %%\n");
4517 }
4518
4519 static void suppress_expansion(FILE *const f, const char *text)
4520 {
4521 if (!text) {
4522 return;
4523 }
4524
4525 for (; *text; ++text) {
4526 if (*text == '\\') {
4527 fprintf(f, "\\noexpand");
4528 }
4529 fputc(*text, f);
4530 }
4531 }
4532
4533 static int first_note_near_clef(const gregorio_score *const score) {
4534 gregorio_clef_info clef = gregorio_default_clef;
4535 if (score->first_voice_info) {
4536 clef = score->first_voice_info->initial_clef;
4537 if (!clef.secondary_line && !clef.flatted && score->first_syllable
4538 && score->first_syllable->elements) {
4539 const gregorio_element *element = score->first_syllable->elements[0];
4540 if (element && element->type == GRE_ELEMENT) {
4541 const gregorio_glyph *glyph = element->u.first_glyph;
4542 if (glyph && glyph->type == GRE_GLYPH) {
4543 const signed char clef_pitch = LOW_LINE_PITCH
4544 + ((clef.line - 1) * 2);
4545 const gregorio_note *low_note = glyph->u.notes.first_note;
4546 const gregorio_note *high_note = low_note;
4547 switch (glyph->u.notes.glyph_type) {
4548 case G_PODATUS:
4549 /* next note is above the previous */
4550 if (low_note->next) {
4551 high_note = low_note->next;
4552 }
4553 break;
4554 case G_FLEXA:
4555 case G_PORRECTUS:
4556 case G_PORRECTUS_FLEXUS:
4557 /* there is a stem the size of the ambitus */
4558 if (high_note->next) {
4559 low_note = high_note->next;
4560 }
4561 break;
4562 default:
4563 /* to prevent the enum warning */
4564 break;
4565 }
4566 if (high_note->u.note.pitch < clef_pitch - 3
4567 || low_note->u.note.pitch > clef_pitch + 3) {
4568 return 0;
4569 }
4570 }
4571 }
4572 }
4573 }
4574 return 1;
4575 }
4576
4577 static int clef_size(const gregorio_clef_info *const clef)
4578 {
4579 /* this is a heuristic clef size approximation:
4580 * C-clef = 1
4581 * F-clef = 2
4582 * flatted = +2
4583 * unstacked double-clef = +1 (plus the clef size)
4584 */
4585 /* purposely making line signed */
4586 int line_diff = clef->line - clef->secondary_line;
4587 int size;
4588 if (clef->clef == CLEF_F) {
4589 size = 2;
4590 } else {
4591 size = 1;
4592 }
4593 if (clef->flatted) {
4594 size += 2;
4595 }
4596 if (clef->secondary_line) {
4597 if (line_diff < -1 || line_diff > 1) { /* stacked */
4598 int secondary_size;
4599 if (clef->secondary_clef == CLEF_F) {
4600 secondary_size = 2;
4601 } else {
4602 secondary_size = 1;
4603 }
4604 if (clef->secondary_flatted) {
4605 secondary_size += 2;
4606 }
4607 if (secondary_size > size) {
4608 size = secondary_size;
4609 }
4610 } else { /* unstacked */
4611 size += 1;
4612 if (clef->secondary_clef == CLEF_F) {
4613 size += 2;
4614 } else {
4615 size += 1;
4616 }
4617 if (clef->secondary_flatted) {
4618 size += 2;
4619 }
4620 }
4621 }
4622 return size;
4623 }
4624
4625 static void write_largest_clef(FILE *const f, gregorio_score *const score)
4626 {
4627 const gregorio_syllable *syllable;
4628 const gregorio_element *element;
4629 gregorio_clef_info clef = gregorio_default_clef;
4630 int size;
4631
4632 if (score->first_voice_info) {
4633 clef = score->first_voice_info->initial_clef;
4634 }
4635 size = clef_size(&clef);
4636
4637 for (syllable = score->first_syllable; syllable;
4638 syllable = syllable->next_syllable) {
4639 if (syllable->elements) {
4640 for (element = *syllable->elements; element;
4641 element = element->next) {
4642 if (element->type == GRE_CLEF) {
4643 const int this_size = clef_size(&element->u.misc.clef);
4644 if (this_size > size) {
4645 clef = element->u.misc.clef;
4646 size = this_size;
4647 }
4648 }
4649 }
4650 }
4651 }
4652
4653 fprintf(f, "\\GreSetLargestClef{%c}{%d}{%d}{%c}{%d}{%d}%%\n",
4654 gregorio_clef_to_char(clef.clef), clef.line,
4655 clef_flat_height(clef.clef, clef.line, clef.flatted),
4656 gregorio_clef_to_char(clef.secondary_clef), clef.secondary_line,
4657 clef_flat_height(clef.secondary_clef, clef.secondary_line,
4658 clef.secondary_flatted));
4659 }
4660
4661 void gregoriotex_write_score(FILE *const f, gregorio_score *const score,
4662 const char *const point_and_click_filename)
4663 {
4664 gregorio_clef_info clef = gregorio_default_clef;
4665 gregorio_syllable *current_syllable;
4666 int annotation_num;
4667 gregoriotex_status status;
4668 const gregorio_element *last_of_voice[MAX_NUMBER_OF_VOICES];
4669
4670 memset(last_of_voice, 0, sizeof last_of_voice);
4671 initialize_score(&status, score, point_and_click_filename != NULL,
4672 last_of_voice);
4673
4674 gregorio_assert(f, gregoriotex_write_score, "call with NULL file", return);
4675
4676 gregorio_assert_only(score->number_of_voices == 1, gregoriotex_write_score,
4677 "gregoriotex only works in monophony (for the moment)");
4678
4679 fprintf(f, "%% File generated by gregorio %s\n", GREGORIO_VERSION);
4680 fprintf(f, "\\GregorioTeXAPIVersion{%s}%%\n", VERSION);
4681
4682 if (score->name) {
4683 fprintf(f, "%% Name: %s\n", score->name);
4684 }
4685 if (score->author) {
4686 fprintf(f, "%% Author: %s\n", score->author);
4687 }
4688 if (score->gabc_copyright) {
4689 fprintf(f, "%% The copyright of this gabc is: %s\n",
4690 score->gabc_copyright);
4691 }
4692 if (score->score_copyright) {
4693 fprintf(f, "%% The copyright of the score is: %s\n",
4694 score->score_copyright);
4695 }
4696
4697 write_headers(f, score);
4698
4699 if (score->first_voice_info) {
4700 clef = score->first_voice_info->initial_clef;
4701 }
4702 fprintf(f, "\\GreBeginScore{%s}{%d}{%d}{%d}{%d}{%s}{%u}"
4703 "{\\GreInitialClefPosition{%d}{%d}}%%\n",
4704 digest_to_hex(score->digest), status.top_height,
4705 status.bottom_height, bool_to_int(status.translation),
4706 bool_to_int(status.abovelinestext),
4707 point_and_click_filename? point_and_click_filename : "",
4708 score->staff_lines, clef.line, clef.secondary_line);
4709 if (score->nabc_lines) {
4710 fprintf(f, "\\GreScoreNABCLines{%d}", (int)score->nabc_lines);
4711 }
4712 if (score->annotation[0]) {
4713 fprintf(f, "\\GreAnnotationLines");
4714 for (annotation_num = 0; annotation_num < MAX_ANNOTATIONS;
4715 ++annotation_num) {
4716 if (score->annotation[annotation_num]) {
4717 fprintf(f, "{%s}",
4718 score->annotation[annotation_num]);
4719 }
4720 else {
4721 fprintf(f, "{}");
4722 }
4723 }
4724 fprintf(f, "%%\n");
4725 }
4726 if (score->mode) {
4727 fprintf(f, "\\GreMode{");
4728 if (*(score->mode) >= '1' && *(score->mode) <= '8') {
4729 fprintf(f, "\\GreModeNumber{%c}%s", *(score->mode), score->mode + 1);
4730 } else {
4731 fprintf(f, "%s", score->mode);
4732 }
4733 fprintf(f, "}{");
4734 suppress_expansion(f, score->mode_modifier);
4735 fprintf(f, "}{");
4736 suppress_expansion(f, score->mode_differentia);
4737 fprintf(f, "}%%\n");
4738 }
4739
4740 write_largest_clef(f, score);
4741 fprintf(f, "\\GreScoreOpening{%%\n"); /* GreScoreOpening#1 */
4742 if (score->first_voice_info) {
4743 gregoriotex_write_voice_info(f, score->first_voice_info);
4744 }
4745 fprintf(f, "}{%%\n"); /* GreScoreOpening#2 */
4746 fprintf(f, "\\GreSetInitialClef{%c}{%d}{%d}{%c}{%d}{%d}{%d}%%\n",
4747 gregorio_clef_to_char(clef.clef), clef.line,
4748 clef_flat_height(clef.clef, clef.line, clef.flatted),
4749 gregorio_clef_to_char(clef.secondary_clef), clef.secondary_line,
4750 clef_flat_height(clef.secondary_clef, clef.secondary_line,
4751 clef.secondary_flatted),
4752 first_note_near_clef(score));
4753 fprintf(f, "}{%%\n"); /* GreScoreOpening#3 */
4754 current_syllable = score->first_syllable;
4755 if (current_syllable) {
4756 write_syllable(f, current_syllable, 0, &status, score, last_of_voice,
4757 write_first_syllable_text);
4758 current_syllable = current_syllable->next_syllable;
4759 } else {
4760 /* edge case: a score with no syllables */
4761 fprintf(f, "}{}{\\GreSetNoFirstSyllableText}%%\n");
4762 }
4763 while (current_syllable) {
4764 write_syllable(f, current_syllable, 0, &status, score, last_of_voice,
4765 write_syllable_text);
4766 current_syllable = current_syllable->next_syllable;
4767 }
4768 fprintf(f, "\\GreEndScore %%\n\\endinput %%\n");
4769 }