Debian Codegraph @

texk/gregorio/gregorio-src/src/gabc/gabc-elements-determination.c

raw
1 /*
2 * Gregorio is a program that translates gabc files to GregorioTeX.
3 * This file provides functions for determining elements from notes.
4 *
5 * Copyright (C) 2006-2025 The Gregorio Project (see CONTRIBUTORS.md)
6 *
7 * This file is part of Gregorio.
8 *
9 * This program is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "config.h"
24 #include <stdio.h>
25 #include "bool.h"
26 #include "struct.h"
27 #include "messages.h"
28
29 #include "gabc.h"
30
31 #define SINGLE_NOTE_GLYPH \
32 G_PUNCTUM: \
33 case G_VIRGA: \
34 case G_BIVIRGA: \
35 case G_TRIVIRGA: \
36 case G_VIRGA_REVERSA: \
37 case G_STROPHA: \
38 case G_STROPHA_AUCTA: \
39 case G_DISTROPHA: \
40 case G_DISTROPHA_AUCTA: \
41 case G_TRISTROPHA: \
42 case G_TRISTROPHA_AUCTA
43
44 #define PUNCTA_INCLINATA_ASCENDENS_GLYPH \
45 G_2_PUNCTA_INCLINATA_ASCENDENS: \
46 case G_3_PUNCTA_INCLINATA_ASCENDENS: \
47 case G_4_PUNCTA_INCLINATA_ASCENDENS: \
48 case G_5_PUNCTA_INCLINATA_ASCENDENS
49
50 #define PUNCTA_INCLINATA_DESCENDENS_GLYPH \
51 G_2_PUNCTA_INCLINATA_DESCENDENS: \
52 case G_3_PUNCTA_INCLINATA_DESCENDENS: \
53 case G_4_PUNCTA_INCLINATA_DESCENDENS: \
54 case G_5_PUNCTA_INCLINATA_DESCENDENS
55
56 static __inline signed char glyph_note_ambitus(
57 const gregorio_glyph *const current_glyph,
58 const gregorio_glyph *const previous_glyph)
59 {
60 return current_glyph->u.notes.first_note->u.note.pitch -
61 gregorio_glyph_last_note(previous_glyph)->u.note.pitch;
62 }
63
64 /*
65 *
66 * A function that will be called several times: it adds an element to the
67 * current element current_element: the current_element in the determination,
68 * it will be updated to the element that we will add first_glyph: the
69 * first_glyph of the element that we will add current_glyph: the last glyph
70 * that will be in the element
71 *
72 */
73
74 static void close_element(gregorio_element **current_element,
75 gregorio_glyph **const first_glyph,
76 const gregorio_glyph *const current_glyph)
77 {
78 gregorio_add_element(current_element, *first_glyph);
79 if (*first_glyph && (*first_glyph)->previous) {
80 (*first_glyph)->previous->next = NULL;
81 (*first_glyph)->previous = NULL;
82 }
83 *first_glyph = current_glyph->next;
84 }
85
86 /*
87 *
88 * inline function to automatically do two or three things
89 *
90 */
91 static __inline void cut_before(gregorio_glyph *current_glyph,
92 gregorio_glyph **first_glyph,
93 gregorio_element **current_element)
94 {
95 if (*first_glyph != current_glyph) {
96 close_element(current_element, first_glyph, current_glyph);
97 /* yes, this is changing value close_element sets for first_glyph */
98 *first_glyph = current_glyph;
99 }
100 }
101
102 /*
103 *
104 * The big function of the file, but rather simple I think.
105 *
106 */
107
108 static gregorio_element *gabc_det_elements_from_glyphs(
109 gregorio_glyph *current_glyph)
110 {
111 /* the last element we have successfully added to the list of elements */
112 gregorio_element *current_element = NULL;
113 /* the first element, that we will return at the end. We have to consider
114 * it because the gregorio_element struct does not have previous_element
115 * element. */
116 gregorio_element *first_element = NULL;
117 /* the first_glyph of the element that we are currently determining */
118 gregorio_glyph *first_glyph = current_glyph;
119 /* the last real (GRE_GLYPH) that we have processed */
120 gregorio_glyph *previous_glyph = NULL;
121 /* boolean necessary to determine some cases */
122 bool do_not_cut = false;
123 bool force_cut = false;
124
125 gregorio_not_null(current_glyph, gabc_det_elements_from_glyphs, return NULL);
126 /* first we go to the first glyph in the chained list of glyphs (maybe to
127 * suppress ?) */
128 gregorio_go_to_first_glyph(&current_glyph);
129
130 while (current_glyph) {
131 if (current_glyph->type != GRE_GLYPH) {
132 force_cut = false;
133 /* we must not cut after a glyph-level space */
134 if (current_glyph->type == GRE_SPACE) {
135 switch (current_glyph->u.misc.unpitched.info.space) {
136 case SP_ZERO_WIDTH:
137 case SP_HALF_SPACE:
138 case SP_INTERGLYPH_SPACE:
139 if (!current_glyph->next) {
140 close_element(&current_element, &first_glyph, current_glyph);
141 }
142 current_glyph = current_glyph->next;
143 do_not_cut = true;
144 continue;
145 default:
146 /* any other space should be handled normally */
147 break;
148 }
149 } else if (current_glyph->type == GRE_TEXVERB_GLYPH) {
150 /* we must not cut after a texverb */
151 if (!current_glyph->next) {
152 close_element(&current_element, &first_glyph, current_glyph);
153 }
154 current_glyph = current_glyph->next;
155 do_not_cut = true;
156 continue;
157 }
158 /* clef change or space or end of line */
159 cut_before(current_glyph, &first_glyph, &current_element);
160 /* if statement to make neumatic cuts not appear in elements, as
161 * there is always one between elements, unless the next element
162 * is a space */
163 if (current_glyph->type != GRE_SPACE
164 || current_glyph->u.misc.unpitched.info.space
165 != SP_NEUMATIC_CUT
166 || (current_glyph->next
167 && current_glyph->next->type == GRE_SPACE)) {
168 /* clef change or space other than neumatic cut */
169 if (!first_element) {
170 first_element = current_element;
171 }
172 gregorio_add_misc_element(&current_element, current_glyph->type,
173 &(current_glyph->u.misc),
174 current_glyph->texverb);
175 }
176 first_glyph = current_glyph->next;
177 previous_glyph = NULL;
178 current_glyph->texverb = 0;
179 gregorio_free_one_glyph(&current_glyph);
180 continue;
181 }
182
183 if (is_fused(current_glyph->u.notes.liquescentia)) {
184 do_not_cut = true;
185 } else if (force_cut) {
186 cut_before(current_glyph, &first_glyph, &current_element);
187 previous_glyph = NULL;
188 }
189 force_cut = false;
190
191 switch (current_glyph->u.notes.glyph_type) {
192 case PUNCTA_INCLINATA_ASCENDENS_GLYPH:
193 case G_PUNCTUM_INCLINATUM:
194 case G_ALTERATION:
195 if (!do_not_cut) {
196 cut_before(current_glyph, &first_glyph, &current_element);
197 do_not_cut = true;
198 }
199 break;
200
201 case G_STROPHA_AUCTA:
202 case G_STROPHA:
203 if (current_glyph->u.notes.liquescentia
204 & (L_AUCTUS_ASCENDENS | L_AUCTUS_DESCENDENS)) {
205 force_cut = true;
206 }
207 /* fall through */
208 case PUNCTA_INCLINATA_DESCENDENS_GLYPH:
209 /* we don't cut before, so we don't do anything else */
210 if (do_not_cut) {
211 do_not_cut = false;
212 }
213 break;
214
215 default:
216 if (previous_glyph && previous_glyph->type == GRE_GLYPH
217 && !is_tail_liquescentia(
218 previous_glyph->u.notes.liquescentia)) {
219 bool break_early = false;
220 signed char ambitus;
221
222 switch (previous_glyph->u.notes.glyph_type) {
223 case SINGLE_NOTE_GLYPH:
224 /* we determine the last pitch */
225 ambitus = glyph_note_ambitus(current_glyph, previous_glyph);
226 if (ambitus == 0) {
227 do_not_cut = false;
228 break_early = true;
229 }
230 break;
231
232 case PUNCTA_INCLINATA_DESCENDENS_GLYPH:
233 case G_PUNCTUM_INCLINATUM:
234 switch (current_glyph->u.notes.glyph_type) {
235 case SINGLE_NOTE_GLYPH:
236 ambitus = glyph_note_ambitus(current_glyph, previous_glyph);
237 if (ambitus > -2 && ambitus < 2) {
238 do_not_cut = false;
239 break_early = true;
240 }
241 break;
242
243 default:
244 /* do nothing in particular */
245 break;
246 }
247 break;
248
249 case G_PODATUS:
250 if (current_glyph->u.notes.glyph_type == G_VIRGA) {
251 ambitus = glyph_note_ambitus(current_glyph, previous_glyph);
252 if (ambitus >= 0) {
253 do_not_cut = false;
254 break_early = true;
255 }
256 }
257 break;
258
259 default:
260 /* do nothing in particular */
261 break;
262 }
263
264 if (break_early) {
265 break;
266 }
267 }
268
269 if (do_not_cut) {
270 do_not_cut = false;
271 } else {
272 cut_before(current_glyph, &first_glyph, &current_element);
273 }
274 }
275
276 if (gregorio_glyph_last_note(current_glyph)->signs
277 & (_PUNCTUM_MORA | _AUCTUM_DUPLEX)) {
278 force_cut = true;
279 }
280
281 /* we must determine the first element, that we will return */
282 if (!first_element && current_element) {
283 first_element = current_element;
284 }
285 if (!current_glyph->next) {
286 close_element(&current_element, &first_glyph, current_glyph);
287 }
288
289 previous_glyph = current_glyph;
290 current_glyph = current_glyph->next;
291 } /* end of while */
292
293 /*
294 * we must determine the first element, that we will return
295 */
296 if (!first_element && current_element) {
297 first_element = current_element;
298 }
299 return first_element;
300 }
301
302 /*
303 *
304 * Two "hat" functions, they permit to have a good API. They amost don't do
305 * anything except calling the det_elements_from_glyphs.
306 *
307 * All those functions change the current_key, according to the possible new
308 * values (with key changes)
309 *
310 */
311
312 static gregorio_element *gabc_det_elements_from_notes(
313 gregorio_note *current_note, int *current_key,
314 gregorio_shape *const punctum_inclinatum_orientation,
315 const gregorio_score *const score)
316 {
317 gregorio_element *final = NULL;
318 gregorio_glyph *tmp = gabc_det_glyphs_from_notes(current_note, current_key,
319 punctum_inclinatum_orientation, score);
320 final = gabc_det_elements_from_glyphs(tmp);
321 return final;
322 }
323
324 gregorio_element *gabc_det_elements_from_string(char *const str,
325 int *const current_key, char *macros[10],
326 gregorio_scanner_location *const loc,
327 gregorio_shape *const punctum_inclinatum_orientation,
328 const gregorio_score *const score)
329 {
330 gregorio_element *final;
331 gregorio_note *tmp;
332 tmp = gabc_det_notes_from_string(str, macros, loc, score);
333 final = gabc_det_elements_from_notes(tmp, current_key,
334 punctum_inclinatum_orientation, score);
335 return final;
336 }
337