Debian Codegraph @

texk/gregorio/gregorio-src/src/characters.c

raw
1 /*
2 * Gregorio is a program that translates gabc files to GregorioTeX.
3 * This file contains functions that deal with lyrics and styles.
4 *
5 * Copyright (C) 2008-2025 The Gregorio Project (see CONTRIBUTORS.md)
6 *
7 * This file is part of Gregorio.
8 *
9 * Gregorio is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Gregorio is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Gregorio. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /**
24 * @file
25 * @brief This file contains almost all the gregorio_character manipulation things.
26 *
27 * @warning The code is very spaghetti, but I don't really think I can do it another way with the chosen representation of styles, and the fact that everything must be xml-compliant and tex-compliant...
28 * So this file is basically very hard to maintain. Moreover, it has not been very well coded, so it is even harder to maintain...
29 */
30
31 #include "config.h"
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <sys/stat.h>
39 #include <assert.h>
40 #include "bool.h"
41 #include "struct.h"
42 #include "unicode.h"
43 #include "characters.h"
44 #include "messages.h"
45 #include "support.h"
46 #include "utf8strings.h"
47 #include "vowel/vowel.h"
48
49 #define VOWEL_FILE "gregorio-vowels.dat"
50
51 static bool read_vowel_rules(char *const lang) {
52 char *language = lang;
53 rulefile_parse_status status = RFPS_NOT_FOUND;
54 char **filenames, *filename, **p;
55 const char *description;
56 int tries;
57
58 gregorio_kpse_find_or_else(filenames, VOWEL_FILE, return false);
59
60 gregorio_vowel_tables_init();
61 /* only need to try twice; if it's not resolved by then, there is an alias
62 * loop */
63 for (tries = 0; tries < 2; ++tries) {
64 for (p = filenames; status != RFPS_FOUND && (filename = *p); ++p) {
65 /* read and parse the file */
66 gregorio_messagef("read_rules", VERBOSITY_INFO, 0,
67 _("Looking for %s in %s"), language, filename);
68 gregorio_vowel_tables_load(filename, &language, &status);
69 switch (status) {
70 case RFPS_FOUND:
71 description = "Found";
72 break;
73 case RFPS_ALIASED:
74 description = "Aliased to";
75 break;
76 default:
77 description = "Could not find";
78 break;
79 }
80 gregorio_messagef("read_rules", VERBOSITY_INFO, 0, _("%s %s in %s"),
81 description, language, filename);
82 }
83 if (status != RFPS_ALIASED) {
84 /* if it's not aliased, there's no reason to scan again */
85 break;
86 }
87 }
88 if ((strcmp(language, "Latin") == 0 || strcmp(language, "latin") == 0 || strcmp(language, "la") == 0 || strcmp(language, "lat") == 0) && status == RFPS_NOT_FOUND) {
89 gregorio_messagef("read_rules", VERBOSITY_INFO, 0, "Falling back on internal Latin vowel rules");
90 }
91 if (status == RFPS_ALIASED) {
92 gregorio_messagef("read_rules", VERBOSITY_WARNING, 0,
93 _("Unable to resolve alias for %s"), lang);
94 }
95
96 /* free the allocated memory */
97 for (p = filenames; *p; ++p) {
98 free(*p);
99 }
100 free(filenames);
101 if (language != lang) {
102 free(language);
103 }
104
105 return status == RFPS_FOUND;
106 }
107
108 void gregorio_set_centering_language(char *const language)
109 {
110 if (!read_vowel_rules(language)) {
111 if (strcmp(language, "Latin") != 0 && strcmp(language, "latin") != 0 && strcmp(language, "la") != 0 && strcmp(language, "lat") != 0) {
112 gregorio_messagef("gregorio_set_centering_language",
113 VERBOSITY_WARNING, 0, _("unable to read vowel files for "
114 "%s; defaulting to Latin vowel rules"), language);
115 }
116
117 gregorio_vowel_tables_init();
118 gregorio_vowel_table_add(DEFAULT_VOWELS);
119 gregorio_prefix_table_add("i");
120 gregorio_prefix_table_add("I");
121 gregorio_prefix_table_add("u");
122 gregorio_prefix_table_add("U");
123 }
124 }
125
126 static __inline const gregorio_character *skip_verbatim_or_special(
127 const gregorio_character *ch)
128 {
129 /* skip past verbatim and special characters */
130 if (ch->cos.s.type == ST_T_BEGIN && (ch->cos.s.style == ST_VERBATIM
131 || ch->cos.s.style == ST_SPECIAL_CHAR)) {
132 if (ch->next_character) {
133 ch = ch->next_character;
134 }
135 while (ch->next_character && ch->is_character) {
136 ch = ch->next_character;
137 }
138 }
139 return ch;
140 }
141
142 static __inline bool handle_elision(const gregorio_character *const ch,
143 bool *in_elision)
144 {
145 if (ch->cos.s.style != ST_ELISION) {
146 return false;
147 }
148
149 switch (ch->cos.s.type) {
150 case ST_T_BEGIN:
151 *in_elision = true;
152 break;
153
154 case ST_T_END:
155 *in_elision = false;
156 break;
157
158 case ST_T_NOTHING:
159 /* not reachable unless there's a programming error */
160 assert(false); /* LCOV_EXCL_LINE */
161 break;
162 }
163 return true;
164 }
165
166 static __inline int for_each_centerable_character(
167 const gregorio_character *const character,
168 void (*capture)(grewchar *, int, grewchar), grewchar *buffer)
169 {
170 int count;
171 const gregorio_character *ch;
172 bool in_elision = false;
173
174 for (count = 0, ch = character; ch; ch = ch->next_character) {
175 if (ch->is_character) {
176 if (!in_elision) {
177 if (capture) {
178 capture(buffer, count, ch->cos.character);
179 }
180 ++count;
181 }
182 } else {
183 if (ch->cos.s.style == ST_PROTRUSION_FACTOR) {
184 /* if there is a protrusion, we will always encounter the
185 * protrusion factor first */
186 break;
187 }
188 if (ch->cos.s.type == ST_T_BEGIN && ch->cos.s.style == ST_ELISION) {
189 /* put in a special character at the beginning of an elision
190 * that represents the elision */
191 if (capture) {
192 capture(buffer, count, GREVOWEL_ELISION_MARK);
193 }
194 ++count;
195 }
196 if (!handle_elision(ch, &in_elision)) {
197 ch = skip_verbatim_or_special(ch);
198 }
199 }
200 }
201 return count;
202 }
203
204 /* this function assumes subject is sized correctly */
205 static void capture_subject(grewchar *const subject, const int count,
206 const grewchar character)
207 {
208 subject[count] = character;
209 }
210
211 static void determine_center(gregorio_character *character, int *start,
212 int *end)
213 {
214 const int count = for_each_centerable_character(character, NULL, NULL);
215 grewchar *subject;
216
217 if (count == 0) {
218 return;
219 }
220
221 subject = (grewchar *)gregorio_malloc((count + 1) * sizeof(grewchar));
222 for_each_centerable_character(character, capture_subject, subject);
223 subject[count] = (grewchar)0;
224
225 *start = *end = -1;
226 gregorio_find_vowel_group(subject, start, end);
227
228 free(subject);
229 }
230
231 static bool go_to_end_initial(gregorio_character **param_character)
232 {
233 bool has_initial = false;
234 gregorio_character *current_character = *param_character;
235 if (!current_character) {
236 return false;
237 }
238 gregorio_go_to_first_character_c(&current_character);
239 /* skip past any initial */
240 if (!current_character->is_character
241 && current_character->cos.s.type == ST_T_BEGIN
242 && current_character->cos.s.style == ST_INITIAL) {
243 has_initial = true;
244 while (current_character) {
245 if (!current_character->is_character
246 && current_character->cos.s.type == ST_T_END
247 && current_character->cos.s.style == ST_INITIAL) {
248 break;
249 }
250 current_character = current_character->next_character;
251 }
252 }
253 (*param_character) = current_character;
254 return has_initial;
255 }
256
257 /*
258 * The push function pushes a style in the stack, and updates first_style to
259 * this element.
260 */
261
262 static void style_push(det_style **current_style, grestyle_style style)
263 {
264 det_style *element;
265 gregorio_assert(current_style, style_push, "current_style may not be NULL",
266 return);
267 element = (det_style *) gregorio_malloc(sizeof(det_style));
268 element->style = style;
269 element->previous_style = NULL;
270 element->next_style = (*current_style);
271 if (*current_style) {
272 (*current_style)->previous_style = element;
273 }
274 (*current_style) = element;
275 }
276
277 /*
278 * Pop deletes an style in the stack (the style to delete is the parameter)
279 */
280
281 static void style_pop(det_style **first_style, det_style *element)
282 {
283 gregorio_assert(element && first_style && *first_style, style_pop,
284 "invalid arguments", return);
285 if (element->previous_style) {
286 assert(*first_style != element);
287 element->previous_style->next_style = element->next_style;
288 if (element->next_style) {
289 element->next_style->previous_style = element->previous_style;
290 }
291 } else {
292 if (element->next_style) {
293 element->next_style->previous_style = NULL;
294 (*first_style) = element->next_style;
295 } else {
296 (*first_style) = NULL;
297 }
298 }
299 free(element);
300 }
301
302 /*
303 * free_styles just frees the stack.
304 */
305
306 static void free_styles(det_style **first_style)
307 {
308 det_style *current_style;
309 gregorio_not_null(first_style, free_styles, return);
310 current_style = (*first_style);
311 while (current_style) {
312 current_style = current_style->next_style;
313 free((*first_style));
314 (*first_style) = current_style;
315 }
316 }
317
318
319 /*
320 * inline function that will be used for verbatim and special-characters in the
321 *
322 * next function, it calls function with a grewchar * which is the verbatim or
323 * special-character. It places current_character to the character next to the
324 * end of the verbatim or special_char charachters.
325 */
326 static __inline void verb_or_sp(const gregorio_character **ptr_character,
327 const grestyle_style style, FILE *const f,
328 void (*const function) (FILE *, const grewchar *))
329 {
330 int i, j;
331 grewchar *text;
332 const gregorio_character *current_character;
333 const gregorio_character *begin_character;
334
335 i = 0;
336 j = 0;
337
338 current_character = (*ptr_character)->next_character;
339 begin_character = current_character;
340 while (current_character) {
341 if (current_character->cos.s.type == ST_T_END
342 && current_character->cos.s.style == style) {
343 break;
344 } else {
345 if (current_character->is_character) {
346 i++;
347 }
348 current_character = current_character->next_character;
349 }
350 }
351 if (i == 0) {
352 *ptr_character = current_character;
353 return;
354 }
355 text = (grewchar *) gregorio_malloc((i + 1) * sizeof(grewchar));
356 current_character = begin_character;
357 while (j < i) {
358 if (current_character->is_character) {
359 text[j] = current_character->cos.character;
360 j++;
361 }
362 current_character = current_character->next_character;
363 }
364 text[i] = 0;
365 function(f, text);
366 free(text);
367
368 *ptr_character = current_character;
369 }
370
371 /**
372 * This function is made to simplify the output modules : they just have to
373 * declare some simple functions (what to do when meeting a beginning of style,
374 * a character, etc.) and to call this function with pointer to these
375 * functions, and that will automatically write the good ouput. This function
376 * does not test at all the gregorio_character list, if it is wrong, then the
377 * ouput will be wrong. It is very simple to understand, even if it is a bit
378 * long.
379 *
380 * @warning The difficulty comes when we have to write the first syllable text,
381 * without the first letter.
382 * The behaviour can have some bugs in this case if the first syllable has some
383 * complex styles. It would be a bit stupid to do such a thing, but users are
384 * usually very creative when it comes to inventing twisted things...
385 */
386 void gregorio_write_text(const gregorio_write_text_phase phase,
387 const gregorio_character *current_character, FILE *const f,
388 void (*const printverb) (FILE *, const grewchar *),
389 void (*const printchar) (FILE *, grewchar),
390 void (*const begin) (FILE *, grestyle_style),
391 void (*const end) (FILE *, grestyle_style),
392 void (*const printspchar) (FILE *, const grewchar *))
393 {
394 gregorio_assert(current_character, gregorio_write_text,
395 "current_character may not be NULL", return);
396 while (current_character) {
397 if (current_character->is_character) {
398 printchar(f, current_character->cos.character);
399 } else {
400 if (current_character->cos.s.type == ST_T_BEGIN) {
401 switch (current_character->cos.s.style) {
402 case ST_VERBATIM:
403 verb_or_sp(&current_character, ST_VERBATIM, f, printverb);
404 break;
405 case ST_SPECIAL_CHAR:
406 verb_or_sp(&current_character, ST_SPECIAL_CHAR, f,
407 printspchar);
408 break;
409 case ST_INITIAL:
410 if (phase == WTP_FIRST_SYLLABLE) {
411 while (current_character) {
412 if (!current_character->is_character
413 && current_character->cos.s.type == ST_T_END
414 && current_character->cos.s.style ==
415 ST_INITIAL) {
416 break;
417 }
418 current_character =
419 current_character->next_character;
420 }
421 } else {
422 begin(f, ST_INITIAL);
423 }
424 break;
425 default:
426 begin(f, current_character->cos.s.style);
427 break;
428 }
429 } else { /* ST_T_END */
430 end(f, current_character->cos.s.style);
431 }
432 }
433
434 if (current_character) {
435 current_character = current_character->next_character;
436 }
437 }
438 }
439
440 void gregorio_write_first_letter_alignment_text(
441 const gregorio_write_text_phase phase,
442 const gregorio_character *current_character, FILE *const f,
443 void (*const printverb) (FILE *, const grewchar *),
444 void (*const printchar) (FILE *, grewchar),
445 void (*const begin) (FILE *, grestyle_style),
446 void (*const end) (FILE *, grestyle_style),
447 void (*const printspchar) (FILE *, const grewchar *))
448 {
449 /* stack of styles to close and reopen */
450 det_style *first_style = NULL;
451 det_style *last_style = NULL;
452 int first_letter_open = (phase == WTP_FIRST_SYLLABLE)? 2 : 1;
453
454 gregorio_assert(current_character,
455 gregorio_write_first_letter_alignment_text,
456 "current_character may not be NULL", return);
457
458 /* go to the first character */
459 gregorio_go_to_first_character(&current_character);
460
461 if (phase != WTP_FIRST_SYLLABLE) {
462 begin(f, ST_SYLLABLE_INITIAL);
463 }
464
465 /* loop until there are no characters left */
466 for (; current_character;
467 current_character = current_character->next_character) {
468 bool close_first_letter = false;
469 /* found a real character */
470 if (current_character->is_character) {
471 printchar(f, current_character->cos.character);
472 close_first_letter = first_letter_open != 0;
473 } else switch (current_character->cos.s.type) {
474 case ST_T_NOTHING:
475 /* not reachable unless there's a programming error */
476 assert(false); /* LCOV_EXCL_LINE */
477 break;
478 case ST_T_BEGIN:
479 /* handle styles */
480 switch (current_character->cos.s.style) {
481 case ST_CENTER:
482 case ST_FORCED_CENTER:
483 case ST_INITIAL:
484 case ST_FIRST_SYLLABLE_INITIAL:
485 /* ignore */
486 break;
487 case ST_VERBATIM:
488 verb_or_sp(&current_character, ST_VERBATIM, f, printverb);
489 close_first_letter = first_letter_open != 0;
490 break;
491 case ST_SPECIAL_CHAR:
492 verb_or_sp(&current_character, ST_SPECIAL_CHAR, f, printspchar);
493 close_first_letter = first_letter_open != 0;
494 break;
495 case ST_FIRST_WORD:
496 case ST_FIRST_SYLLABLE:
497 if (phase == WTP_FIRST_SYLLABLE) {
498 break;
499 } /* else fall through */
500 default:
501 /* push the style onto the stack */
502 style_push(&first_style, current_character->cos.s.style);
503 begin(f, current_character->cos.s.style);
504 }
505 break;
506 case ST_T_END:
507 switch (current_character->cos.s.style) {
508 case ST_CENTER:
509 case ST_FORCED_CENTER:
510 case ST_FIRST_SYLLABLE_INITIAL:
511 /* ignore */
512 break;
513 case ST_INITIAL:
514 /* in the case where the initial is closed before the first
515 * letter */
516 close_first_letter = first_letter_open > 1;
517 break;
518 case ST_VERBATIM:
519 case ST_SPECIAL_CHAR:
520 /* not reachable unless there's a programming error */
521 assert(false); /* LCOV_EXCL_LINE */
522 break;
523 case ST_FIRST_WORD:
524 case ST_FIRST_SYLLABLE:
525 if (phase == WTP_FIRST_SYLLABLE) {
526 break;
527 } /* else fall through */
528 default:
529 /* pop the style from the stack */
530 assert(first_style);
531 assert(first_style->style
532 == current_character->cos.s.style);
533 style_pop(&first_style, first_style);
534 end(f, current_character->cos.s.style);
535 }
536 break;
537 }
538
539 while (close_first_letter || (!current_character->next_character
540 && first_letter_open > 0)) {
541 close_first_letter = false;
542 --first_letter_open;
543
544 /* close all the styles in the stack */
545 if (first_style) {
546 det_style *current_style;
547 /* these are immediately closed, so go past them */
548 while (first_style && current_character
549 && current_character->next_character
550 && !current_character->next_character->is_character
551 && current_character->next_character->cos.s.type
552 == ST_T_END
553 && current_character->next_character->cos.s.style
554 == first_style->style) {
555 current_character = current_character->next_character;
556 style_pop(&first_style, first_style);
557 end(f, current_character->cos.s.style);
558 }
559 for (current_style = first_style; current_style;
560 current_style = current_style->next_style) {
561 end(f, current_style->style);
562 last_style = current_style;
563 }
564 }
565
566 end(f, ST_SYLLABLE_INITIAL);
567
568 /* reopen all styles */
569 for (; last_style; last_style = last_style->previous_style) {
570 begin(f, last_style->style);
571 }
572 }
573 }
574
575 if (phase == WTP_FIRST_SYLLABLE) {
576 /* the only way this can happen is if the first syllable is empty,
577 * but if the first syllable is empty, this function will not be
578 * called */
579 gregorio_assert_only(!first_letter_open,
580 gregorio_write_first_letter_alignment_text,
581 "first_syllable was not closed properly");
582 }
583
584 free_styles(&first_style);
585 }
586
587 /*
588 * Here starts the code of the handling of text and styles.
589 *
590 * This part is not the easiest, in fact is is the most complicated. The reason
591 * is that I want to make something coherent in memory (easy to interprete),
592 * and to let the user type whatever he wants. This part was originally written
593 * for gabc, that's why it's always talking about it. But it could as well be
594 * used by other read plugins, to put things in order.
595 *
596 * Basically all the following lines of code are made for the last function,
597 * that will take a "user-written" gregorio_character list into a xml-compliant
598 * and tex-compliant list. It's more complicated than it seems...
599 *
600 * Functionalities: For example if the user types tt<i>ttt<b>ttt</i>tt I want
601 * it to be represented as tt<i>ttt<b>ttt</b></i><b>tt</b>. The fabulous thing
602 * is that it is xml-compliant. This part also determines the middle, for
603 * example pot will be interpreted as p{o}t. When I did that I also thought
604 * about TeX styles that needed things like {p}{o}{t}, so when the user types
605 * <i>pot</i>, it is interpreted as <i>p</i>{<i>o</i>}<i>t</i>.
606 *
607 * Internal structure: To do so we have a structure, det_style, that will help
608 * us : it is a stack (double chained list) of the styles that we have seen
609 * until now. When we encounter a <i>, we push the i style on the stack. If we
610 * encounter a </i> we suppress the i style from the stack. Let's take a more
611 * complex example: if we encounter <i><b></i>, the stack will be first null,
612 * then i then bi, and there we want to end i, but it is not the first style of
613 * the stack, so we close all the styles that we encounter before we encounter
614 * i (remember, this is for xml-compliance), so we insert a </b> before the
615 * </i>. But that's not all, we also write a <b> after the </i>, so that the b
616 * style continues. There our stack is just b. For center, we just close all
617 * the styles in the stack, insert a { and reopen all the styles in the stack.
618 *
619 * The structure used for character, styles, etc. is described in
620 * include/struct.h
621 *
622 * The functionment in this file is quite simple : we add all the characters
623 * that we see, even if they are incoherent, in the gregorio_character list,
624 * and then we call a very complex function that will build a stack of the
625 * style, determine the middle, make all xml-compliant, mow the lawn, etc.
626 *
627 * This code is *really* spaghetti, but I think it's a necessary pain.
628 *
629 */
630
631 /*
632 *
633 * This function inserts a style before current_character, updating the double
634 * chained list.
635 *
636 */
637
638 static void insert_style_before(unsigned char type,
639 unsigned char style, gregorio_character *current_character)
640 {
641 gregorio_character *element =
642 (gregorio_character *) gregorio_malloc(sizeof(gregorio_character));
643 element->is_character = 0;
644 element->cos.s.type = type;
645 element->cos.s.style = style;
646 element->next_character = current_character;
647 if (current_character->previous_character) {
648 current_character->previous_character->next_character = element;
649 }
650 element->previous_character = current_character->previous_character;
651 current_character->previous_character = element;
652 }
653
654 /*
655 *
656 * This function puts a style after current_character, and updates
657 * current_character to the gregorio_character it created. It updates the
658 * double chained list. It does not touche to the det_styles list.
659 *
660 */
661
662 static void insert_style_after(unsigned char type, unsigned char style,
663 gregorio_character **current_character)
664 {
665 gregorio_character *element =
666 (gregorio_character *) gregorio_malloc(sizeof(gregorio_character));
667 element->is_character = 0;
668 element->cos.s.type = type;
669 element->cos.s.style = style;
670 element->next_character = (*current_character)->next_character;
671 if ((*current_character)->next_character) {
672 (*current_character)->next_character->previous_character = element;
673 }
674 element->previous_character = (*current_character);
675 (*current_character)->next_character = element;
676 (*current_character) = element;
677 }
678
679 #if 0
680 static void insert_char_after(grewchar c,
681 gregorio_character **current_character)
682 {
683 gregorio_character *element =
684 (gregorio_character *) gregorio_malloc(sizeof(gregorio_character));
685 element->is_character = 1;
686 element->cos.character = c;
687 element->next_character = (*current_character)->next_character;
688 if ((*current_character)->next_character) {
689 (*current_character)->next_character->previous_character = element;
690 }
691 element->previous_character = (*current_character);
692 (*current_character)->next_character = element;
693 (*current_character) = element;
694 }
695 #endif
696
697 /*
698 *
699 * This function suppresses the character, updates the double chained
700 * list, and returns the character after, if there is one.
701 *
702 */
703
704 static __inline gregorio_character *suppress_character(
705 gregorio_character *current_character)
706 {
707 if (current_character) {
708 gregorio_character *thischaracter = current_character;
709 if (current_character->previous_character) {
710 current_character->previous_character->next_character =
711 current_character->next_character;
712 }
713 if (current_character->next_character) {
714 current_character->next_character->previous_character =
715 current_character->previous_character;
716 }
717 current_character = current_character->next_character;
718 free(thischaracter);
719 }
720 return current_character;
721 }
722
723 /*
724 *
725 * Then start the inline functions for the big function. the first one is the
726 * function we call when we close a style. The magic thing is that is will
727 * prevent things like a<i></i>{<i>b</b>: when the user types a<i>b</i>, if the
728 * middle is between a and b (...), it will interpret it as a{<i>b</i>.
729 *
730 */
731
732 static __inline void close_style(gregorio_character *current_character,
733 det_style *current_style)
734 {
735 /* we suppose that there is a previous_character, because there is a
736 * current_style
737 */
738 gregorio_assert(current_character && current_character->previous_character,
739 close_syllable, "have a style but no previous character", return);
740 if (!current_character->previous_character->is_character
741 && current_character->previous_character->cos.s.style ==
742 current_style->style) {
743 /* we do it this way rather than calling suppress_character on the
744 * previous character because the intent is more obvious to both the
745 * human reading it and to the clang static analyzer */
746 gregorio_character *tofree = current_character->previous_character;
747 if (tofree->previous_character) {
748 tofree->previous_character->next_character = current_character;
749 }
750 current_character->previous_character = tofree->previous_character;
751 free(tofree);
752 } else {
753 insert_style_before(ST_T_END, current_style->style, current_character);
754 }
755 }
756
757 /*
758 *
759 * next the function called when we have determined that we must end the center
760 * here : it closes all styles, adds a } and then reopens all styles.
761 *
762 */
763
764 /* type is ST_CENTER or ST_FORCED_CENTER */
765 static __inline void end_center(grestyle_style type,
766 gregorio_character *current_character, det_style **ptr_style)
767 {
768 det_style *current_style;
769 current_style = *ptr_style;
770
771 while (current_style) {
772 close_style(current_character, current_style);
773 if (current_style->next_style) {
774 current_style = current_style->next_style;
775 } else {
776 break;
777 }
778 }
779 insert_style_before(ST_T_END, type, current_character);
780 while (current_style) {
781 insert_style_before(ST_T_BEGIN, current_style->style,
782 current_character);
783 if (current_style->previous_style) {
784 current_style = current_style->previous_style;
785 } else {
786 break;
787 }
788 }
789
790 *ptr_style = current_style;
791 }
792
793 /*
794 * about the same, but adding a {
795 */
796
797 static __inline void begin_center(grestyle_style type,
798 gregorio_character *current_character, det_style **ptr_style)
799 {
800 det_style *current_style;
801 current_style = *ptr_style;
802
803 while (current_style) {
804 close_style(current_character, current_style);
805 if (current_style->next_style) {
806 current_style = current_style->next_style;
807 } else {
808 break;
809 }
810 }
811 insert_style_before(ST_T_BEGIN, type, current_character);
812 while (current_style) {
813 insert_style_before(ST_T_BEGIN, current_style->style,
814 current_character);
815 if (current_style->previous_style) {
816 current_style = current_style->previous_style;
817 } else {
818 break;
819 }
820 }
821
822 *ptr_style = current_style;
823 }
824
825 /*
826 * THE big function. Very long, using a lot of long macros, etc. I hope you
827 * really want to understand it, 'cause it won't be easy.
828 *
829 * current character is a pointer to a gregorio_character. The
830 * gregorio_character double-chained list it is in will be totally reorganized
831 * so that it is xml compliant, and the function will update it to the first
832 * character of this brand new list.
833 *
834 * center_is_determined has the values present in characters.h.
835 *
836 * Another difficulty is the fact that we must consider characters in verbatim
837 * and special character styles like only one block, we can't say the center is
838 * in the middle of a verbatim block.
839 */
840
841 void gregorio_rebuild_characters(gregorio_character **const param_character,
842 gregorio_center_determination center_is_determined, bool skip_initial)
843 {
844 /* the current_character */
845 gregorio_character *current_character = *param_character;
846 gregorio_character *sentinel;
847 det_style *first_style = NULL;
848 /* determining the type of centering (forced or not) */
849 grestyle_style center_type = ST_NO_STYLE;
850 int start = -1, end = -1, index = -1;
851 bool in_elision = false;
852 det_style *current_style;
853 /* so, here we start: we go to the first_character */
854 if (skip_initial && go_to_end_initial(&current_character)) {
855 if (!current_character->next_character) {
856 /* nothing else to rebuild, but the initial needs to be ST_CENTER */
857 insert_style_after(ST_T_END, ST_CENTER, &current_character);
858 gregorio_go_to_first_character_c(&current_character);
859 insert_style_before(ST_T_BEGIN, ST_CENTER, current_character);
860 gregorio_go_to_first_character_c(&current_character);
861 (*param_character) = current_character;
862 return;
863 }
864 /* move to the character after the initial */
865 current_character = current_character->next_character;
866 } else {
867 if (current_character) {
868 gregorio_go_to_first_character_c(&current_character);
869 }
870 }
871 /* first we see if there is already a center determined */
872 if (center_is_determined == CENTER_NOT_DETERMINED) {
873 center_type = ST_CENTER;
874 determine_center(current_character, &start, &end);
875 } else {
876 center_type = ST_FORCED_CENTER;
877 }
878 if (!current_character) {
879 return;
880 }
881 for (sentinel = current_character; sentinel->next_character;
882 sentinel = sentinel->next_character) {
883 /* just loop to get the last character */
884 }
885 /* put a sentinel at the end to aid in looping */
886 insert_style_after(ST_T_NOTHING, ST_SENTINEL, &sentinel);
887 /* we loop until there isn't any character */
888 while (current_character && current_character != sentinel) {
889 /* IMPORTANT: A continue inside this loop MUST put current_character on
890 * the next character to process */
891
892 /* the first part of the function deals with real characters (not
893 * styles) */
894 if (current_character->is_character) {
895 if (!in_elision) {
896 ++index;
897 if (!center_is_determined && index == start) {
898 /* the firstcase is if the user hasn't determined the
899 * middle, and we have only seen vowels so far (else
900 * center_is_determined would be DETERMINING_MIDDLE). The
901 * current_character is the first vowel, so we start the
902 * center here. */
903 begin_center(center_type, current_character, &first_style);
904 center_is_determined = CENTER_DETERMINING_MIDDLE;
905 } else if (center_is_determined == CENTER_DETERMINING_MIDDLE
906 && index == end) {
907 /* the case where the user has not determined the middle
908 * and we are in the middle section of the syllable, but
909 * there we encounter something that is not a vowel, so the
910 * center ends there. */
911 end_center(center_type, current_character, &first_style);
912 center_is_determined = CENTER_FULLY_DETERMINED;
913 } /* else it is just a normal character... we simply pass. */
914 }
915 } else {
916 const grestyle_style style = current_character->cos.s.style;
917 /* there starts the second part of the function that deals with the
918 * styles characters */
919 switch (current_character->cos.s.type) {
920 case ST_T_BEGIN:
921 /* the beginning of a style */
922
923 if (center_is_determined == CENTER_DETERMINING_MIDDLE
924 && index == end - 1) {
925 /* if we are determining the end of the center and we have
926 * a, ELISION, VERBATIM or SPECIAL_CHAR style, we end the
927 * center determination */
928 switch (style) {
929 case ST_VERBATIM:
930 case ST_SPECIAL_CHAR:
931 case ST_ELISION:
932 end_center(center_type, current_character, &first_style);
933 center_is_determined = CENTER_FULLY_DETERMINED;
934 break;
935
936 default:
937 /* something else; don't do anything */
938 break;
939 }
940 }
941
942 if (style == ST_ELISION) {
943 /* skip over the elision marker in the vowel determination
944 * buffer */
945 ++index;
946 }
947
948 switch (style) {
949 case ST_VERBATIM:
950 case ST_SPECIAL_CHAR:
951 /* Here we pass all the characters after a verbatim (or
952 * special char) beginning, until we find a style (begin or
953 * end) */
954 if (current_character->next_character) {
955 current_character = current_character->next_character;
956 }
957 while (current_character && current_character != sentinel
958 && current_character->is_character) {
959 current_character = current_character->next_character;
960 }
961 assert(current_character);
962 if (current_character != sentinel) {
963 gregorio_assert2(!current_character->is_character
964 && current_character->cos.s.type == ST_T_END
965 && current_character->cos.s.style == style,
966 gregorio_rebuild_characters, "mismatched %s",
967 grestyle_style_to_string(style), break);
968 }
969 break;
970
971 case ST_CENTER:
972 /* not reachable unless there's a programming error */
973 /* LCOV_EXCL_START */
974 gregorio_fail(gregorio_rebuild_characters,
975 "encountered unexpected begin ST_CENTER");
976 break;
977 /* LCOV_EXCL_STOP */
978
979 case ST_FORCED_CENTER:
980 begin_center(center_type, current_character, &first_style);
981 current_character = suppress_character(current_character);
982 continue;
983
984 default:
985 handle_elision(current_character, &in_elision);
986 /* it the beginning of a style, which is not center. We
987 * check if the style is not already in the stack. If it is
988 * the case, we suppress the character and pass */
989 for (current_style = first_style; current_style
990 && current_style->style != current_character->cos.s.style;
991 current_style = current_style->next_style) {
992 /* just loop */
993 }
994 if (current_style) {
995 /* repeated styles should already have been filtered out
996 * during parsing, so if this is hit, it's a programming
997 * error */
998 /* LCOV_EXCL_START */
999 gregorio_fail(gregorio_rebuild_characters,
1000 "encountered repeated style");
1001 current_character = suppress_character(current_character);
1002 continue;
1003 /* LCOV_EXCL_STOP */
1004 }
1005 /* if it is something to add then we just push the style in the
1006 * stack and continue. */
1007 style_push(&first_style, current_character->cos.s.style);
1008 break;
1009 }
1010 break;
1011
1012 case ST_T_END:
1013 /* the beginning of a style */
1014 switch(current_character->cos.s.style) {
1015 case ST_VERBATIM:
1016 case ST_SPECIAL_CHAR:
1017 case ST_CENTER:
1018 /* not reachable unless there's a programming error */
1019 /* LCOV_EXCL_START */
1020 gregorio_fail2(gregorio_rebuild_characters,
1021 "encountered unexpected end %s",
1022 grestyle_style_to_string(
1023 current_character->cos.s.style));
1024 break;
1025 /* LCOV_EXCL_STOP */
1026
1027 case ST_FORCED_CENTER:
1028 end_center(center_type, current_character, &first_style);
1029 current_character = suppress_character(current_character);
1030 continue;
1031
1032 default:
1033 handle_elision(current_character, &in_elision);
1034 /* the case of the end of a style (the most complex).
1035 * First, we have to see if the style is in the stack. If
1036 * there is no stack, we just suppress and continue. */
1037 if (!first_style) {
1038 current_character = suppress_character(current_character);
1039 continue;
1040 }
1041 /* so, we look if it is in the stack. If it is we put
1042 * current_style to the style just before the style
1043 * corresponding to the character that we are treating
1044 * (still there ?) */
1045 for (current_style = first_style; current_style
1046 && current_style->style
1047 != current_character->cos.s.style;
1048 current_style = current_style->next_style) {
1049 /* just loop */
1050 }
1051 if (current_style) {
1052 det_style *this_style;
1053 /* if there are styles before in the stack, we close
1054 * them */
1055 for (this_style = first_style;
1056 this_style != current_style;
1057 this_style = this_style->next_style) {
1058 insert_style_before(ST_T_END, this_style->style,
1059 current_character);
1060 }
1061 /* and then we reopen them */
1062 for (this_style = this_style->previous_style;
1063 this_style;
1064 this_style = this_style->previous_style) {
1065 insert_style_after(ST_T_BEGIN, this_style->style,
1066 &current_character);
1067 }
1068 /* we delete the style in the stack */
1069 style_pop(&first_style, current_style);
1070 } else {
1071 current_character = suppress_character(current_character);
1072 continue;
1073 }
1074 }
1075 break;
1076
1077 default:
1078 /* not reachable unless there's a programming error */
1079 /* LCOV_EXCL_START */
1080 gregorio_fail(gregorio_rebuild_characters,
1081 "unexpected style type");
1082 }
1083 /* LCOV_EXCL_STOP */
1084 }
1085 current_character = current_character->next_character;
1086 }
1087 /* destroy the sentinel */
1088 current_character = sentinel->previous_character;
1089 current_character->next_character = NULL;
1090 free(sentinel);
1091 /* we terminate all the styles that are still in the stack */
1092 for (current_style = first_style; current_style;
1093 current_style = current_style->next_style) {
1094 insert_style_after(ST_T_END, current_style->style,
1095 &current_character);
1096 }
1097 /* current_character is at the end of the list now, so if we havn't closed
1098 * the center, we do it at the end. */
1099 if (center_is_determined != CENTER_FULLY_DETERMINED) {
1100 insert_style_after(ST_T_END, center_type, &current_character);
1101 }
1102 /* these three lines are for the case where the user didn't tell anything
1103 * about the middle and there aren't any vowel in the syllable, so we
1104 * begin the center before the first character (you can notice that there
1105 * is no problem of style). */
1106 if (!center_is_determined) {
1107 if (skip_initial && go_to_end_initial(&current_character)) {
1108 current_character = current_character->next_character;
1109 } else {
1110 gregorio_go_to_first_character_c(&current_character);
1111 }
1112 insert_style_before(ST_T_BEGIN, ST_CENTER, current_character);
1113 }
1114 /* well.. you're quite brave if you reach this comment. */
1115 gregorio_go_to_first_character_c(&current_character);
1116 (*param_character) = current_character;
1117 free_styles(&first_style);
1118 }
1119
1120 /**
1121 * @brief This function will determine the behaviour of gregorio when it comes to the recognition of the initial.
1122 *
1123 * Basically it will take a gregorio_character list and return the same list, but with a style added : ST_INITIAL. This style will incidate the initial. The center will be placed at the second letter, unless there is a FORCED_CENTER somewhere.
1124 * Finally all will be xml-compliant and tex-compliant when we call gregorio_rebuild_characters.
1125 *
1126 * If we note <> for the initial and {} for the center, here is what we want:
1127 *
1128 * @li \verbatim Po -> <P>{o} \endverbatim
1129 * @li \verbatim {A}b -> <>{A}b \endverbatim
1130 * @li \verbatim {}a -> <>{}a \endverbatim
1131 * @li \verbatim Glo -> <G>{l}o \endverbatim
1132 * @li \verbatim Gl{o} -> <G>l{o} \endverbatim
1133 *
1134 * @param param_character is a pointer to the (pointer to the) first character, it will be modified so that it points to the new first character.
1135 */
1136 void gregorio_rebuild_first_syllable(gregorio_character **param_character,
1137 bool separate_initial)
1138 {
1139 /* the current_character */
1140 gregorio_character *current_character = *param_character;
1141 gregorio_character *first_character;
1142 gregorio_character *start_of_special;
1143 /* so, here we start: we go to the first_character */
1144 gregorio_go_to_first_character_c(&current_character);
1145 /* first we look at the styles to see if there is a FORCED_CENTER
1146 * somewhere */
1147 /* Note: we don't have to deal with ST_CENTER here because
1148 * gregorio_rebuild_first_syllable is meant to be run **before**
1149 * gregorio_rebuild_characters, which is what adds ST_CENTER */
1150 gregorio_assert(param_character, gregorio_rebuild_first_syllable,
1151 "param_character may not be NULL", return);
1152 while (current_character) {
1153 if (!current_character->is_character) {
1154 if (current_character->cos.s.style == ST_FORCED_CENTER) {
1155 /* we can break here, as there won't be forced center plus
1156 * center */
1157 break;
1158 }
1159 }
1160 current_character = current_character->next_character;
1161 }
1162 current_character = *param_character;
1163 gregorio_go_to_first_character_c(&current_character);
1164 first_character = current_character;
1165 /* now we are going to place the two INITIAL styles (begin and end) */
1166 while (current_character) {
1167 if (!current_character->is_character
1168 && (current_character->cos.s.style == ST_FORCED_CENTER
1169 || current_character->cos.s.style == ST_PROTRUSION_FACTOR)) {
1170 /* we don't touch anything after a FORCED_CENTER, so we put an
1171 * empty INITIAL style just before; we handle the weird case of
1172 * a protrusion (manual or automatic) at the initial the same way
1173 * (we need only check for PROTRUSION_FACTOR since that will come
1174 * before PROTRUSION) */
1175 current_character = first_character;
1176 insert_style_before(ST_T_BEGIN, ST_INITIAL, current_character);
1177 current_character = current_character->previous_character;
1178 insert_style_after(ST_T_END, ST_INITIAL, &current_character);
1179 break;
1180 }
1181 /* this if is a hack to make gregorio consider verbatim blocks and
1182 * special chars like one block, not a sequence of letters */
1183 if (!current_character->is_character
1184 && current_character->cos.s.type == ST_T_BEGIN
1185 && (current_character->cos.s.style == ST_VERBATIM
1186 || current_character->cos.s.style == ST_SPECIAL_CHAR)) {
1187 insert_style_before(ST_T_BEGIN, ST_INITIAL, current_character);
1188 start_of_special = current_character->previous_character;
1189 /* ... which is now the begin-initial style */
1190
1191 if (current_character->next_character) {
1192 current_character = current_character->next_character;
1193 }
1194 while (current_character->next_character
1195 && current_character->is_character) {
1196 current_character = current_character->next_character;
1197 }
1198 insert_style_after(ST_T_END, ST_INITIAL, &current_character);
1199 if (separate_initial && start_of_special->previous_character) {
1200 /* we need to move the initial to the front */
1201 start_of_special->previous_character->next_character =
1202 current_character->next_character;
1203 current_character->next_character->previous_character =
1204 start_of_special->previous_character;
1205 start_of_special->previous_character = NULL;
1206 current_character->next_character = first_character;
1207 first_character->previous_character = current_character;
1208 }
1209 break;
1210 }
1211 if (current_character->is_character) {
1212 if (separate_initial && current_character->previous_character
1213 && current_character->next_character) {
1214 /* we need to move the initial to the front */
1215 current_character->previous_character->next_character =
1216 current_character->next_character;
1217 current_character->next_character->previous_character =
1218 current_character->previous_character;
1219 current_character->previous_character = NULL;
1220 current_character->next_character = first_character;
1221 first_character->previous_character = current_character;
1222 }
1223 insert_style_before(ST_T_BEGIN, ST_INITIAL, current_character);
1224 insert_style_after(ST_T_END, ST_INITIAL, &current_character);
1225 break;
1226 }
1227 current_character = current_character->next_character;
1228 }
1229 /* now apply the first syllable style */
1230 current_character = *param_character;
1231 if (separate_initial) {
1232 if (go_to_end_initial(&current_character)) {
1233 current_character = current_character->next_character;
1234 }
1235 } else {
1236 gregorio_go_to_first_character_c(&current_character);
1237 }
1238 if (current_character) {
1239 bool marked_syllable_initial = false;
1240 insert_style_before(ST_T_BEGIN, ST_FIRST_SYLLABLE, current_character);
1241 do {
1242 if (!marked_syllable_initial && (current_character->is_character
1243 || (current_character->cos.s.type == ST_T_BEGIN
1244 && (current_character->cos.s.style == ST_VERBATIM
1245 || current_character->cos.s.style
1246 == ST_SPECIAL_CHAR
1247 )
1248 )
1249 )) {
1250 marked_syllable_initial = true;
1251 insert_style_before(ST_T_BEGIN, ST_FIRST_SYLLABLE_INITIAL,
1252 current_character);
1253 if (!current_character->is_character) {
1254 /* skip past the verbatim or special character */
1255 if (current_character->next_character) {
1256 current_character = current_character->next_character;
1257 }
1258 while (current_character->next_character
1259 && current_character->is_character) {
1260 current_character = current_character->next_character;
1261 }
1262 }
1263 insert_style_after(ST_T_END, ST_FIRST_SYLLABLE_INITIAL,
1264 &current_character);
1265 }
1266 if (!current_character->is_character) {
1267 switch (current_character->cos.s.style) {
1268 case ST_CENTER:
1269 case ST_FORCED_CENTER:
1270 insert_style_before(ST_T_END, ST_FIRST_SYLLABLE,
1271 current_character);
1272 insert_style_after(ST_T_BEGIN, ST_FIRST_SYLLABLE,
1273 &current_character);
1274 break;
1275
1276 case ST_PROTRUSION_FACTOR:
1277 /* protrusion is not allowed to be the syllable initial;
1278 * we only need to check PROTRUSION_FACTOR since that will
1279 * come before PROTRUSION */
1280 marked_syllable_initial = true;
1281 break;
1282
1283 default:
1284 break;
1285 }
1286 }
1287 if (!current_character->next_character) {
1288 insert_style_after(ST_T_END, ST_FIRST_SYLLABLE,
1289 &current_character);
1290 }
1291 current_character = current_character->next_character;
1292 } while (current_character);
1293 }
1294
1295 current_character = *param_character;
1296 gregorio_go_to_first_character_c(&current_character);
1297 (*param_character) = current_character;
1298 }
1299
1300 void gregorio_set_first_word(gregorio_character **const character)
1301 {
1302 gregorio_character *ch = *character;
1303 if (go_to_end_initial(&ch)) {
1304 ch = ch->next_character;
1305 }
1306 if (ch) {
1307 bool started_style = false;
1308 while (ch) {
1309 if (!ch->is_character
1310 && (ch->cos.s.style == ST_CENTER
1311 || ch->cos.s.style == ST_FORCED_CENTER)) {
1312 if (started_style) {
1313 started_style = false;
1314 insert_style_before(ST_T_END, ST_FIRST_WORD, ch);
1315 }
1316 } else if (!started_style) {
1317 started_style = true;
1318 insert_style_before(ST_T_BEGIN, ST_FIRST_WORD, ch);
1319 }
1320
1321 if (!ch->next_character && started_style) {
1322 insert_style_after(ST_T_END, ST_FIRST_WORD, &ch);
1323 }
1324
1325 ch = ch->next_character;
1326 }
1327 }
1328 /* else there are no more characters here */
1329
1330 if (*character) {
1331 gregorio_go_to_first_character_c(character);
1332 }
1333 }