]> git.donarmstrong.com Git - lilypond.git/blob - lily/gregorian-ligature-engraver.cc
ca09fe88fb363b48acade31ca3de7e42cbb45fea
[lilypond.git] / lily / gregorian-ligature-engraver.cc
1 /*
2   gregorian-ligature-engraver.cc -- implement Gregorian_ligature_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c)  2003 Juergen Reuter <reuter@ipd.uka.de>
7  */
8
9 #include "gregorian-ligature-engraver.hh"
10 #include "gregorian-ligature.hh"
11 #include "item.hh"
12 #include "warn.hh"
13 #include "staff-symbol-referencer.hh"
14 #include "spanner.hh"
15 #include "paper-column.hh"
16
17 /*
18  * This abstract class is the common superclass for all ligature
19  * engravers for Gregorian chant notation.  It cares for the musical
20  * handling of the neumes, such as checking for valid combinations of
21  * neumes and providing context information.  Notational aspects such
22  * as the glyphs to use or calculating the total width of a ligature,
23  * are left to the concrete subclass.  Currently, there is only a
24  * single subclass, Vaticana_ligature_engraver.  Other ligature
25  * engravers for Gregorian chant will be added in the future, such as
26  * Medicaea_ligature_engraver or Hufnagel_ligature_engraver.
27  */
28 Gregorian_ligature_engraver::Gregorian_ligature_engraver ()
29 {
30   pes_or_flexa_req_ = 0;
31 }
32
33 bool
34 Gregorian_ligature_engraver::try_music (Music *m)
35 {
36   if (m->is_mus_type ("pes-or-flexa-event"))
37     {
38       pes_or_flexa_req_ = m;
39       return true;
40     }
41   else
42     return Ligature_engraver::try_music (m);
43 }
44
45 void fix_prefix (char *name, int mask,
46                  int *current_set, int min_set, int max_set,
47                  Grob *primitive)
48 {
49   bool current = *current_set & mask;
50   bool min = min_set & mask;
51   bool max = max_set & mask;
52   if (max < min)
53     {
54       programming_error ("min_set > max_set");
55       return;
56     }
57   if (min && !current)
58     {
59       primitive->warning (_f ("\\%s ignored", name));
60       *current_set &= ~mask;
61     }
62   if (!max && current)
63     {
64       primitive->warning (_f ("implied \\%s added", name));
65       *current_set |= mask;
66     }
67 }
68
69 void fix_prefix_set (int *current_set, int min_set, int max_set, Grob *primitive)
70 {
71   fix_prefix ("virga", VIRGA, current_set, min_set, max_set, primitive);
72   fix_prefix ("stropha", STROPHA, current_set, min_set, max_set, primitive);
73   fix_prefix ("inclinatum", INCLINATUM, current_set, min_set, max_set, primitive);
74   fix_prefix ("auctum", AUCTUM, current_set, min_set, max_set, primitive);
75   fix_prefix ("descendens", DESCENDENS, current_set, min_set, max_set, primitive);
76   fix_prefix ("ascendens", ASCENDENS, current_set, min_set, max_set, primitive);
77   fix_prefix ("oriscus", ORISCUS, current_set, min_set, max_set, primitive);
78   fix_prefix ("quilisma", QUILISMA, current_set, min_set, max_set, primitive);
79   fix_prefix ("deminutus", DEMINUTUM, current_set, min_set, max_set, primitive);
80   fix_prefix ("semivocalis", SEMIVOCALIS, current_set, min_set, max_set, primitive);
81   fix_prefix ("cavum", CAVUM, current_set, min_set, max_set, primitive);
82   fix_prefix ("linea", LINEA, current_set, min_set, max_set, primitive);
83   fix_prefix ("pes_or_flexa", LINEA, current_set, min_set, max_set, primitive);
84 }
85
86 void check_and_fix_all_prefixes (Array<Grob_info> primitives)
87 {
88   /* Check for illegal head modifier combinations */
89   for (int i = 0; i < primitives.size(); i++)
90     {
91     Grob *primitive = primitives[i].grob_;
92
93     /* compute head prefix set by inspecting primitive grob properties */
94     int prefix_set =
95       (VIRGA * to_boolean (primitive->get_grob_property ("virga"))) |
96       (STROPHA * to_boolean (primitive->get_grob_property ("stropha"))) |
97       (INCLINATUM * to_boolean (primitive->get_grob_property ("inclinatum"))) |
98       (AUCTUM * to_boolean (primitive->get_grob_property ("auctum"))) |
99       (DESCENDENS * to_boolean (primitive->get_grob_property ("descendens"))) |
100       (ASCENDENS * to_boolean (primitive->get_grob_property ("ascendens"))) |
101       (ORISCUS * to_boolean (primitive->get_grob_property ("oriscus"))) |
102       (QUILISMA * to_boolean (primitive->get_grob_property ("quilisma"))) |
103       (DEMINUTUM * to_boolean (primitive->get_grob_property ("deminutum"))) |
104       (SEMIVOCALIS * to_boolean (primitive->get_grob_property ("semivocalis"))) |
105       (CAVUM * to_boolean (primitive->get_grob_property ("cavum"))) |
106       (LINEA * to_boolean (primitive->get_grob_property ("linea"))) |
107       (PES_OR_FLEXA * to_boolean (primitive->get_grob_property ("pes-or-flexa")));
108
109     /* check: ascendens and descendens exclude each other; same with
110        auctum and diminutum */
111     if (prefix_set & DESCENDENS)
112       {
113         fix_prefix_set (&prefix_set,
114                         prefix_set & ~ASCENDENS,
115                         prefix_set & ~ASCENDENS,
116                         primitive);
117       }
118     if (prefix_set & AUCTUM)
119       {
120         fix_prefix_set (&prefix_set,
121                         prefix_set & ~DEMINUTUM,
122                         prefix_set & ~DEMINUTUM,
123                         primitive);
124       }
125
126     /* check: virga, quilisma and oriscus can not be combined with any
127        other prefix, but may be part of a pes or flexa */
128     if (prefix_set & VIRGA)
129       {
130         fix_prefix_set (&prefix_set,
131                         VIRGA,
132                         VIRGA | PES_OR_FLEXA,
133                         primitive);
134       }
135     if (prefix_set & QUILISMA)
136       {
137         fix_prefix_set (&prefix_set,
138                         QUILISMA,
139                         QUILISMA | PES_OR_FLEXA,
140                         primitive);
141       }
142     if (prefix_set & ORISCUS)
143       {
144         fix_prefix_set (&prefix_set,
145                         ORISCUS,
146                         ORISCUS | PES_OR_FLEXA,
147                         primitive);
148       }
149
150     /* check: auctum is the only valid optional prefix for stropha */
151     if (prefix_set & STROPHA)
152       {
153         fix_prefix_set (&prefix_set,
154                         STROPHA,
155                         STROPHA | AUCTUM,
156                         primitive);
157       }
158
159     /* check: semivocalis must occur in combination with and only with
160        pes or flexa */
161     if (prefix_set & SEMIVOCALIS)
162       {
163         fix_prefix_set (&prefix_set,
164                         SEMIVOCALIS | PES_OR_FLEXA,
165                         SEMIVOCALIS | PES_OR_FLEXA,
166                         primitive);
167       }
168
169     /* check: inclinatum may be prefixed with auctum or diminutum only */
170     if (prefix_set & INCLINATUM)
171       {
172         fix_prefix_set (&prefix_set,
173                         INCLINATUM,
174                         INCLINATUM | AUCTUM | DEMINUTUM,
175                         primitive);
176       }
177
178     /* check: cavum and linea (either or both) may be applied only
179        upon core punctum */
180     if (prefix_set & (CAVUM | LINEA))
181       {
182         fix_prefix_set (&prefix_set,
183                         0,
184                         CAVUM | LINEA,
185                         primitive);
186       }
187
188     /* all other combinations should be valid (unless I made a
189        mistake) */
190
191     primitive->set_grob_property ("prefix-set", gh_int2scm (prefix_set));
192   }
193 }
194
195 /*
196  * Marks those heads that participate in a pes or flexa.
197  */
198 void
199 provide_context_info (Array<Grob_info> primitives)
200 {
201   Grob *prev_primitive = 0;
202   int prev_prefix_set = 0;
203   int prev_context_info = 0;
204   int prev_pitch = 0;
205   for (int i = 0; i < primitives.size(); i++) {
206     Grob *primitive = primitives[i].grob_;
207     Music *music_cause = primitives[i].music_cause ();
208     int context_info = 0;
209     int pitch = unsmob_pitch (music_cause->get_mus_property ("pitch"))->steps ();
210     int prefix_set = gh_scm2int (primitive->get_grob_property ("prefix-set"));
211
212     if (prefix_set & PES_OR_FLEXA)
213       if (pitch > prev_pitch) // pes
214         {
215           prev_context_info |= PES_LOWER;
216           context_info |= PES_UPPER;
217         }
218       else if (pitch < prev_pitch) // flexa
219         {
220           prev_context_info |= FLEXA_LEFT;
221           context_info |= FLEXA_RIGHT;
222         }
223       else // (pitch == prev_pitch)
224         {
225           primitive->warning ("may not apply `\\~' on heads with "
226                               "identical pitch; ignoring `\\~'");
227         }
228     if (prev_prefix_set & VIRGA)
229       {
230         context_info |= AFTER_VIRGA;
231       }
232
233     if (prev_primitive)
234       prev_primitive->set_grob_property ("context-info",
235                                          gh_int2scm (prev_context_info));
236     prev_primitive = primitive;
237     prev_prefix_set = prefix_set;
238     prev_context_info = context_info;
239     prev_pitch = pitch;
240   }
241   if (prev_primitive)
242     prev_primitive->set_grob_property ("context-info",
243                                        gh_int2scm (prev_context_info));
244 }
245
246 void
247 Gregorian_ligature_engraver::transform_heads (Spanner *, Array<Grob_info>)
248 {
249   programming_error ("Gregorian_ligature_engraver::transform_heads (): "
250                      "this is an abstract method that should not be called, "
251                      "but overridden by a subclass");
252 }
253
254 void
255 Gregorian_ligature_engraver::build_ligature (Spanner *ligature,
256                                              Array<Grob_info> primitives)
257 {
258   // apply style-independent checking and transformation
259   check_and_fix_all_prefixes (primitives);
260   provide_context_info (primitives);
261
262   // apply style-specific transformation (including line-up); to be
263   // implemented by subclass
264   transform_heads (ligature, primitives);
265 }
266
267 void
268 Gregorian_ligature_engraver::start_translation_timestep ()
269 {
270   Ligature_engraver::start_translation_timestep ();
271   pes_or_flexa_req_ = 0;
272 }
273
274 ENTER_DESCRIPTION (Gregorian_ligature_engraver,
275 /* descr */       "This is an abstract class.  Subclasses such as Vaticana_ligature_engraver handle ligatures by glueing special ligature heads together.",
276 /* creats*/       "",
277 /* accepts */     "ligature-event abort-event",
278 /* acks  */      "note-head-interface rest-interface",
279 /* reads */       "",
280 /* write */       "");