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