Debian Codegraph @

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

raw
1 /*
2 * Gregorio is a program that translates gabc files to GregorioTeX
3 * This file contains the logic for positioning signs on neumes.
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 #include "config.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include "bool.h"
26 #include "struct.h"
27
28 #include "gregoriotex.h"
29
30 /* (loose) naming convention, employing camel case to be TeX-csname-compliant:
31 * {specific-glyph-shape}{note-position}{note-shape}{first-ambitus}{second-ambitus}
32 */
33 OFFSET_CASE(FinalPunctum);
34 OFFSET_CASE(FinalDeminutus);
35 OFFSET_CASE(PenultBeforePunctumWide);
36 OFFSET_CASE(PenultBeforeDeminutus);
37 OFFSET_CASE(AntepenultBeforePunctum);
38 OFFSET_CASE(AntepenultBeforeDeminutus);
39 OFFSET_CASE(InitialPunctum);
40 OFFSET_CASE(InitioDebilis);
41 OFFSET_CASE(PorrNonAuctusInitialWide);
42 OFFSET_CASE(PorrNonAuctusInitialOne);
43 OFFSET_CASE(PorrAuctusInitialAny);
44 OFFSET_CASE(FinalInclinatum);
45 OFFSET_CASE(FinalInclinatumDeminutus);
46 OFFSET_CASE(FinalStropha);
47 OFFSET_CASE(FinalQuilisma);
48 OFFSET_CASE(FinalOriscus);
49 OFFSET_CASE(PenultBeforePunctumOne);
50 OFFSET_CASE(FinalUpperPunctum);
51 OFFSET_CASE(InitialOriscus);
52 OFFSET_CASE(InitialQuilisma);
53 OFFSET_CASE(TorcResNonAuctusSecondWideWide);
54 OFFSET_CASE(TorcResNonAuctusSecondOneWide);
55 OFFSET_CASE(TorcResDebilisNonAuctusSecondAnyWide);
56 OFFSET_CASE(FinalLineaPunctum);
57 OFFSET_CASE(TorcResQuilismaNonAuctusSecondWideWide);
58 OFFSET_CASE(TorcResOriscusNonAuctusSecondWideWide);
59 OFFSET_CASE(TorcResQuilismaNonAuctusSecondOneWide);
60 OFFSET_CASE(TorcResOriscusNonAuctusSecondOneWide);
61 OFFSET_CASE(TorcResNonAuctusSecondWideOne);
62 OFFSET_CASE(TorcResDebilisNonAuctusSecondAnyOne);
63 OFFSET_CASE(TorcResQuilismaNonAuctusSecondWideOne);
64 OFFSET_CASE(TorcResOriscusNonAuctusSecondWideOne);
65 OFFSET_CASE(TorcResNonAuctusSecondOneOne);
66 OFFSET_CASE(TorcResQuilismaNonAuctusSecondOneOne);
67 OFFSET_CASE(TorcResOriscusNonAuctusSecondOneOne);
68 OFFSET_CASE(TorcResAuctusSecondWideAny);
69 OFFSET_CASE(TorcResDebilisAuctusSecondAnyAny);
70 OFFSET_CASE(TorcResQuilismaAuctusSecondWideAny);
71 OFFSET_CASE(TorcResOriscusAuctusSecondWideAny);
72 OFFSET_CASE(TorcResAuctusSecondOneAny);
73 OFFSET_CASE(TorcResQuilismaAuctusSecondOneAny);
74 OFFSET_CASE(TorcResOriscusAuctusSecondOneAny);
75 OFFSET_CASE(ConnectedPenultBeforePunctumWide);
76 OFFSET_CASE(ConnectedPenultBeforePunctumOne);
77 OFFSET_CASE(InitialConnectedPunctum);
78 OFFSET_CASE(InitialConnectedVirga);
79 OFFSET_CASE(InitialConnectedQuilisma);
80 OFFSET_CASE(InitialConnectedOriscus);
81 OFFSET_CASE(FinalConnectedPunctum);
82 OFFSET_CASE(FinalConnectedAuctus);
83 OFFSET_CASE(FinalVirgaAuctus);
84 OFFSET_CASE(FinalConnectedVirga);
85 OFFSET_CASE(InitialVirga);
86 OFFSET_CASE(SalicusOriscusWide);
87 OFFSET_CASE(SalicusOriscusOne);
88 OFFSET_CASE(LeadingPunctum);
89 OFFSET_CASE(LeadingQuilisma);
90 OFFSET_CASE(LeadingOriscus);
91 OFFSET_CASE(Flat);
92 OFFSET_CASE(FlatParen);
93 OFFSET_CASE(FlatSoft);
94 OFFSET_CASE(Sharp);
95 OFFSET_CASE(SharpParen);
96 OFFSET_CASE(SharpSoft);
97 OFFSET_CASE(Natural);
98 OFFSET_CASE(NaturalParen);
99 OFFSET_CASE(NaturalSoft);
100
101 static __inline const char *note_before_last_note_case_ignoring_deminutus(
102 const gregorio_note *const current_note)
103 {
104 assert(current_note->next);
105 if ((current_note->u.note.pitch - current_note->next->u.note.pitch) == 1
106 || (current_note->u.note.pitch -
107 current_note->next->u.note.pitch) == -1) {
108 if (!current_note->previous || current_note->u.note.pitch -
109 current_note->previous->u.note.pitch > 1) {
110 return ConnectedPenultBeforePunctumOne;
111 } else {
112 return PenultBeforePunctumOne;
113 }
114 } else {
115 if (!current_note->previous || current_note->u.note.pitch -
116 current_note->previous->u.note.pitch > 1) {
117 return ConnectedPenultBeforePunctumWide;
118 } else {
119 return PenultBeforePunctumWide;
120 }
121 }
122 }
123
124 static __inline const char *note_before_last_note_case(
125 const gregorio_glyph *const current_glyph,
126 const gregorio_note *const current_note)
127 {
128 if ((current_glyph->u.notes.liquescentia & L_DEMINUTUS)
129 && current_note->next) {
130 return PenultBeforeDeminutus;
131 } else {
132 return note_before_last_note_case_ignoring_deminutus(current_note);
133 }
134 }
135
136 /* num can be FinalPunctum or FinalUpperPunctum according if the last note is a
137 * standard punctum or a smaller punctum (for pes, porrectus and torculus
138 * resupinus */
139 static __inline const char *last_note_case(
140 const gregorio_glyph *const current_glyph, const char* offset_pos,
141 gregorio_note *current_note, bool no_ambitus_one)
142 {
143 if (current_glyph->u.notes.liquescentia & L_DEMINUTUS) {
144 /*
145 * may seem strange, but it is unlogical to typeset a small horizontal
146 * episema at the end of a flexus deminutus
147 */
148 return FinalDeminutus;
149 }
150 if (!current_note->previous
151 || (!no_ambitus_one && (offset_pos == FinalUpperPunctum
152 || current_note->previous->u.note.pitch -
153 current_note->u.note.pitch == 1 ||
154 current_note->u.note.pitch - current_note->previous->u.note.pitch
155 == 1))) {
156 return offset_pos;
157 }
158 if (current_note->previous->u.note.pitch < current_note->u.note.pitch) {
159 if (current_glyph->u.notes.liquescentia &
160 (L_AUCTUS_ASCENDENS | L_AUCTUS_DESCENDENS)) {
161 return FinalConnectedAuctus;
162 } else {
163 return FinalConnectedVirga;
164 }
165 } else {
166 return FinalConnectedPunctum;
167 }
168 }
169
170 static __inline const char *first_note_case(
171 const gregorio_note *const current_note,
172 const gregorio_glyph *const current_glyph)
173 {
174 bool ambitus_one = true;
175 if (current_glyph->u.notes.liquescentia >= L_INITIO_DEBILIS) {
176 return InitioDebilis;
177 } else {
178 if (current_note->next && current_note->next->u.note.pitch -
179 current_note->u.note.pitch != 1 &&
180 current_note->u.note.pitch - current_note->next->u.note.pitch
181 != 1) {
182 ambitus_one = false;
183 }
184 switch (current_note->u.note.shape) {
185 case S_ORISCUS_ASCENDENS:
186 case S_ORISCUS_DESCENDENS:
187 return ambitus_one ? InitialOriscus : InitialConnectedOriscus;
188
189 case S_QUILISMA:
190 return ambitus_one ? InitialQuilisma : InitialConnectedQuilisma;
191
192 default:
193 if (ambitus_one) {
194 return InitialPunctum;
195 }
196 if (current_note->u.note.pitch > current_note->next->u.note.pitch) {
197 return InitialConnectedVirga;
198 } else {
199 return InitialConnectedPunctum;
200 }
201 }
202 }
203 }
204
205 static __inline const char *fused_single_note_case(
206 const gregorio_glyph *const glyph, const char *const unfused_case,
207 const char *const fused_case) {
208 int fuse_to_next_glyph = glyph->u.notes.fuse_to_next_glyph;
209 if (fuse_to_next_glyph < -1 || fuse_to_next_glyph > 1) {
210 return fused_case;
211 }
212 return unfused_case;
213 }
214
215 static __inline gregorio_vposition below_if_auctus(
216 const gregorio_glyph *const glyph)
217 {
218 if (glyph->u.notes.liquescentia &
219 (L_AUCTUS_ASCENDENS | L_AUCTUS_DESCENDENS)) {
220 return VPOS_BELOW;
221 }
222 return VPOS_ABOVE;
223 }
224
225 static __inline gregorio_vposition above_if_h_episema(
226 const gregorio_note *const note)
227 {
228 if (note && note->h_episema_above) {
229 return VPOS_ABOVE;
230 }
231 return VPOS_BELOW;
232 }
233
234 static __inline gregorio_vposition above_if_fused_next_h_episema(
235 const gregorio_glyph *glyph)
236 {
237 const gregorio_glyph *next = gregorio_next_non_texverb_glyph(glyph);
238 gregorio_assert(next && next->type == GRE_GLYPH
239 && is_fused(next->u.notes.liquescentia),
240 above_if_fused_next_h_episema,
241 "expected this glyph to be fused to the next one",
242 return VPOS_BELOW);
243 return above_if_h_episema(next->u.notes.first_note);
244 }
245
246 static __inline gregorio_vposition above_if_either_h_episema(
247 const gregorio_note *const note)
248 {
249 if ((note->previous && note->previous->h_episema_above)
250 || (note->next && note->next->h_episema_above)) {
251 return VPOS_ABOVE;
252 }
253 return VPOS_BELOW;
254 }
255
256 static __inline gregorio_vposition below_if_next_ambitus_allows(
257 const gregorio_note *const note)
258 {
259 assert(note->next);
260
261 if (!note->next || note->u.note.pitch - note->next->u.note.pitch > 2) {
262 return VPOS_BELOW;
263 }
264 return VPOS_ABOVE;
265 }
266
267 static __inline void low_high_set_lower(const gregorio_glyph *const glyph,
268 gregorio_note *const note)
269 {
270 if ((glyph->u.notes.liquescentia & L_DEMINUTUS) ||
271 !(glyph->u.notes.liquescentia
272 & (L_AUCTUS_ASCENDENS | L_AUCTUS_DESCENDENS))) {
273 note->is_lower_note = true;
274 }
275 }
276
277 static __inline void low_high_set_upper(const gregorio_glyph *const glyph,
278 gregorio_note *const note)
279 {
280 if ((glyph->u.notes.liquescentia & L_DEMINUTUS) ||
281 !(glyph->u.notes.liquescentia
282 & (L_AUCTUS_ASCENDENS | L_AUCTUS_DESCENDENS))) {
283 note->is_upper_note = true;
284 }
285 }
286
287 static __inline void high_low_set_upper(const gregorio_glyph *const glyph,
288 gregorio_note *const note)
289 {
290 if (glyph->u.notes.liquescentia & L_DEMINUTUS) {
291 note->is_upper_note = true;
292 }
293 }
294
295 static __inline void high_low_set_lower(const gregorio_glyph *const glyph,
296 gregorio_note *const note)
297 {
298 if (glyph->u.notes.liquescentia & L_DEMINUTUS) {
299 note->is_lower_note = true;
300 }
301 }
302
303 /* a function that finds the good sign (additional line, vepisema or
304 * hepisema) number, according to the gregoriotex convention (described in
305 * gregoriotex.tex)
306 * this function is REALLY a pain in the ass, but it is sadly necessary */
307 /*
308 *
309 * For the first note of a porrectus (flexus), this table summarizes the sign
310 * number (amb2 is the ambitus after the diagonal stroke):
311 *
312 * Porrectus | Porrectus | Porrectus | Porrectus
313 * non-auctus | non-auctus | auctus | Flexus
314 * amb2=1 | amb2>1 | |
315 * =================|=================|==============|==============
316 * PorrNonAuctus | PorrNonAuctus | PorrAuctus | PorrAuctus
317 * ~InitialOne | ~InitialWide | ~InitialAny | ~InitialAny
318 *
319 * For the second note of a torculus resupinus (flexus), this table summarizes
320 * the number (amb1 is the ambitus before the diagonal stroke and amb2 is the
321 * ambitus after the diagonal stroke):
322 *
323 * | | Torculus | Torculus | Torculus | Torculus
324 * | | Resupinus | Resupinus | Resupinus | Resupinus
325 * | Initial | non-auctus | non-auctus | auctus | Flexus
326 * amb1 | Note | amb2=1 | amb2>1 | |
327 * ======|============|=============|=============|============|============
328 * 1 | Punctum | TorcRes | TorcRes | TorcRes | TorcRes
329 * | | ~NonAuctus | ~NonAuctus | ~Auctus | ~Auctus
330 * | | ~Second | ~Second | ~Second | ~Second
331 * | | ~OneOne | ~OneWide | ~OneAny | ~OneAny
332 * ------+------------+-------------+-------------+------------+------------
333 * 1 | I. Debilis | TorcRes | TorcRes | TorcRes | TorcRes
334 * | | ~Debilis | ~Debilis | ~Debilis | ~Debilis
335 * | | ~NonAuctus | ~NonAuctus | ~Auctus | ~Auctus
336 * | | ~Second | ~Second | ~Second | ~Second
337 * | | ~AnyOne | ~AnyWide | ~AnyAny | ~AnyAny
338 * ------+------------+-------------+-------------+------------+------------
339 * 1 | Quilisma | TorcRes | TorcRes | TorcRes | TorcRes
340 * | | ~Quilisma | ~Quilisma | ~Quilisma | ~Quilisma
341 * | | ~NonAuctus | ~NonAuctus | ~Auctus | ~Auctus
342 * | | ~Second | ~Second | ~Second | ~Second
343 * | | ~OneOne | ~OneWide | ~OneAny | ~OneAny
344 * ------+------------+-------------+-------------+------------+------------
345 * 1 | Oriscus | TorcRes | TorcRes | TorcRes | TorcRes
346 * | | ~Oriscus | ~Oriscus | ~Oriscus | ~Oriscus
347 * | | ~NonAuctus | ~NonAuctus | ~Auctus | ~Auctus
348 * | | ~Second | ~Second | ~Second | ~Second
349 * | | ~OneOne | ~OneWide | ~OneAny | ~OneAny
350 * ======|============|=============|=============|============|============
351 * >1 | Punctum | TorcRes | TorcRes | TorcRes | TorcRes
352 * | | ~NonAuctus | ~NonAuctus | ~Auctus | ~Auctus
353 * | | ~Second | ~Second | ~Second | ~Second
354 * | | ~WideOne | ~WideWide | ~WideAny | ~WideAny
355 * ------+------------+-------------+-------------+------------+------------
356 * >1 | I. Debilis | TorcRes | TorcRes | TorcRes | TorcRes
357 * | | ~Debilis | ~Debilis | ~Debilis | ~Debilis
358 * | | ~NonAuctus | ~NonAuctus | ~Auctus | ~Auctus
359 * | | ~Second | ~Second | ~Second | ~Second
360 * | | ~AnyOne | ~AnyWide | ~AnyAny | ~AnyAny
361 * ------+------------+-------------+-------------+------------+------------
362 * >1 | Quilisma | TorcRes | TorcRes | TorcRes | TorcRes
363 * | | ~Quilisma | ~Quilisma | ~Quilisma | ~Quilisma
364 * | | ~NonAuctus | ~NonAuctus | ~Auctus | ~Auctus
365 * | | ~Second | ~Second | ~Second | ~Second
366 * | | ~WideOne | ~WideWide | ~WideAny | ~WideAny
367 * ------+------------+-------------+-------------+------------+------------
368 * >1 | Oriscus | TorcRes | TorcRes | TorcRes | TorcRes
369 * | | ~Oriscus | ~Oriscus | ~Oriscus | ~Oriscus
370 * | | ~NonAuctus | ~NonAuctus | ~Auctus | ~Auctus
371 * | | ~Second | ~Second | ~Second | ~Second
372 * | | ~WideOne | ~WideWide | ~WideAny | ~WideAny
373 *
374 */
375 static gregorio_vposition advise_positioning(const gregorio_glyph *const glyph,
376 gregorio_note *const note, int i, const gtex_type type)
377 {
378 gregorio_vposition h_episema = VPOS_AUTO, v_episema = VPOS_AUTO;
379 bool v_episema_below_is_lower = false, done;
380 const gregorio_glyph *previous;
381
382 /* no need to clear is_lower_note/is_upper_note because we used calloc */
383
384 switch (type) {
385 case T_PES:
386 case T_QUILISMA_PES:
387 /*
388 * in the case of a pes, we put the episema just under the bottom note
389 */
390 if (i == 1) {
391 if (glyph->u.notes.liquescentia & L_INITIO_DEBILIS) {
392 note->gtex_offset_case = InitioDebilis;
393 h_episema = above_if_h_episema(note->next);
394 } else {
395 note->is_lower_note = true;
396 if (note->u.note.shape == S_QUILISMA) {
397 note->gtex_offset_case = FinalQuilisma;
398 } else {
399 note->gtex_offset_case = FinalPunctum;
400 }
401 h_episema = VPOS_BELOW;
402 }
403 v_episema = VPOS_BELOW;
404 } else { /* i=2 */
405 if (!(glyph->u.notes.liquescentia & L_INITIO_DEBILIS)) {
406 note->is_upper_note = true;
407 }
408 if (glyph->u.notes.liquescentia & L_DEMINUTUS) {
409 note->gtex_offset_case = FinalDeminutus;
410 } else {
411 note->gtex_offset_case = FinalUpperPunctum;
412 }
413 h_episema = VPOS_ABOVE;
414 v_episema = below_if_auctus(glyph);
415 }
416 break;
417 case T_PES_QUADRATUM:
418 case T_PES_QUASSUS:
419 case T_QUILISMA_PES_QUADRATUM:
420 if (i == 1) {
421 note->gtex_offset_case = first_note_case(note, glyph);
422 h_episema = above_if_h_episema(note->next);
423 v_episema = VPOS_BELOW;
424 } else { /* i=2 */
425 if (glyph->u.notes.liquescentia & L_DEMINUTUS) {
426 note->gtex_offset_case = InitioDebilis;
427 } else {
428 note->gtex_offset_case = last_note_case(glyph, FinalPunctum,
429 note, true);
430 }
431 h_episema = VPOS_ABOVE;
432 v_episema = VPOS_BELOW;
433 }
434 break;
435 case T_FLEXUS:
436 case T_FLEXUS_ORISCUS:
437 if (i == 1) {
438 high_low_set_upper(glyph, note);
439 if (type == T_FLEXUS_ORISCUS) {
440 note->gtex_offset_case = InitialConnectedOriscus;
441 } else {
442 if (note->next && note->u.note.pitch -
443 note->next->u.note.pitch == 1) {
444 note->gtex_offset_case = InitialVirga;
445 } else {
446 note->gtex_offset_case = InitialConnectedVirga;
447 }
448 }
449 h_episema = VPOS_ABOVE;
450 if (glyph->u.notes.liquescentia & L_DEMINUTUS) {
451 v_episema = below_if_next_ambitus_allows(note);
452 } else {
453 v_episema = VPOS_BELOW;
454 }
455 } else { /* i=2 */
456 high_low_set_lower(glyph, note);
457 note->gtex_offset_case = last_note_case(glyph, FinalPunctum, note,
458 false);
459 if (glyph->u.notes.liquescentia & L_DEMINUTUS) {
460 h_episema = VPOS_BELOW;
461 } else {
462 h_episema = above_if_h_episema(note->previous);
463 }
464 v_episema = VPOS_BELOW;
465 }
466 break;
467 case T_TORCULUS_RESUPINUS_FLEXUS:
468 done = true;
469 switch (i) {
470 case 1:
471 note->gtex_offset_case = first_note_case(note, glyph);
472 h_episema = above_if_h_episema(note->next);
473 v_episema = VPOS_BELOW;
474 break;
475 case HEPISEMA_FIRST_TWO:
476 case 2:
477 if (glyph->u.notes.liquescentia & L_INITIO_DEBILIS) {
478 note->gtex_offset_case = TorcResDebilisAuctusSecondAnyAny;
479 } else {
480 if (note->u.note.pitch - note->previous->u.note.pitch == 1) {
481 switch (note->previous->u.note.shape) {
482 case S_QUILISMA:
483 note->gtex_offset_case =
484 TorcResQuilismaAuctusSecondOneAny;
485 break;
486 case S_ORISCUS_ASCENDENS:
487 case S_ORISCUS_DESCENDENS:
488 note->gtex_offset_case =
489 TorcResOriscusAuctusSecondOneAny;
490 break;
491 default:
492 note->gtex_offset_case = TorcResAuctusSecondOneAny;
493 break;
494 }
495 } else {
496 switch (note->previous->u.note.shape) {
497 case S_QUILISMA:
498 note->gtex_offset_case =
499 TorcResQuilismaAuctusSecondWideAny;
500 break;
501 case S_ORISCUS_ASCENDENS:
502 case S_ORISCUS_DESCENDENS:
503 note->gtex_offset_case =
504 TorcResOriscusAuctusSecondWideAny;
505 break;
506 default:
507 note->gtex_offset_case = TorcResAuctusSecondWideAny;
508 break;
509 }
510 }
511 }
512 h_episema = VPOS_ABOVE;
513 v_episema = VPOS_BELOW;
514 v_episema_below_is_lower = true;
515 break;
516 default:
517 --i;
518 done = false;
519 break;
520 }
521
522 if (done) {
523 break;
524 }
525 /* fall through */
526 case T_PORRECTUS_FLEXUS:
527 switch (i) {
528 case HEPISEMA_FIRST_TWO:
529 case 1:
530 note->gtex_offset_case = PorrAuctusInitialAny;
531 h_episema = VPOS_ABOVE;
532 v_episema = VPOS_BELOW;
533 v_episema_below_is_lower = true;
534 break;
535 case 2:
536 if (glyph->u.notes.liquescentia & L_DEMINUTUS) {
537 note->gtex_offset_case = AntepenultBeforeDeminutus;
538 } else {
539 note->gtex_offset_case = AntepenultBeforePunctum;
540 }
541 h_episema = above_if_either_h_episema(note);
542 v_episema = VPOS_BELOW;
543 break;
544 case 3:
545 high_low_set_upper(glyph, note);
546 h_episema = VPOS_ABOVE;
547 if ((glyph->u.notes.liquescentia & L_DEMINUTUS) && note->next) {
548 v_episema = below_if_next_ambitus_allows(note);
549 note->gtex_offset_case = PenultBeforeDeminutus;
550 } else {
551 v_episema = VPOS_BELOW;
552 note->gtex_offset_case = note_before_last_note_case(glyph,
553 note);
554 }
555 break;
556 default:
557 high_low_set_lower(glyph, note);
558 note->gtex_offset_case = last_note_case(glyph, FinalPunctum, note,
559 false);
560 h_episema = above_if_h_episema(note->previous);
561 v_episema = VPOS_BELOW;
562 break;
563 }
564 break;
565 case T_TORCULUS_RESUPINUS:
566 done = true;
567 switch (i) {
568 case 1:
569 note->gtex_offset_case = first_note_case(note, glyph);
570 h_episema = above_if_h_episema(note->next);
571 v_episema = VPOS_BELOW;
572 break;
573 case HEPISEMA_FIRST_TWO:
574 case 2:
575 if (glyph->u.notes.liquescentia &
576 (L_AUCTUS_ASCENDENS | L_AUCTUS_DESCENDENS)) {
577 /* auctus */
578 if (glyph->u.notes.liquescentia & L_INITIO_DEBILIS) {
579 note->gtex_offset_case = TorcResDebilisAuctusSecondAnyAny;
580 } else {
581 if (note->u.note.pitch -
582 note->previous->u.note.pitch == 1) {
583 switch (note->previous->u.note.shape) {
584 case S_QUILISMA:
585 note->gtex_offset_case =
586 TorcResQuilismaAuctusSecondOneAny;
587 break;
588 case S_ORISCUS_ASCENDENS:
589 case S_ORISCUS_DESCENDENS:
590 note->gtex_offset_case =
591 TorcResOriscusAuctusSecondOneAny;
592 break;
593 default:
594 note->gtex_offset_case = TorcResAuctusSecondOneAny;
595 break;
596 }
597 } else {
598 switch (note->previous->u.note.shape) {
599 case S_QUILISMA:
600 note->gtex_offset_case =
601 TorcResQuilismaAuctusSecondWideAny;
602 break;
603 case S_ORISCUS_ASCENDENS:
604 case S_ORISCUS_DESCENDENS:
605 note->gtex_offset_case =
606 TorcResOriscusAuctusSecondWideAny;
607 break;
608 default:
609 note->gtex_offset_case = TorcResAuctusSecondWideAny;
610 break;
611 }
612 }
613 }
614 } else if (note->next->next->u.note.pitch -
615 note->next->u.note.pitch == 1) {
616 /* non-auctus with a second ambitus of 1 */
617 if (glyph->u.notes.liquescentia & L_INITIO_DEBILIS) {
618 note->gtex_offset_case =
619 TorcResDebilisNonAuctusSecondAnyOne;
620 } else {
621 if (note->u.note.pitch -
622 note->previous->u.note.pitch == 1) {
623 switch (note->previous->u.note.shape) {
624 case S_QUILISMA:
625 note->gtex_offset_case =
626 TorcResQuilismaNonAuctusSecondOneOne;
627 break;
628 case S_ORISCUS_ASCENDENS:
629 case S_ORISCUS_DESCENDENS:
630 note->gtex_offset_case =
631 TorcResOriscusNonAuctusSecondOneOne;
632 break;
633 default:
634 note->gtex_offset_case =
635 TorcResNonAuctusSecondOneOne;
636 break;
637 }
638 } else {
639 switch (note->previous->u.note.shape) {
640 case S_QUILISMA:
641 note->gtex_offset_case =
642 TorcResQuilismaNonAuctusSecondWideOne;
643 break;
644 case S_ORISCUS_ASCENDENS:
645 case S_ORISCUS_DESCENDENS:
646 note->gtex_offset_case =
647 TorcResOriscusNonAuctusSecondWideOne;
648 break;
649 default:
650 note->gtex_offset_case =
651 TorcResNonAuctusSecondWideOne;
652 break;
653 }
654 }
655 }
656 } else {
657 /* non-auctus with a second ambitus of at least 2 */
658 if (glyph->u.notes.liquescentia & L_INITIO_DEBILIS) {
659 note->gtex_offset_case =
660 TorcResDebilisNonAuctusSecondAnyWide;
661 } else {
662 if (note->u.note.pitch -
663 note->previous->u.note.pitch == 1) {
664 switch (note->previous->u.note.shape) {
665 case S_QUILISMA:
666 note->gtex_offset_case =
667 TorcResQuilismaNonAuctusSecondOneWide;
668 break;
669 case S_ORISCUS_ASCENDENS:
670 case S_ORISCUS_DESCENDENS:
671 note->gtex_offset_case =
672 TorcResOriscusNonAuctusSecondOneWide;
673 break;
674 default:
675 note->gtex_offset_case =
676 TorcResNonAuctusSecondOneWide;
677 break;
678 }
679 } else {
680 switch (note->previous->u.note.shape) {
681 case S_QUILISMA:
682 note->gtex_offset_case =
683 TorcResQuilismaNonAuctusSecondWideWide;
684 break;
685 case S_ORISCUS_ASCENDENS:
686 case S_ORISCUS_DESCENDENS:
687 note->gtex_offset_case =
688 TorcResOriscusNonAuctusSecondWideWide;
689 break;
690 default:
691 note->gtex_offset_case =
692 TorcResNonAuctusSecondWideWide;
693 break;
694 }
695 }
696 }
697 }
698 h_episema = VPOS_ABOVE;
699 v_episema = VPOS_BELOW;
700 v_episema_below_is_lower = true;
701 break;
702 default:
703 --i;
704 done = false;
705 break;
706 }
707
708 if (done) {
709 break;
710 }
711 /* fall through */
712 case T_PORRECTUS: /* or porrectus-like flexus */
713 switch (i) {
714 case HEPISEMA_FIRST_TWO:
715 case 1:
716 if (glyph->u.notes.fuse_to_next_glyph ||
717 (glyph->u.notes.liquescentia
718 & (L_AUCTUS_ASCENDENS | L_AUCTUS_DESCENDENS))) {
719 /* auctus */
720 note->gtex_offset_case = PorrAuctusInitialAny;
721 } else if (note->next->next->u.note.pitch
722 - note->next->u.note.pitch == 1) {
723 /* non-auctus with a second ambitus of 1 */
724 note->gtex_offset_case = PorrNonAuctusInitialOne;
725 } else {
726 /* non-auctus with a second ambitus of at least 2 */
727 note->gtex_offset_case = PorrNonAuctusInitialWide;
728 }
729 h_episema = VPOS_ABOVE;
730 v_episema = VPOS_BELOW;
731 v_episema_below_is_lower = true;
732 break;
733 case 2:
734 low_high_set_lower(glyph, note);
735 if (glyph->u.notes.liquescentia & L_DEMINUTUS) {
736 note->gtex_offset_case = PenultBeforeDeminutus;
737 } else if (glyph->u.notes.liquescentia
738 & (L_AUCTUS_ASCENDENS | L_AUCTUS_DESCENDENS)) {
739 note->gtex_offset_case = PenultBeforePunctumWide;
740 } else {
741 note->gtex_offset_case = FinalPunctum;
742 }
743 h_episema = above_if_h_episema(note->previous);
744 if (h_episema == VPOS_BELOW && !note->next) {
745 /* handle porrectus-like flexus */
746 h_episema = above_if_fused_next_h_episema(glyph);
747 }
748 v_episema = VPOS_BELOW;
749 break;
750 default: /* case 3 */
751 low_high_set_upper(glyph, note);
752 note->gtex_offset_case = last_note_case(glyph, FinalUpperPunctum,
753 note, true);
754 h_episema = VPOS_ABOVE;
755 v_episema = below_if_auctus(glyph);
756 break;
757 }
758 break;
759 case T_SCANDICUS:
760 switch (i) {
761 case 1:
762 note->gtex_offset_case = first_note_case(note, glyph);
763 h_episema = VPOS_BELOW;
764 v_episema = VPOS_BELOW;
765 break;
766 case 2:
767 low_high_set_lower(glyph, note);
768 if (note->u.note.pitch - note->previous->u.note.pitch == 1) {
769 note->gtex_offset_case = FinalPunctum;
770 } else {
771 note->gtex_offset_case = FinalConnectedPunctum;
772 }
773 h_episema = VPOS_BELOW;
774 v_episema = VPOS_BELOW;
775 break;
776 default:
777 low_high_set_upper(glyph, note);
778 note->gtex_offset_case = last_note_case(glyph, FinalPunctum, note,
779 true);
780 h_episema = VPOS_ABOVE;
781 v_episema = VPOS_BELOW;
782 break;
783 }
784 break;
785 case T_SALICUS_FLEXUS:
786 done = true;
787 v_episema = VPOS_BELOW;
788 switch (i) {
789 case 3:
790 note->gtex_offset_case = note_before_last_note_case(glyph, note);
791 h_episema = VPOS_ABOVE;
792 break;
793 case 4:
794 note->gtex_offset_case = last_note_case(glyph, FinalPunctum, note,
795 false);
796 h_episema = VPOS_BELOW;
797 break;
798 default:
799 done = false;
800 break;
801 }
802 if (done) {
803 break;
804 }
805 /* fall through */
806 case T_SALICUS:
807 v_episema = VPOS_BELOW;
808 switch (i) {
809 case 1:
810 note->gtex_offset_case = InitialConnectedPunctum;
811 h_episema = above_if_h_episema(note->next);
812 break;
813 case 2:
814 if (note->next->u.note.pitch - note->u.note.pitch == 1) {
815 note->gtex_offset_case = SalicusOriscusOne;
816 } else {
817 note->gtex_offset_case = SalicusOriscusWide;
818 }
819 h_episema = VPOS_ABOVE;
820 break;
821 default:
822 note->gtex_offset_case = last_note_case(glyph, FinalPunctum, note,
823 false);
824 if (glyph->u.notes.liquescentia & L_DEMINUTUS) {
825 v_episema = VPOS_ABOVE;
826 }
827 h_episema = VPOS_ABOVE;
828 break;
829 }
830 break;
831 case T_TORCULUS_LIQUESCENS:
832 done = true;
833 switch (i) {
834 case 1:
835 note->gtex_offset_case = first_note_case(note, glyph);
836 h_episema = above_if_h_episema(note->next);
837 v_episema = VPOS_BELOW;
838 break;
839 case 2:
840 note->gtex_offset_case =
841 note_before_last_note_case_ignoring_deminutus(note);
842 h_episema = VPOS_ABOVE;
843 v_episema = VPOS_BELOW;
844 break;
845 default:
846 --i;
847 done = false;
848 break;
849 }
850
851 if (done) {
852 break;
853 }
854 /* fall through */
855 case T_ANCUS:
856 switch (i) {
857 case 1:
858 note->gtex_offset_case = first_note_case(note, glyph);
859 h_episema = above_if_h_episema(note->next);
860 v_episema = VPOS_BELOW;
861 break;
862 case 2:
863 note->is_upper_note = true;
864 note->gtex_offset_case = note_before_last_note_case(glyph, note);
865 h_episema = VPOS_ABOVE;
866 v_episema = below_if_next_ambitus_allows(note);
867 break;
868 default:
869 note->is_lower_note = true;
870 note->gtex_offset_case = last_note_case(glyph, FinalPunctum, note,
871 false);
872 h_episema = VPOS_BELOW;
873 v_episema = VPOS_BELOW;
874 break;
875 }
876 break;
877 case T_TORCULUS:
878 switch (i) {
879 case 1:
880 note->gtex_offset_case = first_note_case(note, glyph);
881 h_episema = above_if_h_episema(note->next);
882 v_episema = VPOS_BELOW;
883 break;
884 case 2:
885 high_low_set_upper(glyph, note);
886 note->gtex_offset_case = note_before_last_note_case(glyph, note);
887 h_episema = VPOS_ABOVE;
888 if (glyph->u.notes.liquescentia & L_DEMINUTUS) {
889 v_episema = below_if_next_ambitus_allows(note);
890 } else {
891 v_episema = VPOS_BELOW;
892 }
893 break;
894 default:
895 high_low_set_lower(glyph, note);
896 note->gtex_offset_case = last_note_case(glyph, FinalPunctum, note,
897 false);
898 h_episema = above_if_h_episema(note->previous);
899 v_episema = VPOS_BELOW;
900 break;
901 }
902 break;
903 default: /* case T_ONE_NOTE */
904 previous = gregorio_previous_non_texverb_glyph(glyph);
905 if ((!previous || previous->type != GRE_GLYPH
906 || previous->u.notes.fuse_to_next_glyph < 0)
907 && (glyph->u.notes.fuse_to_next_glyph > 0)) {
908 h_episema = above_if_fused_next_h_episema(glyph);
909 } else {
910 h_episema = VPOS_ABOVE;
911 }
912 v_episema = VPOS_BELOW;
913 switch (note->u.note.shape) {
914 case S_PUNCTUM_INCLINATUM_DEMINUTUS:
915 note->gtex_offset_case = FinalInclinatumDeminutus;
916 break;
917 case S_PUNCTUM_INCLINATUM_AUCTUS:
918 case S_PUNCTUM_INCLINATUM_ASCENDENS:
919 case S_PUNCTUM_INCLINATUM_DESCENDENS:
920 note->gtex_offset_case = FinalInclinatum;
921 break;
922 case S_STROPHA:
923 note->gtex_offset_case = FinalStropha;
924 break;
925 case S_QUILISMA:
926 note->gtex_offset_case = fused_single_note_case(glyph,
927 FinalQuilisma, LeadingQuilisma);
928 break;
929 case S_ORISCUS_ASCENDENS:
930 case S_ORISCUS_DESCENDENS:
931 case S_ORISCUS_DEMINUTUS:
932 note->gtex_offset_case = fused_single_note_case(glyph, FinalOriscus,
933 LeadingOriscus);
934 break;
935 case S_VIRGA:
936 note->gtex_offset_case = InitialVirga;
937 break;
938 case S_VIRGA_REVERSA:
939 if (glyph->u.notes.liquescentia == L_AUCTUS_DESCENDENS) {
940 note->gtex_offset_case = FinalVirgaAuctus;
941 } else {
942 note->gtex_offset_case = InitialVirga;
943 }
944 break;
945 case S_LINEA_PUNCTUM:
946 note->gtex_offset_case = FinalLineaPunctum;
947 break;
948 case S_LINEA:
949 note->gtex_offset_case = FinalPunctum;
950 break;
951 case S_FLAT:
952 note->gtex_offset_case = Flat;
953 break;
954 case S_FLAT_PAREN:
955 note->gtex_offset_case = FlatParen;
956 break;
957 case S_FLAT_SOFT:
958 note->gtex_offset_case = FlatSoft;
959 break;
960 case S_SHARP:
961 note->gtex_offset_case = Sharp;
962 break;
963 case S_SHARP_PAREN:
964 note->gtex_offset_case = SharpParen;
965 break;
966 case S_SHARP_SOFT:
967 note->gtex_offset_case = SharpSoft;
968 break;
969 case S_NATURAL:
970 note->gtex_offset_case = Natural;
971 break;
972 case S_NATURAL_PAREN:
973 note->gtex_offset_case = NaturalParen;
974 break;
975 case S_NATURAL_SOFT:
976 note->gtex_offset_case = NaturalSoft;
977 break;
978 default:
979 note->gtex_offset_case = last_note_case(glyph,
980 fused_single_note_case(glyph, FinalPunctum, LeadingPunctum),
981 note, true);
982 break;
983 }
984 break;
985 }
986
987 if (note->signs & _V_EPISEMA) {
988 if (note->v_episema_height) {
989 if (note->v_episema_height > note->u.note.pitch) {
990 /* above is always higher because of GregorioTeX's design */
991 note->v_episema_height += (int)VPOS_ABOVE;
992 }
993 }
994 else {
995 note->v_episema_height = note->u.note.pitch + (int)v_episema;
996 if ((v_episema == VPOS_BELOW && v_episema_below_is_lower)
997 || v_episema == VPOS_ABOVE) {
998 /* above is always higher because of GregorioTeX's design */
999 note->v_episema_height += (int)v_episema;
1000 }
1001 }
1002 }
1003
1004 return h_episema;
1005 }
1006
1007 static __inline char compute_h_episema_height(
1008 const gregorio_glyph *const glyph, const gregorio_note *const note,
1009 const gregorio_vposition vpos)
1010 {
1011 char height = note->u.note.pitch;
1012
1013 if (note->signs & _V_EPISEMA) {
1014 if ((vpos == VPOS_ABOVE && note->v_episema_height >= height)
1015 || (vpos == VPOS_BELOW && note->v_episema_height <= height)) {
1016 height = note->v_episema_height;
1017 }
1018 }
1019
1020 if (vpos == VPOS_ABOVE && note->choral_sign
1021 && !choral_sign_here_is_low(glyph, note, NULL)) {
1022 ++height;
1023 if (!is_on_a_line(note->u.note.pitch)) {
1024 ++height;
1025 }
1026 }
1027
1028 return height + (int)vpos;
1029 }
1030
1031 static bool is_bridgeable_space(const gregorio_element *const element)
1032 {
1033 if (element->type == GRE_SPACE) {
1034 switch (element->u.misc.unpitched.info.space) {
1035 case SP_NEUMATIC_CUT:
1036 case SP_LARGER_SPACE:
1037 case SP_NEUMATIC_CUT_NB:
1038 case SP_LARGER_SPACE_NB:
1039 return true;
1040 default:
1041 /* do nothing */
1042 break;
1043 }
1044 }
1045
1046 return false;
1047 }
1048
1049 typedef struct height_computation {
1050 const gregorio_vposition vpos;
1051 const gregorio_sign_orientation orientation;
1052 bool (*const is_applicable)(const gregorio_note *);
1053 bool (*const is_shown)(const gregorio_note *);
1054 bool (*const is_connected)(const gregorio_note *);
1055 grehepisema_size (*const get_size)(const gregorio_note *);
1056 bool (*const is_better_height)(signed char, signed char);
1057 void (*const position)(gregorio_note *, signed char, bool);
1058
1059 bool active;
1060 char height;
1061 bool connected;
1062 const gregorio_element *start_element;
1063 const gregorio_glyph *start_glyph;
1064 gregorio_note *start_note;
1065 const gregorio_element *last_connected_element;
1066 const gregorio_glyph *last_connected_glyph;
1067 gregorio_note *last_connected_note;
1068 unsigned short adjustment_index;
1069 } height_computation;
1070
1071 static bool is_h_episema_above_applicable(const gregorio_note *const note)
1072 {
1073 return note->h_episema_above || !note->is_lower_note;
1074 }
1075
1076 static bool is_h_episema_below_applicable(const gregorio_note *const note)
1077 {
1078 return note->h_episema_below || !note->is_upper_note;
1079 }
1080
1081 bool gtex_is_h_episema_above_shown(const gregorio_note *const note)
1082 {
1083 return note->h_episema_above;
1084 }
1085
1086 bool gtex_is_h_episema_below_shown(const gregorio_note *const note)
1087 {
1088 return note->h_episema_below;
1089 }
1090
1091 static bool is_h_episema_above_connected(const gregorio_note *const note)
1092 {
1093 return note->h_episema_above_connect;
1094 }
1095
1096 static bool is_h_episema_below_connected(const gregorio_note *const note)
1097 {
1098 return note->h_episema_below_connect;
1099 }
1100
1101 static grehepisema_size get_h_episema_above_size(
1102 const gregorio_note *const note)
1103 {
1104 return note->h_episema_above_size;
1105 }
1106
1107 static grehepisema_size get_h_episema_below_size(
1108 const gregorio_note *const note)
1109 {
1110 return note->h_episema_below_size;
1111 }
1112
1113 static bool is_h_episema_above_better_height(const signed char new_height,
1114 const signed char old_height)
1115 {
1116 return new_height > old_height;
1117 }
1118
1119 static bool is_h_episema_below_better_height(const signed char new_height,
1120 const signed char old_height)
1121 {
1122 return new_height < old_height;
1123 }
1124
1125 static void adjust_h_episema_above_if_better(gregorio_note *const note,
1126 signed char potential_height)
1127 {
1128 if (is_h_episema_above_better_height(potential_height,
1129 note->h_episema_above)) {
1130 note->h_episema_above = potential_height;
1131 }
1132 }
1133
1134 static void adjust_h_episema_below_if_better(gregorio_note *const note,
1135 signed char potential_height)
1136 {
1137 if (is_h_episema_below_better_height(potential_height,
1138 note->h_episema_below)) {
1139 note->h_episema_below = potential_height;
1140 }
1141 }
1142
1143 static __inline bool has_high_ledger_line(const signed char height,
1144 bool is_sign, const gregorio_score *const score)
1145 {
1146 if (is_sign) {
1147 return height > score->high_ledger_line_pitch;
1148 }
1149 return height >= score->high_ledger_line_pitch;
1150 }
1151
1152 static __inline bool has_low_ledger_line(const signed char height, bool is_sign)
1153 {
1154 if (is_sign) {
1155 return height < LOW_LEDGER_LINE_PITCH;
1156 }
1157 return height <= LOW_LEDGER_LINE_PITCH;
1158 }
1159
1160 static __inline void start_h_episema(height_computation *const h,
1161 const gregorio_element *const element,
1162 const gregorio_glyph *const glyph, gregorio_note *const note)
1163 {
1164 h->start_element = element;
1165 h->start_glyph = glyph;
1166 h->start_note = note;
1167 h->active = true;
1168 h->height = compute_h_episema_height(glyph, note, h->vpos);
1169 }
1170
1171 static __inline void position_h_episema(gregorio_note *const note,
1172 const height_computation *const h, const bool connect,
1173 const bool high_ledger_line, const bool low_ledger_line)
1174 {
1175 const unsigned short adjustment_index =
1176 note->he_adjustment_index[h->orientation];
1177
1178 h->position(note, h->height, connect);
1179 if (adjustment_index) {
1180 gregorio_hepisema_adjustment *adj = gregorio_get_hepisema_adjustment(
1181 adjustment_index);
1182 if (adj->pitch_extremum == NO_PITCH
1183 || h->is_better_height(h->height, adj->pitch_extremum)) {
1184 adj->pitch_extremum = h->height;
1185 }
1186 }
1187 if (!note->high_ledger_specificity && !note->high_ledger_line) {
1188 note->high_ledger_line = high_ledger_line;
1189 }
1190 if (!note->low_ledger_specificity && !note->low_ledger_line) {
1191 note->low_ledger_line = low_ledger_line;
1192 }
1193 }
1194
1195 static __inline void next_has_ledger_line(
1196 const height_computation *const h, bool *high_ledger_line,
1197 bool *low_ledger_line)
1198 {
1199 const gregorio_element *element = h->last_connected_element;
1200 const gregorio_glyph *glyph = h->last_connected_glyph;
1201 gregorio_note *note = h->last_connected_note;
1202 bool eat_upper_note = note->is_lower_note, keep_going = false;
1203
1204 if (!*high_ledger_line || !*low_ledger_line) {
1205 /* going forwards, we must iterate through any lower notes */
1206 do {
1207 note = note->next;
1208 if (!note) {
1209 do {
1210 glyph = glyph->next;
1211 if (!glyph) {
1212 do {
1213 element = element->next;
1214 if (!element) {
1215 return;
1216 }
1217 } while (element->type != GRE_ELEMENT);
1218 glyph = element->u.first_glyph;
1219 }
1220 } while (glyph->type != GRE_GLYPH);
1221 note = glyph->u.notes.first_note;
1222 }
1223
1224 *high_ledger_line = *high_ledger_line || note->high_ledger_line;
1225 *low_ledger_line = *low_ledger_line || note->low_ledger_line;
1226
1227 if (keep_going) {
1228 keep_going = false;
1229 }
1230 if (eat_upper_note && note->is_upper_note) {
1231 eat_upper_note = false;
1232 keep_going = true;
1233 }
1234 } while ((!*high_ledger_line || !*low_ledger_line)
1235 && (note->is_lower_note || keep_going));
1236 }
1237 }
1238
1239 static __inline void previous_has_ledger_line(
1240 const height_computation *const h, bool *high_ledger_line,
1241 bool *low_ledger_line)
1242 {
1243 const gregorio_element *element = h->start_element;
1244 const gregorio_glyph *glyph = h->start_glyph;
1245 gregorio_note *note = h->start_note;
1246 bool eat_lower_note = note->is_upper_note, keep_going = false;
1247
1248 if (!*high_ledger_line || !*low_ledger_line) {
1249 /* going backwards, we must iterate through any upper notes */
1250 do {
1251 note = note->previous;
1252 if (!note) {
1253 do {
1254 glyph = glyph->previous;
1255 if (!glyph) {
1256 do {
1257 element = element->previous;
1258 if (!element) {
1259 return;
1260 }
1261 } while (element->type != GRE_ELEMENT);
1262 for (glyph = element->u.first_glyph; glyph->next;
1263 glyph = glyph->next) {
1264 /* just iterate to find the last glyph */
1265 }
1266 }
1267 } while (glyph->type != GRE_GLYPH);
1268 note = gregorio_glyph_last_note(glyph);
1269 }
1270 *high_ledger_line = *high_ledger_line || note->high_ledger_line;
1271 *low_ledger_line = *low_ledger_line || note->low_ledger_line;
1272
1273 if (keep_going) {
1274 keep_going = false;
1275 }
1276 if (eat_lower_note && note->is_lower_note) {
1277 eat_lower_note = false;
1278 keep_going = true;
1279 }
1280 } while ((!*high_ledger_line || !*low_ledger_line)
1281 && (note->is_upper_note || keep_going));
1282 }
1283 }
1284
1285 static __inline void set_h_episema_height(const height_computation *const h,
1286 gregorio_note *const end, const gregorio_score *const score)
1287 {
1288 gregorio_note *last_note = NULL;
1289
1290 const gregorio_element *element = h->start_element;
1291 const gregorio_glyph *glyph = h->start_glyph;
1292 gregorio_note *note = h->start_note;
1293
1294 bool high_ledger_line = has_high_ledger_line(h->height, true, score)
1295 || has_high_ledger_line(h->height - h->vpos, false, score);
1296 bool low_ledger_line = has_low_ledger_line(h->height, true)
1297 || has_low_ledger_line(h->height - h->vpos, false);
1298
1299 next_has_ledger_line(h, &high_ledger_line, &low_ledger_line);
1300 previous_has_ledger_line(h, &high_ledger_line, &low_ledger_line);
1301
1302 for ( ; element; element = element->next) {
1303 if (element->type == GRE_ELEMENT) {
1304 for (glyph = glyph? glyph : element->u.first_glyph; glyph;
1305 glyph = glyph->next) {
1306 if (glyph && glyph->type == GRE_GLYPH) {
1307 for (note = note? note : glyph->u.notes.first_note; note;
1308 note = note->next) {
1309 if (end && note == end) {
1310 if (last_note) {
1311 position_h_episema(last_note, h, false,
1312 high_ledger_line, low_ledger_line);
1313 }
1314 return;
1315 }
1316 if (h->is_applicable(note)) {
1317 position_h_episema(note, h, true, high_ledger_line,
1318 low_ledger_line);
1319 last_note = note;
1320 }
1321 }
1322 }
1323 }
1324 } else if (!is_bridgeable_space(element)) {
1325 break;
1326 }
1327 }
1328
1329 if (last_note) {
1330 position_h_episema(last_note, h, false, high_ledger_line,
1331 low_ledger_line);
1332 }
1333 }
1334
1335 static __inline bool is_connected_left(const grehepisema_size size) {
1336 return size == H_NORMAL || size == H_SMALL_LEFT;
1337 }
1338
1339 static __inline bool is_connected_right(const grehepisema_size size) {
1340 return size == H_NORMAL || size == H_SMALL_RIGHT;
1341 }
1342
1343 static __inline bool is_connectable_interglyph_ambitus(const signed char first,
1344 const signed char second)
1345 {
1346 return abs(first - second) < 3;
1347 }
1348
1349 static __inline bool is_connectable_interglyph_notes(
1350 const gregorio_note *const first, const gregorio_note *const second)
1351 {
1352 return first && second && is_connectable_interglyph_ambitus(
1353 first->u.note.pitch, second->u.note.pitch);
1354 }
1355
1356 static __inline bool has_space_to_left(const gregorio_note *const note) {
1357 switch (note->u.note.shape) {
1358 case S_PUNCTUM_INCLINATUM_ASCENDENS:
1359 case S_PUNCTUM_INCLINATUM_DESCENDENS:
1360 case S_PUNCTUM_INCLINATUM_DEMINUTUS:
1361 case S_PUNCTUM_INCLINATUM_AUCTUS:
1362 return !is_connectable_interglyph_notes(note->previous, note);
1363
1364 default:
1365 return !note->previous;
1366 }
1367 }
1368
1369 static __inline void end_h_episema(height_computation *const h,
1370 gregorio_note *const end, const gregorio_score *const score)
1371 {
1372 signed char proposed_height;
1373
1374 if (h->active) {
1375 /* don't let the episema clash with the note before or after */
1376 if (is_connected_left(h->get_size(h->start_note))) {
1377 if (h->start_note->previous) {
1378 if (!has_space_to_left(h->start_note)
1379 && h->start_note->previous->type == GRE_NOTE
1380 && is_connected_right(h->get_size(
1381 h->start_note->previous))) {
1382 proposed_height = h->start_note->previous->u.note.pitch + h->vpos;
1383 if (h->is_better_height(proposed_height, h->height)) {
1384 h->height = proposed_height;
1385 }
1386 }
1387 } else {
1388 const gregorio_glyph *previous =
1389 gregorio_previous_non_texverb_glyph(h->start_glyph);
1390 if (previous && previous->type == GRE_GLYPH
1391 && is_fused(h->start_glyph->u.notes.liquescentia)) {
1392 gregorio_note *note = gregorio_glyph_last_note(
1393 h->start_glyph->previous);
1394 if (note->type == GRE_NOTE
1395 && is_connected_right(h->get_size(note))) {
1396 proposed_height = note->u.note.pitch + h->vpos;
1397 if (h->is_better_height(proposed_height, h->height)) {
1398 h->height = proposed_height;
1399 }
1400 }
1401 }
1402 }
1403 }
1404 if (end && end->type == GRE_NOTE && is_connected_left(h->get_size(end))
1405 && !(end->u.note.liquescentia & L_DEMINUTUS)
1406 && h->last_connected_note
1407 && is_connected_right(h->get_size(h->last_connected_note))) {
1408 if (h->last_connected_note->next) {
1409 if (h->last_connected_note->next->type == GRE_NOTE
1410 && !has_space_to_left(h->last_connected_note->next)) {
1411 gregorio_note *note;
1412 /* this loop checks that it's within the same glyph */
1413 for (note = end->previous; note; note = note->previous) {
1414 if (note == h->start_note) {
1415 proposed_height = end->u.note.pitch + h->vpos;
1416 if (h->is_better_height(proposed_height,
1417 h->height)) {
1418 h->height = proposed_height;
1419 }
1420 break;
1421 }
1422 }
1423 }
1424 } else if (is_fused(end->u.note.liquescentia) && !end->previous) {
1425 proposed_height = end->u.note.pitch + h->vpos;
1426 if (h->is_better_height(proposed_height, h->height)) {
1427 h->height = proposed_height;
1428 }
1429 }
1430 }
1431
1432 set_h_episema_height(h, end, score);
1433
1434 h->active = false;
1435 h->height = 0;
1436 h->connected = false;
1437 h->start_element = NULL;
1438 h->start_glyph = NULL;
1439 h->start_note = NULL;
1440 h->last_connected_element = NULL;
1441 h->last_connected_glyph = NULL;
1442 h->last_connected_note = NULL;
1443 }
1444 }
1445
1446 static __inline void compute_h_episema(height_computation *const h,
1447 const gregorio_element *const element,
1448 const gregorio_glyph *const glyph, gregorio_note *const note,
1449 const int i, const gtex_type type, const gregorio_score *const score)
1450 {
1451 grehepisema_size size;
1452
1453 if (h->is_applicable(note)) {
1454 if (h->is_shown(note)) {
1455 size = h->get_size(note);
1456
1457 if (h->active) {
1458 const signed char next_height = compute_h_episema_height(
1459 glyph, note, h->vpos);
1460 if (h->connected && is_connected_left(size)
1461 && (i != 1 || is_connectable_interglyph_ambitus(
1462 next_height, h->height))) {
1463 if (h->is_better_height(next_height, h->height)) {
1464 h->height = next_height;
1465 }
1466 }
1467 else {
1468 end_h_episema(h, note, score);
1469 start_h_episema(h, element, glyph, note);
1470 }
1471 } else {
1472 start_h_episema(h, element, glyph, note);
1473 }
1474
1475 h->connected = h->is_connected(note) && is_connected_right(size);
1476 h->last_connected_element = element;
1477 h->last_connected_glyph = glyph;
1478 h->last_connected_note = note;
1479 } else {
1480 end_h_episema(h, note, score);
1481 }
1482 } else if (!h->is_shown(note)) {
1483 /* special handling for porrectus shapes because of their shape:
1484 * the lower note of the porrectus stroke is normally not applicable,
1485 * but we have to end the episema on the upper note if the episema
1486 * on the lower note is not shown. */
1487 switch(type) {
1488 case T_PORRECTUS:
1489 case T_PORRECTUS_FLEXUS:
1490 if (i == 2) {
1491 end_h_episema(h, note, score);
1492 }
1493 break;
1494
1495 case T_TORCULUS_RESUPINUS:
1496 case T_TORCULUS_RESUPINUS_FLEXUS:
1497 if (i == 3) {
1498 end_h_episema(h, note, score);
1499 }
1500 break;
1501
1502 default:
1503 /* do nothing */
1504 break;
1505 }
1506 }
1507 }
1508
1509 static __inline void compute_note_positioning(height_computation *const above,
1510 height_computation *const below, const gregorio_element *const element,
1511 const gregorio_glyph *const glyph, gregorio_note *const note,
1512 const int i, const gtex_type type, const gregorio_score *const score)
1513 {
1514 gregorio_vposition default_vpos = advise_positioning(glyph, note, i, type);
1515
1516 if (note->h_episema_above == HEPISEMA_AUTO
1517 && note->h_episema_below == HEPISEMA_AUTO) {
1518 if (default_vpos == VPOS_BELOW) {
1519 note->h_episema_above = HEPISEMA_NONE;
1520 }
1521 else { /* default_vpos == VPOS_ABOVE */
1522 note->h_episema_below = HEPISEMA_NONE;
1523 }
1524 }
1525
1526 compute_h_episema(above, element, glyph, note, i, type, score);
1527 compute_h_episema(below, element, glyph, note, i, type, score);
1528 }
1529
1530 static __inline int compute_fused_shift(const gregorio_glyph *glyph)
1531 {
1532 int shift;
1533 gregorio_note *first_note, *prev_note;
1534 const gregorio_glyph *next, *previous;
1535 bool next_is_fused;
1536
1537 if (!glyph || !is_fused(glyph->u.notes.liquescentia)
1538 || glyph->type != GRE_GLYPH
1539 || !(first_note = glyph->u.notes.first_note)
1540 || first_note->type != GRE_NOTE
1541 || !(previous = gregorio_previous_non_texverb_glyph(glyph))
1542 || previous->type != GRE_GLYPH
1543 || previous->u.notes.liquescentia & TAIL_LIQUESCENTIA_MASK
1544 || !previous->u.notes.first_note) {
1545 /* base conditions prevent fusion */
1546 return 0;
1547 }
1548
1549 next = gregorio_next_non_texverb_glyph(glyph);
1550 next_is_fused = next && next->type == GRE_GLYPH
1551 && is_fused(next->u.notes.liquescentia);
1552
1553 switch (glyph->u.notes.glyph_type) {
1554 case G_PORRECTUS:
1555 case G_PODATUS:
1556 if (next_is_fused) {
1557 return 0;
1558 }
1559 /* else it may be fused to the previous note */
1560 break;
1561
1562 case G_PUNCTUM:
1563 case G_FLEXA:
1564 case G_VIRGA:
1565 /* these may be fused to the previous note */
1566 break;
1567
1568 default:
1569 /* anything else may not be fused to the previous note */
1570 return 0;
1571 }
1572
1573 switch (first_note->u.note.shape) {
1574 case S_QUILISMA:
1575 case S_QUILISMA_QUADRATUM:
1576 case S_FLAT:
1577 case S_SHARP:
1578 case S_NATURAL:
1579 /* if this glyph starts with one of these, it's not fusible */
1580 return 0;
1581
1582 default:
1583 /* anything else is potentially fusible */
1584 break;
1585 }
1586
1587 switch (previous->u.notes.glyph_type) {
1588 case G_PUNCTUM:
1589 case G_FLEXA:
1590 case G_VIRGA_REVERSA:
1591 /* these are potentially fusible to this note */
1592 break;
1593
1594 default:
1595 /* everything else is not fusible */
1596 return 0;
1597 }
1598
1599 prev_note = gregorio_glyph_last_note(previous);
1600
1601 gregorio_assert(prev_note->type == GRE_NOTE, compute_fused_shift,
1602 "previous note wasn't a note", return 0);
1603
1604 if (prev_note->u.note.shape == S_VIRGA) {
1605 /* cannot fuse after a virga */
1606 return 0;
1607 }
1608
1609 shift = first_note->u.note.pitch - prev_note->u.note.pitch;
1610 gregorio_assert(shift >= -MAX_AMBITUS && shift <= MAX_AMBITUS,
1611 compute_fused_shift, "ambitus too large to fuse", return 0);
1612
1613 if (shift > 0 && previous->u.notes.glyph_type == G_VIRGA_REVERSA) {
1614 /* virga reversa cannot fuse upwards */
1615 return 0;
1616 }
1617
1618 if (shift < 0) {
1619 if (glyph->u.notes.glyph_type == G_PODATUS) {
1620 if (glyph->u.notes.liquescentia & L_DEMINUTUS) {
1621 /* podatus deminutus may be fused from above */
1622 return shift;
1623 }
1624 switch (first_note->u.note.shape) {
1625 case S_ORISCUS_ASCENDENS:
1626 case S_ORISCUS_DESCENDENS:
1627 case S_ORISCUS_SCAPUS_ASCENDENS:
1628 case S_ORISCUS_SCAPUS_DESCENDENS:
1629 return shift;
1630 default:
1631 /* otherwise, podatus may not be fused from above */
1632 return 0;
1633 }
1634 }
1635
1636 /* the FLEXA check below checks for a porrectus-like flexus, which is
1637 * not fusible from above */
1638 if ((next_is_fused && glyph->u.notes.glyph_type == G_FLEXA)
1639 || glyph->u.notes.glyph_type == G_PORRECTUS
1640 || (previous->u.notes.glyph_type == G_PUNCTUM
1641 && is_initio_debilis(previous->u.notes.liquescentia))) {
1642 /* may not be fused from above */
1643 return 0;
1644 }
1645 }
1646
1647 return shift;
1648 }
1649
1650 void gregoriotex_compute_positioning(
1651 const gregorio_element *const param_element,
1652 const gregorio_score *const score)
1653 {
1654 height_computation above = {
1655 /*.vpos =*/ VPOS_ABOVE,
1656 /*.orientation =*/ SO_OVER,
1657 /*.is_applicable =*/ &is_h_episema_above_applicable,
1658 /*.is_shown =*/ &gtex_is_h_episema_above_shown,
1659 /*.is_connected =*/ &is_h_episema_above_connected,
1660 /*.get_size =*/ &get_h_episema_above_size,
1661 /*.is_better_height =*/ &is_h_episema_above_better_height,
1662 /*.position =*/ &gregorio_position_h_episema_above,
1663
1664 /*.active =*/ false,
1665 /*.height =*/ 0,
1666 /*.connected =*/ false,
1667 /*.start_element =*/ NULL,
1668 /*.start_glyph =*/ NULL,
1669 /*.start_note =*/ NULL,
1670 /*.last_connected_element =*/ NULL,
1671 /*.last_connected_glyph =*/ NULL,
1672 /*.last_connected_note =*/ NULL,
1673 /*.adjustment_index =*/ 0,
1674 };
1675 height_computation below = {
1676 /*.vpos =*/ VPOS_BELOW,
1677 /*.orientation =*/ SO_UNDER,
1678 /*.is_applicable =*/ &is_h_episema_below_applicable,
1679 /*.is_shown =*/ &gtex_is_h_episema_below_shown,
1680 /*.is_connected =*/ &is_h_episema_below_connected,
1681 /*.get_size =*/ &get_h_episema_below_size,
1682 /*.is_better_height =*/ &is_h_episema_below_better_height,
1683 /*.position =*/ &gregorio_position_h_episema_below,
1684
1685 /*.active =*/ false,
1686 /*.height =*/ 0,
1687 /*.connected =*/ false,
1688 /*.start_element =*/ NULL,
1689 /*.start_glyph =*/ NULL,
1690 /*.start_note =*/ NULL,
1691 /*.last_connected_element =*/ NULL,
1692 /*.last_connected_glyph =*/ NULL,
1693 /*.last_connected_note =*/ NULL,
1694 /*.adjustment_index =*/ 0,
1695 };
1696 int i;
1697 gtex_alignment ignored;
1698 gtex_type type;
1699 const gregorio_element *element;
1700
1701 for (element = param_element; element; element = element->next) {
1702 if (element->type == GRE_ELEMENT) {
1703 gregorio_glyph *glyph;
1704 for (glyph = element->u.first_glyph; glyph;
1705 glyph = glyph->next) {
1706 if (glyph->type == GRE_GLYPH) {
1707 gregorio_note *note;
1708 glyph->u.notes.fuse_to_next_glyph = compute_fused_shift(
1709 gregorio_next_non_texverb_glyph(glyph));
1710 i = 0;
1711 gregoriotex_determine_glyph_name(glyph, &ignored, &type);
1712 for (note = glyph->u.notes.first_note; note;
1713 note = note->next) {
1714 if (note->type == GRE_NOTE) {
1715 compute_note_positioning(&above, &below, element,
1716 glyph, note, ++i, type, score);
1717 }
1718 }
1719 }
1720 }
1721 } else if (!is_bridgeable_space(element)) {
1722 end_h_episema(&above, NULL, score);
1723 end_h_episema(&below, NULL, score);
1724 }
1725 }
1726 end_h_episema(&above, NULL, score);
1727 end_h_episema(&below, NULL, score);
1728 }
1729
1730 static __inline void adjust_hepisema(gregorio_note *const note,
1731 const gregorio_sign_orientation orientation,
1732 bool (*const is_shown)(const gregorio_note *),
1733 void (*const adjust_if_better)(gregorio_note *, signed char))
1734 {
1735 const unsigned short adjustment_index =
1736 note->he_adjustment_index[orientation];
1737
1738 if (adjustment_index && is_shown(note)) {
1739 gregorio_hepisema_adjustment *adj = gregorio_get_hepisema_adjustment(
1740 adjustment_index);
1741
1742 adjust_if_better(note, adj->pitch_extremum);
1743 }
1744 }
1745
1746 void gregoriotex_compute_cross_syllable_positioning(
1747 const gregorio_score *const score)
1748 {
1749 gregorio_syllable *syllable;
1750 for (syllable = score->first_syllable; syllable;
1751 syllable = syllable->next_syllable) {
1752 int voice;
1753 for (voice = 0; voice < score->number_of_voices; ++voice) {
1754 gregorio_element *element;
1755 for (element = syllable->elements[voice]; element;
1756 element = element->next) {
1757 if (element->type == GRE_ELEMENT) {
1758 gregorio_glyph *glyph;
1759 for (glyph = element->u.first_glyph; glyph;
1760 glyph = glyph->next) {
1761 if (glyph->type == GRE_GLYPH) {
1762 gregorio_note *note;
1763 for (note = glyph->u.notes.first_note; note;
1764 note = note->next) {
1765 if (note->type == GRE_NOTE) {
1766 adjust_hepisema(note, SO_OVER,
1767 gtex_is_h_episema_above_shown,
1768 adjust_h_episema_above_if_better);
1769 adjust_hepisema(note, SO_UNDER,
1770 gtex_is_h_episema_below_shown,
1771 adjust_h_episema_below_if_better);
1772 }
1773 }
1774 }
1775 }
1776 }
1777 }
1778 }
1779 }
1780 }